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 GCC.
9 GCC 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 GCC 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 GCC; 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"
38 #include "diagnostic.h"
40 extern int inhibit_warnings;
42 static tree build_field_call (tree, tree, tree);
43 static struct z_candidate * tourney (struct z_candidate *);
44 static int equal_functions (tree, tree);
45 static int joust (struct z_candidate *, struct z_candidate *, bool);
46 static int compare_ics (tree, tree);
47 static tree build_over_call (struct z_candidate *, int);
48 static tree build_java_interface_fn_ref (tree, tree);
49 #define convert_like(CONV, EXPR) \
50 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0)
51 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
52 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0)
53 static tree convert_like_real (tree, tree, tree, int, int);
54 static void op_error (enum tree_code, enum tree_code, tree, tree,
56 static tree build_object_call (tree, tree);
57 static tree resolve_args (tree);
58 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
59 static void print_z_candidates (struct z_candidate *);
60 static tree build_this (tree);
61 static struct z_candidate *splice_viable (struct z_candidate *);
62 static bool any_viable (struct z_candidate *);
63 static bool any_strictly_viable (struct z_candidate *);
64 static struct z_candidate *add_template_candidate
65 (struct z_candidate *, tree, tree, tree, tree, tree,
66 tree, tree, int, unification_kind_t);
67 static struct z_candidate *add_template_candidate_real
68 (struct z_candidate *, tree, tree, tree, tree, tree,
69 tree, tree, int, tree, unification_kind_t);
70 static struct z_candidate *add_template_conv_candidate
71 (struct z_candidate *, tree, tree, tree, tree, tree, tree);
72 static struct z_candidate *add_builtin_candidates
73 (struct z_candidate *, enum tree_code, enum tree_code,
75 static struct z_candidate *add_builtin_candidate
76 (struct z_candidate *, enum tree_code, enum tree_code,
77 tree, tree, tree, tree *, tree *, int);
78 static bool is_complete (tree);
79 static struct z_candidate *build_builtin_candidate
80 (struct z_candidate *, tree, tree, tree, tree *, tree *,
82 static struct z_candidate *add_conv_candidate
83 (struct z_candidate *, tree, tree, tree, tree, tree);
84 static struct z_candidate *add_function_candidate
85 (struct z_candidate *, tree, tree, tree, tree, tree, int);
86 static tree implicit_conversion (tree, tree, tree, int);
87 static tree standard_conversion (tree, tree, tree);
88 static tree reference_binding (tree, tree, tree, int);
89 static tree non_reference (tree);
90 static tree build_conv (enum tree_code, tree, tree);
91 static bool is_subseq (tree, tree);
92 static tree maybe_handle_ref_bind (tree *);
93 static void maybe_handle_implicit_object (tree *);
94 static struct z_candidate *add_candidate
95 (struct z_candidate *, tree, tree, tree, tree, tree, int);
96 static tree source_type (tree);
97 static void add_warning (struct z_candidate *, struct z_candidate *);
98 static bool reference_related_p (tree, tree);
99 static bool reference_compatible_p (tree, tree);
100 static tree convert_class_to_reference (tree, tree, tree);
101 static tree direct_reference_binding (tree, tree);
102 static bool promoted_arithmetic_type_p (tree);
103 static tree conditional_conversion (tree, tree);
104 static char *name_as_c_string (tree, tree, bool *);
105 static tree call_builtin_trap (void);
106 static tree prep_operand (tree);
107 static struct z_candidate *add_candidates (tree, tree, tree, tree,
108 int, struct z_candidate *);
111 build_vfield_ref (tree datum, tree type)
113 if (datum == error_mark_node)
114 return error_mark_node;
116 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
117 datum = convert_from_reference (datum);
119 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
120 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
121 datum = convert_to_base (datum, type, /*check_access=*/false);
123 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
124 datum, TYPE_VFIELD (type));
127 /* Build a call to a member of an object. I.e., one that overloads
128 operator ()(), or is a pointer-to-function or pointer-to-method. */
131 build_field_call (tree instance_ptr, tree decl, tree parms)
135 if (decl == error_mark_node || decl == NULL_TREE)
138 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
140 /* If it's a field, try overloading operator (),
141 or calling if the field is a pointer-to-function. */
142 instance = build_indirect_ref (instance_ptr, NULL);
143 instance = build_class_member_access_expr (instance, decl,
144 /*access_path=*/NULL_TREE,
145 /*preserve_reference=*/false);
147 if (instance == error_mark_node)
148 return error_mark_node;
150 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
151 return build_new_op (CALL_EXPR, LOOKUP_NORMAL,
152 instance, parms, NULL_TREE);
153 else if (TREE_CODE (TREE_TYPE (instance)) == FUNCTION_TYPE
154 || (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE
155 && (TREE_CODE (TREE_TYPE (TREE_TYPE (instance)))
157 return build_function_call (instance, parms);
163 /* Returns nonzero iff the destructor name specified in NAME
164 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
168 check_dtor_name (tree basetype, tree 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 (tree exp, tree basetype, tree name, tree parms)
212 /* Because this syntactic form does not allow
213 a pointer to a base class to be `stolen',
214 we need not protect the derived->base conversion
217 @@ But we do have to check access privileges later. */
219 tree type = TREE_TYPE (exp);
221 if (type == error_mark_node
222 || basetype == error_mark_node)
223 return error_mark_node;
225 if (processing_template_decl)
227 name = build_min_nt (SCOPE_REF, basetype, name);
228 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
231 if (TREE_CODE (type) == REFERENCE_TYPE)
232 type = TREE_TYPE (type);
234 if (TREE_CODE (basetype) == TREE_VEC)
237 basetype = BINFO_TYPE (binfo);
242 /* Check the destructor call syntax. */
243 if (TREE_CODE (name) == BIT_NOT_EXPR)
245 /* We can get here if someone writes their destructor call like
246 `obj.NS::~T()'; this isn't really a scoped method call, so hand
248 if (TREE_CODE (basetype) == NAMESPACE_DECL)
249 return build_method_call (exp, name, parms, NULL_TREE, LOOKUP_NORMAL);
251 if (! check_dtor_name (basetype, name))
252 error ("qualified type `%T' does not match destructor name `~%T'",
253 basetype, TREE_OPERAND (name, 0));
255 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
256 that explicit ~int is caught in the parser; this deals with typedefs
257 and template parms. */
258 if (! IS_AGGR_TYPE (basetype))
260 if (TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (basetype))
261 error ("type of `%E' does not match destructor type `%T' (type was `%T')",
262 exp, basetype, type);
264 return cp_convert (void_type_node, exp);
268 if (TREE_CODE (basetype) == NAMESPACE_DECL)
270 error ("`%D' is a namespace", basetype);
271 return error_mark_node;
273 if (! is_aggr_type (basetype, 1))
274 return error_mark_node;
276 if (! IS_AGGR_TYPE (type))
278 error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
280 return error_mark_node;
283 decl = build_scoped_ref (exp, basetype, &binfo);
287 /* Call to a destructor. */
288 if (TREE_CODE (name) == BIT_NOT_EXPR)
290 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
291 return cp_convert (void_type_node, exp);
293 return build_delete (TREE_TYPE (decl), decl,
294 sfk_complete_destructor,
295 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
299 /* Call to a method. */
300 return build_method_call (decl, name, parms, binfo,
301 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
303 return error_mark_node;
306 /* We want the address of a function or method. We avoid creating a
307 pointer-to-member function. */
310 build_addr_func (tree function)
312 tree type = TREE_TYPE (function);
314 /* We have to do these by hand to avoid real pointer to member
316 if (TREE_CODE (type) == METHOD_TYPE)
320 type = build_pointer_type (type);
322 if (!cxx_mark_addressable (function))
323 return error_mark_node;
325 addr = build1 (ADDR_EXPR, type, function);
327 /* Address of a static or external variable or function counts
329 if (staticp (function))
330 TREE_CONSTANT (addr) = 1;
335 function = default_conversion (function);
340 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
341 POINTER_TYPE to those. Note, pointer to member function types
342 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
345 build_call (tree function, tree parms)
347 int is_constructor = 0;
354 function = build_addr_func (function);
356 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
358 sorry ("unable to call pointer to member function here");
359 return error_mark_node;
362 fntype = TREE_TYPE (TREE_TYPE (function));
363 result_type = TREE_TYPE (fntype);
365 if (TREE_CODE (function) == ADDR_EXPR
366 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
367 decl = TREE_OPERAND (function, 0);
371 /* We check both the decl and the type; a function may be known not to
372 throw without being declared throw(). */
373 nothrow = ((decl && TREE_NOTHROW (decl))
374 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
376 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
377 current_function_returns_abnormally = 1;
379 if (decl && TREE_DEPRECATED (decl))
380 warn_deprecated_use (decl);
381 require_complete_eh_spec_types (fntype, decl);
383 if (decl && DECL_CONSTRUCTOR_P (decl))
386 if (decl && ! TREE_USED (decl))
388 /* We invoke build_call directly for several library functions.
389 These may have been declared normally if we're building libgcc,
390 so we can't just check DECL_ARTIFICIAL. */
391 if (DECL_ARTIFICIAL (decl)
392 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
398 /* Don't pass empty class objects by value. This is useful
399 for tags in STL, which are used to control overload resolution.
400 We don't need to handle other cases of copying empty classes. */
401 if (! decl || ! DECL_BUILT_IN (decl))
402 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
403 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
404 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
406 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
407 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
408 TREE_VALUE (tmp), t);
411 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
412 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
413 TREE_TYPE (function) = result_type;
414 TREE_SIDE_EFFECTS (function) = 1;
415 TREE_NOTHROW (function) = nothrow;
420 /* Build something of the form ptr->method (args)
421 or object.method (args). This can also build
422 calls to constructors, and find friends.
424 Member functions always take their class variable
427 INSTANCE is a class instance.
429 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
431 PARMS help to figure out what that NAME really refers to.
433 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
434 down to the real instance type to use for access checking. We need this
435 information to get protected accesses correct. This parameter is used
436 by build_member_call.
438 FLAGS is the logical disjunction of zero or more LOOKUP_
439 flags. See cp-tree.h for more info.
441 If this is all OK, calls build_function_call with the resolved
444 This function must also handle being called to perform
445 initialization, promotion/coercion of arguments, and
446 instantiation of default parameters.
448 Note that NAME may refer to an instance variable name. If
449 `operator()()' is defined for the type of that field, then we return
452 #ifdef GATHER_STATISTICS
453 extern int n_build_method_call;
457 build_method_call (tree instance, tree name, tree parms,
458 tree basetype_path, int flags)
462 tree template_args = NULL_TREE;
463 bool has_template_args = false;
465 #ifdef GATHER_STATISTICS
466 n_build_method_call++;
469 if (instance == error_mark_node
470 || name == error_mark_node
471 || parms == error_mark_node
472 || (instance && TREE_TYPE (instance) == error_mark_node))
473 return error_mark_node;
475 if (processing_template_decl)
476 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
478 if (TREE_CODE (instance) == OFFSET_REF)
479 instance = resolve_offset_ref (instance);
480 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
481 instance = convert_from_reference (instance);
482 object_type = TREE_TYPE (instance);
484 if (TREE_CODE (name) == BIT_NOT_EXPR)
489 error ("destructors take no parameters");
491 if (! check_dtor_name (object_type, name))
493 ("destructor name `~%T' does not match type `%T' of expression",
494 TREE_OPERAND (name, 0), object_type);
496 if (! TYPE_HAS_DESTRUCTOR (complete_type (object_type)))
497 return cp_convert (void_type_node, instance);
498 instance = default_conversion (instance);
499 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
500 return build_delete (build_pointer_type (object_type),
501 instance_ptr, sfk_complete_destructor,
502 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
505 if (!CLASS_TYPE_P (object_type))
507 if ((flags & LOOKUP_COMPLAIN)
508 && TREE_TYPE (instance) != error_mark_node)
509 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
510 name, instance, object_type);
511 return error_mark_node;
514 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
516 template_args = TREE_OPERAND (name, 1);
517 has_template_args = true;
518 name = TREE_OPERAND (name, 0);
520 if (TREE_CODE (name) == OVERLOAD)
521 name = DECL_NAME (get_first_fn (name));
522 else if (DECL_P (name))
523 name = DECL_NAME (name);
524 if (has_template_args)
525 fn = lookup_fnfields (object_type, name, /*protect=*/2);
527 fn = lookup_member (object_type, name, /*protect=*/2, /*want_type=*/false);
529 if (fn && TREE_CODE (fn) == TREE_LIST && !BASELINK_P (fn))
531 error ("request for member `%D' is ambiguous", name);
532 print_candidates (fn);
533 return error_mark_node;
536 /* If the name could not be found, issue an error. */
539 unqualified_name_lookup_error (name);
540 return error_mark_node;
543 if (BASELINK_P (fn) && has_template_args)
544 BASELINK_FUNCTIONS (fn)
545 = build_nt (TEMPLATE_ID_EXPR,
546 BASELINK_FUNCTIONS (fn),
548 if (BASELINK_P (fn) && basetype_path)
549 BASELINK_ACCESS_BINFO (fn) = basetype_path;
551 return build_new_method_call (instance, fn, parms,
552 /*conversion_path=*/NULL_TREE, flags);
555 /* New overloading code. */
557 struct z_candidate GTY(()) {
558 /* The FUNCTION_DECL that will be called if this candidate is
559 selected by overload resolution. */
561 /* The arguments to use when calling this function. */
566 /* If FN is a member function, the binfo indicating the path used to
567 qualify the name of FN at the call site. This path is used to
568 determine whether or not FN is accessible if it is selected by
569 overload resolution. The DECL_CONTEXT of FN will always be a
570 (possibly improper) base of this binfo. */
572 /* If FN is a non-static member function, the binfo indicating the
573 subobject to which the `this' pointer should be converted if FN
574 is selected by overload resolution. The type pointed to the by
575 the `this' pointer must correspond to the most derived class
576 indicated by the CONVERSION_PATH. */
577 tree conversion_path;
580 struct z_candidate *next;
583 #define IDENTITY_RANK 0
589 #define ELLIPSIS_RANK 6
592 #define ICS_RANK(NODE) \
593 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
594 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
595 : ICS_USER_FLAG (NODE) ? USER_RANK \
596 : ICS_STD_RANK (NODE))
598 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
600 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
601 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
602 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
603 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
605 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
606 should be created to hold the result of the conversion. */
607 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
609 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
610 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
613 null_ptr_cst_p (tree t)
617 A null pointer constant is an integral constant expression
618 (_expr.const_) rvalue of integer type that evaluates to zero. */
620 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
626 /* Returns nonzero if PARMLIST consists of only default parms and/or
630 sufficient_parms_p (tree parmlist)
632 for (; parmlist && parmlist != void_list_node;
633 parmlist = TREE_CHAIN (parmlist))
634 if (!TREE_PURPOSE (parmlist))
640 build_conv (enum tree_code code, tree type, tree from)
643 int rank = ICS_STD_RANK (from);
645 /* We can't use buildl1 here because CODE could be USER_CONV, which
646 takes two arguments. In that case, the caller is responsible for
647 filling in the second argument. */
648 t = make_node (code);
649 TREE_TYPE (t) = type;
650 TREE_OPERAND (t, 0) = from;
663 if (rank < EXACT_RANK)
669 ICS_STD_RANK (t) = rank;
670 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
671 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
675 /* If T is a REFERENCE_TYPE return the type to which T refers.
676 Otherwise, return T itself. */
679 non_reference (tree t)
681 if (TREE_CODE (t) == REFERENCE_TYPE)
687 strip_top_quals (tree t)
689 if (TREE_CODE (t) == ARRAY_TYPE)
691 return TYPE_MAIN_VARIANT (t);
694 /* Returns the standard conversion path (see [conv]) from type FROM to type
695 TO, if any. For proper handling of null pointer constants, you must
696 also pass the expression EXPR to convert from. */
699 standard_conversion (tree to, tree from, tree expr)
701 enum tree_code fcode, tcode;
703 bool fromref = false;
705 if (TREE_CODE (to) == REFERENCE_TYPE)
707 if (TREE_CODE (from) == REFERENCE_TYPE)
710 from = TREE_TYPE (from);
712 to = strip_top_quals (to);
713 from = strip_top_quals (from);
715 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
716 && expr && type_unknown_p (expr))
718 expr = instantiate_type (to, expr, tf_none);
719 if (expr == error_mark_node)
721 from = TREE_TYPE (expr);
724 fcode = TREE_CODE (from);
725 tcode = TREE_CODE (to);
727 conv = build1 (IDENTITY_CONV, from, expr);
729 if (fcode == FUNCTION_TYPE)
731 from = build_pointer_type (from);
732 fcode = TREE_CODE (from);
733 conv = build_conv (LVALUE_CONV, from, conv);
735 else if (fcode == ARRAY_TYPE)
737 from = build_pointer_type (TREE_TYPE (from));
738 fcode = TREE_CODE (from);
739 conv = build_conv (LVALUE_CONV, from, conv);
741 else if (fromref || (expr && lvalue_p (expr)))
742 conv = build_conv (RVALUE_CONV, from, conv);
744 /* Allow conversion between `__complex__' data types */
745 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
747 /* The standard conversion sequence to convert FROM to TO is
748 the standard conversion sequence to perform componentwise
750 tree part_conv = standard_conversion
751 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
755 conv = build_conv (TREE_CODE (part_conv), to, conv);
756 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
764 if (same_type_p (from, to))
767 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
768 && expr && null_ptr_cst_p (expr))
770 conv = build_conv (STD_CONV, to, conv);
772 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
773 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
775 /* For backwards brain damage compatibility, allow interconversion of
776 pointers and integers with a pedwarn. */
777 conv = build_conv (STD_CONV, to, conv);
778 ICS_BAD_FLAG (conv) = 1;
780 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE
781 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
783 /* For backwards brain damage compatibility, allow interconversion of
784 enums and integers with a pedwarn. */
785 conv = build_conv (STD_CONV, to, conv);
786 ICS_BAD_FLAG (conv) = 1;
788 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
790 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
791 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
793 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
796 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
797 && ufcode != FUNCTION_TYPE)
799 from = build_pointer_type
800 (cp_build_qualified_type (void_type_node,
801 cp_type_quals (TREE_TYPE (from))));
802 conv = build_conv (PTR_CONV, from, conv);
804 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
806 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
807 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
809 if (DERIVED_FROM_P (fbase, tbase)
810 && (same_type_ignoring_top_level_qualifiers_p
811 (TREE_TYPE (TREE_TYPE (from)),
812 TREE_TYPE (TREE_TYPE (to)))))
814 from = build_ptrmem_type (tbase, TREE_TYPE (TREE_TYPE (from)));
815 conv = build_conv (PMEM_CONV, from, conv);
818 else if (IS_AGGR_TYPE (TREE_TYPE (from))
819 && IS_AGGR_TYPE (TREE_TYPE (to)))
821 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
824 cp_build_qualified_type (TREE_TYPE (to),
825 cp_type_quals (TREE_TYPE (from)));
826 from = build_pointer_type (from);
827 conv = build_conv (PTR_CONV, from, conv);
831 if (same_type_p (from, to))
833 else if (comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
834 conv = build_conv (QUAL_CONV, to, conv);
835 else if (expr && string_conv_p (to, expr, 0))
836 /* converting from string constant to char *. */
837 conv = build_conv (QUAL_CONV, to, conv);
838 else if (ptr_reasonably_similar (TREE_TYPE (to), TREE_TYPE (from)))
840 conv = build_conv (PTR_CONV, to, conv);
841 ICS_BAD_FLAG (conv) = 1;
848 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
850 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
851 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
852 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
853 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
855 if (!DERIVED_FROM_P (fbase, tbase)
856 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
857 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
858 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
859 || cp_type_quals (fbase) != cp_type_quals (tbase))
862 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
863 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
864 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
865 from = build_ptrmemfunc_type (build_pointer_type (from));
866 conv = build_conv (PMEM_CONV, from, conv);
868 else if (tcode == BOOLEAN_TYPE)
870 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
871 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
874 conv = build_conv (STD_CONV, to, conv);
875 if (fcode == POINTER_TYPE
876 || (TYPE_PTRMEMFUNC_P (from) && ICS_STD_RANK (conv) < PBOOL_RANK))
877 ICS_STD_RANK (conv) = PBOOL_RANK;
879 /* We don't check for ENUMERAL_TYPE here because there are no standard
880 conversions to enum type. */
881 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
882 || tcode == REAL_TYPE)
884 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
886 conv = build_conv (STD_CONV, to, conv);
888 /* Give this a better rank if it's a promotion. */
889 if (to == type_promotes_to (from)
890 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
891 ICS_STD_RANK (conv) = PROMO_RANK;
893 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
894 && is_properly_derived_from (from, to))
896 if (TREE_CODE (conv) == RVALUE_CONV)
897 conv = TREE_OPERAND (conv, 0);
898 conv = build_conv (BASE_CONV, to, conv);
899 /* The derived-to-base conversion indicates the initialization
900 of a parameter with base type from an object of a derived
901 type. A temporary object is created to hold the result of
903 NEED_TEMPORARY_P (conv) = 1;
911 /* Returns nonzero if T1 is reference-related to T2. */
914 reference_related_p (tree t1, tree t2)
916 t1 = TYPE_MAIN_VARIANT (t1);
917 t2 = TYPE_MAIN_VARIANT (t2);
921 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
922 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
924 return (same_type_p (t1, t2)
925 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
926 && DERIVED_FROM_P (t1, t2)));
929 /* Returns nonzero if T1 is reference-compatible with T2. */
932 reference_compatible_p (tree t1, tree t2)
936 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
937 reference-related to T2 and cv1 is the same cv-qualification as,
938 or greater cv-qualification than, cv2. */
939 return (reference_related_p (t1, t2)
940 && at_least_as_qualified_p (t1, t2));
943 /* Determine whether or not the EXPR (of class type S) can be
944 converted to T as in [over.match.ref]. */
947 convert_class_to_reference (tree t, tree s, tree expr)
952 struct z_candidate *candidates;
953 struct z_candidate *cand;
957 Assuming that "cv1 T" is the underlying type of the reference
958 being initialized, and "cv S" is the type of the initializer
959 expression, with S a class type, the candidate functions are
962 --The conversion functions of S and its base classes are
963 considered. Those that are not hidden within S and yield type
964 "reference to cv2 T2", where "cv1 T" is reference-compatible
965 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
967 The argument list has one argument, which is the initializer
972 /* Conceptually, we should take the address of EXPR and put it in
973 the argument list. Unfortunately, however, that can result in
974 error messages, which we should not issue now because we are just
975 trying to find a conversion operator. Therefore, we use NULL,
976 cast to the appropriate type. */
977 arglist = build_int_2 (0, 0);
978 TREE_TYPE (arglist) = build_pointer_type (s);
979 arglist = build_tree_list (NULL_TREE, arglist);
981 for (conversions = lookup_conversions (s);
983 conversions = TREE_CHAIN (conversions))
985 tree fns = TREE_VALUE (conversions);
987 for (; fns; fns = OVL_NEXT (fns))
989 tree f = OVL_CURRENT (fns);
990 tree t2 = TREE_TYPE (TREE_TYPE (f));
991 struct z_candidate *old_candidates = candidates;
993 /* If this is a template function, try to get an exact
995 if (TREE_CODE (f) == TEMPLATE_DECL)
998 = add_template_candidate (candidates,
1002 build_reference_type (t),
1004 TREE_PURPOSE (conversions),
1008 if (candidates != old_candidates)
1010 /* Now, see if the conversion function really returns
1011 an lvalue of the appropriate type. From the
1012 point of view of unification, simply returning an
1013 rvalue of the right type is good enough. */
1015 t2 = TREE_TYPE (TREE_TYPE (f));
1016 if (TREE_CODE (t2) != REFERENCE_TYPE
1017 || !reference_compatible_p (t, TREE_TYPE (t2)))
1018 candidates = candidates->next;
1021 else if (TREE_CODE (t2) == REFERENCE_TYPE
1022 && reference_compatible_p (t, TREE_TYPE (t2)))
1024 = add_function_candidate (candidates, f, s, arglist,
1026 TREE_PURPOSE (conversions),
1031 /* If none of the conversion functions worked out, let our caller
1033 if (!any_viable (candidates))
1036 candidates = splice_viable (candidates);
1037 cand = tourney (candidates);
1041 /* Now that we know that this is the function we're going to use fix
1042 the dummy first argument. */
1043 cand->args = tree_cons (NULL_TREE,
1045 TREE_CHAIN (cand->args));
1047 conv = build1 (IDENTITY_CONV, s, expr);
1048 conv = build_conv (USER_CONV, TREE_TYPE (TREE_TYPE (cand->fn)),
1050 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
1051 ICS_USER_FLAG (conv) = 1;
1052 if (cand->viable == -1)
1053 ICS_BAD_FLAG (conv) = 1;
1054 cand->second_conv = conv;
1059 /* A reference of the indicated TYPE is being bound directly to the
1060 expression represented by the implicit conversion sequence CONV.
1061 Return a conversion sequence for this binding. */
1064 direct_reference_binding (tree type, tree conv)
1066 tree t = TREE_TYPE (type);
1070 When a parameter of reference type binds directly
1071 (_dcl.init.ref_) to an argument expression, the implicit
1072 conversion sequence is the identity conversion, unless the
1073 argument expression has a type that is a derived class of the
1074 parameter type, in which case the implicit conversion sequence is
1075 a derived-to-base Conversion.
1077 If the parameter binds directly to the result of applying a
1078 conversion function to the argument expression, the implicit
1079 conversion sequence is a user-defined conversion sequence
1080 (_over.ics.user_), with the second standard conversion sequence
1081 either an identity conversion or, if the conversion function
1082 returns an entity of a type that is a derived class of the
1083 parameter type, a derived-to-base conversion. */
1084 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
1086 /* Represent the derived-to-base conversion. */
1087 conv = build_conv (BASE_CONV, t, conv);
1088 /* We will actually be binding to the base-class subobject in
1089 the derived class, so we mark this conversion appropriately.
1090 That way, convert_like knows not to generate a temporary. */
1091 NEED_TEMPORARY_P (conv) = 0;
1093 return build_conv (REF_BIND, type, conv);
1096 /* Returns the conversion path from type FROM to reference type TO for
1097 purposes of reference binding. For lvalue binding, either pass a
1098 reference type to FROM or an lvalue expression to EXPR. If the
1099 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1100 the conversion returned. */
1103 reference_binding (tree rto, tree rfrom, tree expr, int flags)
1105 tree conv = NULL_TREE;
1106 tree to = TREE_TYPE (rto);
1110 cp_lvalue_kind lvalue_p = clk_none;
1112 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1114 expr = instantiate_type (to, expr, tf_none);
1115 if (expr == error_mark_node)
1117 from = TREE_TYPE (expr);
1120 if (TREE_CODE (from) == REFERENCE_TYPE)
1122 /* Anything with reference type is an lvalue. */
1123 lvalue_p = clk_ordinary;
1124 from = TREE_TYPE (from);
1127 lvalue_p = real_lvalue_p (expr);
1129 /* Figure out whether or not the types are reference-related and
1130 reference compatible. We have do do this after stripping
1131 references from FROM. */
1132 related_p = reference_related_p (to, from);
1133 compatible_p = reference_compatible_p (to, from);
1135 if (lvalue_p && compatible_p)
1139 If the initializer expression
1141 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1142 is reference-compatible with "cv2 T2,"
1144 the reference is bound directly to the initializer exprssion
1146 conv = build1 (IDENTITY_CONV, from, expr);
1147 conv = direct_reference_binding (rto, conv);
1148 if ((lvalue_p & clk_bitfield) != 0
1149 && CP_TYPE_CONST_NON_VOLATILE_P (to))
1150 /* For the purposes of overload resolution, we ignore the fact
1151 this expression is a bitfield. (In particular,
1152 [over.ics.ref] says specifically that a function with a
1153 non-const reference parameter is viable even if the
1154 argument is a bitfield.)
1156 However, when we actually call the function we must create
1157 a temporary to which to bind the reference. If the
1158 reference is volatile, or isn't const, then we cannot make
1159 a temporary, so we just issue an error when the conversion
1161 NEED_TEMPORARY_P (conv) = 1;
1164 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1168 If the initializer exprsesion
1170 -- has a class type (i.e., T2 is a class type) can be
1171 implicitly converted to an lvalue of type "cv3 T3," where
1172 "cv1 T1" is reference-compatible with "cv3 T3". (this
1173 conversion is selected by enumerating the applicable
1174 conversion functions (_over.match.ref_) and choosing the
1175 best one through overload resolution. (_over.match_).
1177 the reference is bound to the lvalue result of the conversion
1178 in the second case. */
1179 conv = convert_class_to_reference (to, from, expr);
1181 return direct_reference_binding (rto, conv);
1184 /* From this point on, we conceptually need temporaries, even if we
1185 elide them. Only the cases above are "direct bindings". */
1186 if (flags & LOOKUP_NO_TEMP_BIND)
1191 When a parameter of reference type is not bound directly to an
1192 argument expression, the conversion sequence is the one required
1193 to convert the argument expression to the underlying type of the
1194 reference according to _over.best.ics_. Conceptually, this
1195 conversion sequence corresponds to copy-initializing a temporary
1196 of the underlying type with the argument expression. Any
1197 difference in top-level cv-qualification is subsumed by the
1198 initialization itself and does not constitute a conversion. */
1202 Otherwise, the reference shall be to a non-volatile const type. */
1203 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1208 If the initializer expression is an rvalue, with T2 a class type,
1209 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1210 is bound in one of the following ways:
1212 -- The reference is bound to the object represented by the rvalue
1213 or to a sub-object within that object.
1215 In this case, the implicit conversion sequence is supposed to be
1216 same as we would obtain by generating a temporary. Fortunately,
1217 if the types are reference compatible, then this is either an
1218 identity conversion or the derived-to-base conversion, just as
1219 for direct binding. */
1220 if (CLASS_TYPE_P (from) && compatible_p)
1222 conv = build1 (IDENTITY_CONV, from, expr);
1223 return direct_reference_binding (rto, conv);
1228 Otherwise, a temporary of type "cv1 T1" is created and
1229 initialized from the initializer expression using the rules for a
1230 non-reference copy initialization. If T1 is reference-related to
1231 T2, cv1 must be the same cv-qualification as, or greater
1232 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1233 if (related_p && !at_least_as_qualified_p (to, from))
1236 conv = implicit_conversion (to, from, expr, flags);
1240 conv = build_conv (REF_BIND, rto, conv);
1241 /* This reference binding, unlike those above, requires the
1242 creation of a temporary. */
1243 NEED_TEMPORARY_P (conv) = 1;
1248 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1249 to type TO. The optional expression EXPR may affect the conversion.
1250 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1254 implicit_conversion (tree to, tree from, tree expr, int flags)
1257 struct z_candidate *cand;
1259 /* Resolve expressions like `A::p' that we thought might become
1260 pointers-to-members. */
1261 if (expr && TREE_CODE (expr) == OFFSET_REF)
1263 expr = resolve_offset_ref (expr);
1264 from = TREE_TYPE (expr);
1267 if (from == error_mark_node || to == error_mark_node
1268 || expr == error_mark_node)
1271 /* Make sure both the FROM and TO types are complete so that
1272 user-defined conversions are available. */
1273 complete_type (from);
1276 if (TREE_CODE (to) == REFERENCE_TYPE)
1277 conv = reference_binding (to, from, expr, flags);
1279 conv = standard_conversion (to, from, expr);
1284 if (expr != NULL_TREE
1285 && (IS_AGGR_TYPE (from)
1286 || IS_AGGR_TYPE (to))
1287 && (flags & LOOKUP_NO_CONVERSION) == 0)
1289 cand = build_user_type_conversion_1
1290 (to, expr, LOOKUP_ONLYCONVERTING);
1292 conv = cand->second_conv;
1294 /* We used to try to bind a reference to a temporary here, but that
1295 is now handled by the recursive call to this function at the end
1296 of reference_binding. */
1303 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1306 static struct z_candidate *
1307 add_candidate (struct z_candidate *candidates,
1308 tree fn, tree args, tree convs, tree access_path,
1309 tree conversion_path, int viable)
1311 struct z_candidate *cand
1312 = (struct z_candidate *) ggc_alloc_cleared (sizeof (struct z_candidate));
1316 cand->convs = convs;
1317 cand->access_path = access_path;
1318 cand->conversion_path = conversion_path;
1319 cand->viable = viable;
1320 cand->next = candidates;
1325 /* Create an overload candidate for the function or method FN called with
1326 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1327 to implicit_conversion.
1329 CTYPE, if non-NULL, is the type we want to pretend this function
1330 comes from for purposes of overload resolution. */
1332 static struct z_candidate *
1333 add_function_candidate (struct z_candidate *candidates,
1334 tree fn, tree ctype, tree arglist,
1335 tree access_path, tree conversion_path,
1338 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1341 tree parmnode, argnode;
1345 /* The `this', `in_chrg' and VTT arguments to constructors are not
1346 considered in overload resolution. */
1347 if (DECL_CONSTRUCTOR_P (fn))
1349 parmlist = skip_artificial_parms_for (fn, parmlist);
1350 orig_arglist = arglist;
1351 arglist = skip_artificial_parms_for (fn, arglist);
1354 orig_arglist = arglist;
1356 len = list_length (arglist);
1357 convs = make_tree_vec (len);
1359 /* 13.3.2 - Viable functions [over.match.viable]
1360 First, to be a viable function, a candidate function shall have enough
1361 parameters to agree in number with the arguments in the list.
1363 We need to check this first; otherwise, checking the ICSes might cause
1364 us to produce an ill-formed template instantiation. */
1366 parmnode = parmlist;
1367 for (i = 0; i < len; ++i)
1369 if (parmnode == NULL_TREE || parmnode == void_list_node)
1371 parmnode = TREE_CHAIN (parmnode);
1374 if (i < len && parmnode)
1377 /* Make sure there are default args for the rest of the parms. */
1378 else if (!sufficient_parms_p (parmnode))
1384 /* Second, for F to be a viable function, there shall exist for each
1385 argument an implicit conversion sequence that converts that argument
1386 to the corresponding parameter of F. */
1388 parmnode = parmlist;
1391 for (i = 0; i < len; ++i)
1393 tree arg = TREE_VALUE (argnode);
1394 tree argtype = lvalue_type (arg);
1398 if (parmnode == void_list_node)
1401 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1402 && ! DECL_CONSTRUCTOR_P (fn));
1406 tree parmtype = TREE_VALUE (parmnode);
1408 /* The type of the implicit object parameter ('this') for
1409 overload resolution is not always the same as for the
1410 function itself; conversion functions are considered to
1411 be members of the class being converted, and functions
1412 introduced by a using-declaration are considered to be
1413 members of the class that uses them.
1415 Since build_over_call ignores the ICS for the `this'
1416 parameter, we can just change the parm type. */
1417 if (ctype && is_this)
1420 = build_qualified_type (ctype,
1421 TYPE_QUALS (TREE_TYPE (parmtype)));
1422 parmtype = build_pointer_type (parmtype);
1425 t = implicit_conversion (parmtype, argtype, arg, flags);
1429 t = build1 (IDENTITY_CONV, argtype, arg);
1430 ICS_ELLIPSIS_FLAG (t) = 1;
1434 ICS_THIS_FLAG (t) = 1;
1436 TREE_VEC_ELT (convs, i) = t;
1443 if (ICS_BAD_FLAG (t))
1447 parmnode = TREE_CHAIN (parmnode);
1448 argnode = TREE_CHAIN (argnode);
1452 return add_candidate (candidates, fn, orig_arglist, convs, access_path,
1453 conversion_path, viable);
1456 /* Create an overload candidate for the conversion function FN which will
1457 be invoked for expression OBJ, producing a pointer-to-function which
1458 will in turn be called with the argument list ARGLIST, and add it to
1459 CANDIDATES. FLAGS is passed on to implicit_conversion.
1461 Actually, we don't really care about FN; we care about the type it
1462 converts to. There may be multiple conversion functions that will
1463 convert to that type, and we rely on build_user_type_conversion_1 to
1464 choose the best one; so when we create our candidate, we record the type
1465 instead of the function. */
1467 static struct z_candidate *
1468 add_conv_candidate (struct z_candidate *candidates, tree fn, tree obj,
1469 tree arglist, tree access_path, tree conversion_path)
1471 tree totype = TREE_TYPE (TREE_TYPE (fn));
1472 int i, len, viable, flags;
1473 tree parmlist, convs, parmnode, argnode;
1475 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1476 parmlist = TREE_TYPE (parmlist);
1477 parmlist = TYPE_ARG_TYPES (parmlist);
1479 len = list_length (arglist) + 1;
1480 convs = make_tree_vec (len);
1481 parmnode = parmlist;
1484 flags = LOOKUP_NORMAL;
1486 /* Don't bother looking up the same type twice. */
1487 if (candidates && candidates->fn == totype)
1490 for (i = 0; i < len; ++i)
1492 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1493 tree argtype = lvalue_type (arg);
1497 t = implicit_conversion (totype, argtype, arg, flags);
1498 else if (parmnode == void_list_node)
1501 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1504 t = build1 (IDENTITY_CONV, argtype, arg);
1505 ICS_ELLIPSIS_FLAG (t) = 1;
1508 TREE_VEC_ELT (convs, i) = t;
1512 if (ICS_BAD_FLAG (t))
1519 parmnode = TREE_CHAIN (parmnode);
1520 argnode = TREE_CHAIN (argnode);
1526 if (!sufficient_parms_p (parmnode))
1529 return add_candidate (candidates, totype, arglist, convs, access_path,
1530 conversion_path, viable);
1533 static struct z_candidate *
1534 build_builtin_candidate (struct z_candidate *candidates, tree fnname,
1535 tree type1, tree type2, tree *args, tree *argtypes,
1545 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1547 for (i = 0; i < 2; ++i)
1552 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1556 /* We need something for printing the candidate. */
1557 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1559 else if (ICS_BAD_FLAG (t))
1561 TREE_VEC_ELT (convs, i) = t;
1564 /* For COND_EXPR we rearranged the arguments; undo that now. */
1567 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1568 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1569 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1571 TREE_VEC_ELT (convs, 0) = t;
1576 return add_candidate (candidates, fnname, /*args=*/NULL_TREE, convs,
1577 /*access_path=*/NULL_TREE,
1578 /*conversion_path=*/NULL_TREE,
1583 is_complete (tree t)
1585 return COMPLETE_TYPE_P (complete_type (t));
1588 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1591 promoted_arithmetic_type_p (tree type)
1595 In this section, the term promoted integral type is used to refer
1596 to those integral types which are preserved by integral promotion
1597 (including e.g. int and long but excluding e.g. char).
1598 Similarly, the term promoted arithmetic type refers to promoted
1599 integral types plus floating types. */
1600 return ((INTEGRAL_TYPE_P (type)
1601 && same_type_p (type_promotes_to (type), type))
1602 || TREE_CODE (type) == REAL_TYPE);
1605 /* Create any builtin operator overload candidates for the operator in
1606 question given the converted operand types TYPE1 and TYPE2. The other
1607 args are passed through from add_builtin_candidates to
1608 build_builtin_candidate.
1610 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1611 If CODE is requires candidates operands of the same type of the kind
1612 of which TYPE1 and TYPE2 are, we add both candidates
1613 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1615 static struct z_candidate *
1616 add_builtin_candidate (struct z_candidate *candidates, enum tree_code code,
1617 enum tree_code code2, tree fnname, tree type1,
1618 tree type2, tree *args, tree *argtypes, int flags)
1622 case POSTINCREMENT_EXPR:
1623 case POSTDECREMENT_EXPR:
1624 args[1] = integer_zero_node;
1625 type2 = integer_type_node;
1634 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1635 and VQ is either volatile or empty, there exist candidate operator
1636 functions of the form
1637 VQ T& operator++(VQ T&);
1638 T operator++(VQ T&, int);
1639 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1640 type other than bool, and VQ is either volatile or empty, there exist
1641 candidate operator functions of the form
1642 VQ T& operator--(VQ T&);
1643 T operator--(VQ T&, int);
1644 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1645 complete object type, and VQ is either volatile or empty, there exist
1646 candidate operator functions of the form
1647 T*VQ& operator++(T*VQ&);
1648 T*VQ& operator--(T*VQ&);
1649 T* operator++(T*VQ&, int);
1650 T* operator--(T*VQ&, int); */
1652 case POSTDECREMENT_EXPR:
1653 case PREDECREMENT_EXPR:
1654 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1656 case POSTINCREMENT_EXPR:
1657 case PREINCREMENT_EXPR:
1658 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1660 type1 = build_reference_type (type1);
1665 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1666 exist candidate operator functions of the form
1670 8 For every function type T, there exist candidate operator functions of
1672 T& operator*(T*); */
1675 if (TREE_CODE (type1) == POINTER_TYPE
1676 && (TYPE_PTROB_P (type1)
1677 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1681 /* 9 For every type T, there exist candidate operator functions of the form
1684 10For every promoted arithmetic type T, there exist candidate operator
1685 functions of the form
1689 case CONVERT_EXPR: /* unary + */
1690 if (TREE_CODE (type1) == POINTER_TYPE
1691 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
1694 if (ARITHMETIC_TYPE_P (type1))
1698 /* 11For every promoted integral type T, there exist candidate operator
1699 functions of the form
1703 if (INTEGRAL_TYPE_P (type1))
1707 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1708 is the same type as C2 or is a derived class of C2, T is a complete
1709 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1710 there exist candidate operator functions of the form
1711 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1712 where CV12 is the union of CV1 and CV2. */
1715 if (TREE_CODE (type1) == POINTER_TYPE
1716 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
1718 tree c1 = TREE_TYPE (type1);
1719 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
1720 ? TYPE_METHOD_BASETYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2)))
1721 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
1723 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1724 && (TYPE_PTRMEMFUNC_P (type2)
1725 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1730 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1731 didate operator functions of the form
1736 bool operator<(L, R);
1737 bool operator>(L, R);
1738 bool operator<=(L, R);
1739 bool operator>=(L, R);
1740 bool operator==(L, R);
1741 bool operator!=(L, R);
1742 where LR is the result of the usual arithmetic conversions between
1745 14For every pair of types T and I, where T is a cv-qualified or cv-
1746 unqualified complete object type and I is a promoted integral type,
1747 there exist candidate operator functions of the form
1748 T* operator+(T*, I);
1749 T& operator[](T*, I);
1750 T* operator-(T*, I);
1751 T* operator+(I, T*);
1752 T& operator[](I, T*);
1754 15For every T, where T is a pointer to complete object type, there exist
1755 candidate operator functions of the form112)
1756 ptrdiff_t operator-(T, T);
1758 16For every pointer or enumeration type T, there exist candidate operator
1759 functions of the form
1760 bool operator<(T, T);
1761 bool operator>(T, T);
1762 bool operator<=(T, T);
1763 bool operator>=(T, T);
1764 bool operator==(T, T);
1765 bool operator!=(T, T);
1767 17For every pointer to member type T, there exist candidate operator
1768 functions of the form
1769 bool operator==(T, T);
1770 bool operator!=(T, T); */
1773 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1775 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1777 type2 = ptrdiff_type_node;
1781 case TRUNC_DIV_EXPR:
1782 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1788 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1789 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1791 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
1792 && null_ptr_cst_p (args[1]))
1797 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
1798 && null_ptr_cst_p (args[0]))
1810 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1812 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1814 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1816 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1821 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1829 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1832 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1834 type1 = ptrdiff_type_node;
1837 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1839 type2 = ptrdiff_type_node;
1844 /* 18For every pair of promoted integral types L and R, there exist candi-
1845 date operator functions of the form
1852 where LR is the result of the usual arithmetic conversions between
1855 case TRUNC_MOD_EXPR:
1861 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1865 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1866 type, VQ is either volatile or empty, and R is a promoted arithmetic
1867 type, there exist candidate operator functions of the form
1868 VQ L& operator=(VQ L&, R);
1869 VQ L& operator*=(VQ L&, R);
1870 VQ L& operator/=(VQ L&, R);
1871 VQ L& operator+=(VQ L&, R);
1872 VQ L& operator-=(VQ L&, R);
1874 20For every pair T, VQ), where T is any type and VQ is either volatile
1875 or empty, there exist candidate operator functions of the form
1876 T*VQ& operator=(T*VQ&, T*);
1878 21For every pair T, VQ), where T is a pointer to member type and VQ is
1879 either volatile or empty, there exist candidate operator functions of
1881 VQ T& operator=(VQ T&, T);
1883 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1884 unqualified complete object type, VQ is either volatile or empty, and
1885 I is a promoted integral type, there exist candidate operator func-
1887 T*VQ& operator+=(T*VQ&, I);
1888 T*VQ& operator-=(T*VQ&, I);
1890 23For every triple L, VQ, R), where L is an integral or enumeration
1891 type, VQ is either volatile or empty, and R is a promoted integral
1892 type, there exist candidate operator functions of the form
1894 VQ L& operator%=(VQ L&, R);
1895 VQ L& operator<<=(VQ L&, R);
1896 VQ L& operator>>=(VQ L&, R);
1897 VQ L& operator&=(VQ L&, R);
1898 VQ L& operator^=(VQ L&, R);
1899 VQ L& operator|=(VQ L&, R); */
1906 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1908 type2 = ptrdiff_type_node;
1912 case TRUNC_DIV_EXPR:
1913 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1917 case TRUNC_MOD_EXPR:
1923 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1928 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1930 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1931 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1932 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1933 || ((TYPE_PTRMEMFUNC_P (type1)
1934 || TREE_CODE (type1) == POINTER_TYPE)
1935 && null_ptr_cst_p (args[1])))
1945 type1 = build_reference_type (type1);
1951 For every pair of promoted arithmetic types L and R, there
1952 exist candidate operator functions of the form
1954 LR operator?(bool, L, R);
1956 where LR is the result of the usual arithmetic conversions
1957 between types L and R.
1959 For every type T, where T is a pointer or pointer-to-member
1960 type, there exist candidate operator functions of the form T
1961 operator?(bool, T, T); */
1963 if (promoted_arithmetic_type_p (type1)
1964 && promoted_arithmetic_type_p (type2))
1968 /* Otherwise, the types should be pointers. */
1969 if (!(TREE_CODE (type1) == POINTER_TYPE
1970 || TYPE_PTRMEM_P (type1)
1971 || TYPE_PTRMEMFUNC_P (type1))
1972 || !(TREE_CODE (type2) == POINTER_TYPE
1973 || TYPE_PTRMEM_P (type2)
1974 || TYPE_PTRMEMFUNC_P (type2)))
1977 /* We don't check that the two types are the same; the logic
1978 below will actually create two candidates; one in which both
1979 parameter types are TYPE1, and one in which both parameter
1983 /* These arguments do not make for a valid overloaded operator. */
1990 /* If we're dealing with two pointer types or two enumeral types,
1991 we need candidates for both of them. */
1992 if (type2 && !same_type_p (type1, type2)
1993 && TREE_CODE (type1) == TREE_CODE (type2)
1994 && (TREE_CODE (type1) == REFERENCE_TYPE
1995 || (TREE_CODE (type1) == POINTER_TYPE
1996 && TYPE_PTRMEM_P (type1) == TYPE_PTRMEM_P (type2))
1997 || TYPE_PTRMEMFUNC_P (type1)
1998 || IS_AGGR_TYPE (type1)
1999 || TREE_CODE (type1) == ENUMERAL_TYPE))
2001 candidates = build_builtin_candidate
2002 (candidates, fnname, type1, type1, args, argtypes, flags);
2003 return build_builtin_candidate
2004 (candidates, fnname, type2, type2, args, argtypes, flags);
2007 return build_builtin_candidate
2008 (candidates, fnname, type1, type2, args, argtypes, flags);
2012 type_decays_to (tree type)
2014 if (TREE_CODE (type) == ARRAY_TYPE)
2015 return build_pointer_type (TREE_TYPE (type));
2016 if (TREE_CODE (type) == FUNCTION_TYPE)
2017 return build_pointer_type (type);
2021 /* There are three conditions of builtin candidates:
2023 1) bool-taking candidates. These are the same regardless of the input.
2024 2) pointer-pair taking candidates. These are generated for each type
2025 one of the input types converts to.
2026 3) arithmetic candidates. According to the standard, we should generate
2027 all of these, but I'm trying not to...
2029 Here we generate a superset of the possible candidates for this particular
2030 case. That is a subset of the full set the standard defines, plus some
2031 other cases which the standard disallows. add_builtin_candidate will
2032 filter out the invalid set. */
2034 static struct z_candidate *
2035 add_builtin_candidates (struct z_candidate *candidates, enum tree_code code,
2036 enum tree_code code2, tree fnname, tree *args,
2041 tree type, argtypes[3];
2042 /* TYPES[i] is the set of possible builtin-operator parameter types
2043 we will consider for the Ith argument. These are represented as
2044 a TREE_LIST; the TREE_VALUE of each node is the potential
2048 for (i = 0; i < 3; ++i)
2051 argtypes[i] = lvalue_type (args[i]);
2053 argtypes[i] = NULL_TREE;
2058 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2059 and VQ is either volatile or empty, there exist candidate operator
2060 functions of the form
2061 VQ T& operator++(VQ T&); */
2063 case POSTINCREMENT_EXPR:
2064 case PREINCREMENT_EXPR:
2065 case POSTDECREMENT_EXPR:
2066 case PREDECREMENT_EXPR:
2071 /* 24There also exist candidate operator functions of the form
2072 bool operator!(bool);
2073 bool operator&&(bool, bool);
2074 bool operator||(bool, bool); */
2076 case TRUTH_NOT_EXPR:
2077 return build_builtin_candidate
2078 (candidates, fnname, boolean_type_node,
2079 NULL_TREE, args, argtypes, flags);
2081 case TRUTH_ORIF_EXPR:
2082 case TRUTH_ANDIF_EXPR:
2083 return build_builtin_candidate
2084 (candidates, fnname, boolean_type_node,
2085 boolean_type_node, args, argtypes, flags);
2106 types[0] = types[1] = NULL_TREE;
2108 for (i = 0; i < 2; ++i)
2112 else if (IS_AGGR_TYPE (argtypes[i]))
2116 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2119 convs = lookup_conversions (argtypes[i]);
2121 if (code == COND_EXPR)
2123 if (real_lvalue_p (args[i]))
2124 types[i] = tree_cons
2125 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2127 types[i] = tree_cons
2128 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2134 for (; convs; convs = TREE_CHAIN (convs))
2136 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
2139 && (TREE_CODE (type) != REFERENCE_TYPE
2140 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2143 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2144 types[i] = tree_cons (NULL_TREE, type, types[i]);
2146 type = non_reference (type);
2147 if (i != 0 || ! ref1)
2149 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2150 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2151 types[i] = tree_cons (NULL_TREE, type, types[i]);
2152 if (INTEGRAL_TYPE_P (type))
2153 type = type_promotes_to (type);
2156 if (! value_member (type, types[i]))
2157 types[i] = tree_cons (NULL_TREE, type, types[i]);
2162 if (code == COND_EXPR && real_lvalue_p (args[i]))
2163 types[i] = tree_cons
2164 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2165 type = non_reference (argtypes[i]);
2166 if (i != 0 || ! ref1)
2168 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2169 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2170 types[i] = tree_cons (NULL_TREE, type, types[i]);
2171 if (INTEGRAL_TYPE_P (type))
2172 type = type_promotes_to (type);
2174 types[i] = tree_cons (NULL_TREE, type, types[i]);
2178 /* Run through the possible parameter types of both arguments,
2179 creating candidates with those parameter types. */
2180 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2183 for (type = types[1]; type; type = TREE_CHAIN (type))
2184 candidates = add_builtin_candidate
2185 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2186 TREE_VALUE (type), args, argtypes, flags);
2188 candidates = add_builtin_candidate
2189 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2190 NULL_TREE, args, argtypes, flags);
2197 /* If TMPL can be successfully instantiated as indicated by
2198 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2200 TMPL is the template. EXPLICIT_TARGS are any explicit template
2201 arguments. ARGLIST is the arguments provided at the call-site.
2202 The RETURN_TYPE is the desired type for conversion operators. If
2203 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2204 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2205 add_conv_candidate. */
2207 static struct z_candidate*
2208 add_template_candidate_real (struct z_candidate *candidates, tree tmpl,
2209 tree ctype, tree explicit_targs, tree arglist,
2210 tree return_type, tree access_path,
2211 tree conversion_path, int flags, tree obj,
2212 unification_kind_t strict)
2214 int ntparms = DECL_NTPARMS (tmpl);
2215 tree targs = make_tree_vec (ntparms);
2216 tree args_without_in_chrg = arglist;
2217 struct z_candidate *cand;
2221 /* We don't do deduction on the in-charge parameter, the VTT
2222 parameter or 'this'. */
2223 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2224 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2226 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2227 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2228 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2229 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2231 i = fn_type_unification (tmpl, explicit_targs, targs,
2232 args_without_in_chrg,
2233 return_type, strict, -1);
2238 fn = instantiate_template (tmpl, targs);
2239 if (fn == error_mark_node)
2244 A member function template is never instantiated to perform the
2245 copy of a class object to an object of its class type.
2247 It's a little unclear what this means; the standard explicitly
2248 does allow a template to be used to copy a class. For example,
2253 template <class T> A(const T&);
2256 void g () { A a (f ()); }
2258 the member template will be used to make the copy. The section
2259 quoted above appears in the paragraph that forbids constructors
2260 whose only parameter is (a possibly cv-qualified variant of) the
2261 class type, and a logical interpretation is that the intent was
2262 to forbid the instantiation of member templates which would then
2264 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2266 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2267 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2272 if (obj != NULL_TREE)
2273 /* Aha, this is a conversion function. */
2274 cand = add_conv_candidate (candidates, fn, obj, access_path,
2275 conversion_path, arglist);
2277 cand = add_function_candidate (candidates, fn, ctype,
2278 arglist, access_path,
2279 conversion_path, flags);
2280 if (DECL_TI_TEMPLATE (fn) != tmpl)
2281 /* This situation can occur if a member template of a template
2282 class is specialized. Then, instantiate_template might return
2283 an instantiation of the specialization, in which case the
2284 DECL_TI_TEMPLATE field will point at the original
2285 specialization. For example:
2287 template <class T> struct S { template <class U> void f(U);
2288 template <> void f(int) {}; };
2292 Here, TMPL will be template <class U> S<double>::f(U).
2293 And, instantiate template will give us the specialization
2294 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2295 for this will point at template <class T> template <> S<T>::f(int),
2296 so that we can find the definition. For the purposes of
2297 overload resolution, however, we want the original TMPL. */
2298 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2300 cand->template = DECL_TEMPLATE_INFO (fn);
2306 static struct z_candidate *
2307 add_template_candidate (struct z_candidate *candidates, tree tmpl, tree ctype,
2308 tree explicit_targs, tree arglist, tree return_type,
2309 tree access_path, tree conversion_path, int flags,
2310 unification_kind_t strict)
2313 add_template_candidate_real (candidates, tmpl, ctype,
2314 explicit_targs, arglist, return_type,
2315 access_path, conversion_path,
2316 flags, NULL_TREE, strict);
2320 static struct z_candidate *
2321 add_template_conv_candidate (struct z_candidate *candidates, tree tmpl,
2322 tree obj, tree arglist, tree return_type,
2323 tree access_path, tree conversion_path)
2326 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2327 arglist, return_type, access_path,
2328 conversion_path, 0, obj, DEDUCE_CONV);
2333 any_viable (struct z_candidate *cands)
2335 for (; cands; cands = cands->next)
2336 if (pedantic ? cands->viable == 1 : cands->viable)
2342 any_strictly_viable (struct z_candidate *cands)
2344 for (; cands; cands = cands->next)
2345 if (cands->viable == 1)
2350 static struct z_candidate *
2351 splice_viable (struct z_candidate *cands)
2353 struct z_candidate **p = &cands;
2357 if (pedantic ? (*p)->viable == 1 : (*p)->viable)
2367 build_this (tree obj)
2369 /* Fix this to work on non-lvalues. */
2370 return build_unary_op (ADDR_EXPR, obj, 0);
2374 print_z_candidates (struct z_candidate *candidates)
2376 const char *str = "candidates are:";
2377 for (; candidates; candidates = candidates->next)
2379 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
2381 if (TREE_VEC_LENGTH (candidates->convs) == 3)
2382 error ("%s %D(%T, %T, %T) <built-in>", str, candidates->fn,
2383 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2384 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
2385 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
2386 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
2387 error ("%s %D(%T, %T) <built-in>", str, candidates->fn,
2388 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
2389 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
2391 error ("%s %D(%T) <built-in>", str, candidates->fn,
2392 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
2394 else if (TYPE_P (candidates->fn))
2395 error ("%s %T <conversion>", str, candidates->fn);
2397 cp_error_at ("%s %+#D%s", str, candidates->fn,
2398 candidates->viable == -1 ? " <near match>" : "");
2403 /* Returns the best overload candidate to perform the requested
2404 conversion. This function is used for three the overloading situations
2405 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2406 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2407 per [dcl.init.ref], so we ignore temporary bindings. */
2409 static struct z_candidate *
2410 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2412 struct z_candidate *candidates, *cand;
2413 tree fromtype = TREE_TYPE (expr);
2414 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
2415 tree args = NULL_TREE;
2417 /* We represent conversion within a hierarchy using RVALUE_CONV and
2418 BASE_CONV, as specified by [over.best.ics]; these become plain
2419 constructor calls, as specified in [dcl.init]. */
2420 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2421 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2423 if (IS_AGGR_TYPE (totype))
2424 ctors = lookup_fnfields (TYPE_BINFO (totype),
2425 complete_ctor_identifier,
2428 if (IS_AGGR_TYPE (fromtype))
2429 convs = lookup_conversions (fromtype);
2432 flags |= LOOKUP_NO_CONVERSION;
2438 ctors = BASELINK_FUNCTIONS (ctors);
2440 t = build_int_2 (0, 0);
2441 TREE_TYPE (t) = build_pointer_type (totype);
2442 args = build_tree_list (NULL_TREE, expr);
2443 /* We should never try to call the abstract or base constructor
2445 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2446 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2448 args = tree_cons (NULL_TREE, t, args);
2450 for (; ctors; ctors = OVL_NEXT (ctors))
2452 tree ctor = OVL_CURRENT (ctors);
2453 if (DECL_NONCONVERTING_P (ctor))
2456 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2458 add_template_candidate (candidates, ctor, totype,
2459 NULL_TREE, args, NULL_TREE,
2460 TYPE_BINFO (totype),
2461 TYPE_BINFO (totype),
2465 candidates = add_function_candidate (candidates, ctor, totype,
2466 args, TYPE_BINFO (totype),
2467 TYPE_BINFO (totype),
2471 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2475 args = build_tree_list (NULL_TREE, build_this (expr));
2477 for (; convs; convs = TREE_CHAIN (convs))
2480 tree conversion_path = TREE_PURPOSE (convs);
2481 int convflags = LOOKUP_NO_CONVERSION;
2483 /* If we are called to convert to a reference type, we are trying to
2484 find an lvalue binding, so don't even consider temporaries. If
2485 we don't find an lvalue binding, the caller will try again to
2486 look for a temporary binding. */
2487 if (TREE_CODE (totype) == REFERENCE_TYPE)
2488 convflags |= LOOKUP_NO_TEMP_BIND;
2490 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2492 tree fn = OVL_CURRENT (fns);
2493 struct z_candidate *old_candidates = candidates;
2495 /* [over.match.funcs] For conversion functions, the function
2496 is considered to be a member of the class of the implicit
2497 object argument for the purpose of defining the type of
2498 the implicit object parameter.
2500 So we pass fromtype as CTYPE to add_*_candidate. */
2502 if (TREE_CODE (fn) == TEMPLATE_DECL)
2504 add_template_candidate (candidates, fn, fromtype, NULL_TREE,
2505 args, totype, TYPE_BINFO (fromtype),
2510 candidates = add_function_candidate (candidates, fn, fromtype,
2512 TYPE_BINFO (fromtype),
2516 if (candidates != old_candidates)
2518 tree ics = implicit_conversion
2519 (totype, TREE_TYPE (TREE_TYPE (candidates->fn)),
2522 candidates->second_conv = ics;
2524 if (ics == NULL_TREE)
2525 candidates->viable = 0;
2526 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2527 candidates->viable = -1;
2532 if (! any_viable (candidates))
2535 candidates = splice_viable (candidates);
2536 cand = tourney (candidates);
2540 if (flags & LOOKUP_COMPLAIN)
2542 error ("conversion from `%T' to `%T' is ambiguous",
2544 print_z_candidates (candidates);
2547 cand = candidates; /* any one will do */
2548 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2549 ICS_USER_FLAG (cand->second_conv) = 1;
2550 if (!any_strictly_viable (candidates))
2551 ICS_BAD_FLAG (cand->second_conv) = 1;
2552 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2553 ambiguous conversion is no worse than another user-defined
2559 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
2560 p = &(TREE_OPERAND (*p, 0));
2564 (DECL_CONSTRUCTOR_P (cand->fn)
2565 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2566 expr, build_zc_wrapper (cand));
2568 ICS_USER_FLAG (cand->second_conv) = ICS_USER_FLAG (*p) = 1;
2569 if (cand->viable == -1)
2570 ICS_BAD_FLAG (cand->second_conv) = ICS_BAD_FLAG (*p) = 1;
2576 build_user_type_conversion (tree totype, tree expr, int flags)
2578 struct z_candidate *cand
2579 = build_user_type_conversion_1 (totype, expr, flags);
2583 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2584 return error_mark_node;
2585 return convert_from_reference (convert_like (cand->second_conv, expr));
2590 /* Find the possibly overloaded set of functions corresponding to a
2591 call of the form SCOPE::NAME (...). NAME might be a
2592 TEMPLATE_ID_EXPR, OVERLOAD, _DECL, IDENTIFIER_NODE or LOOKUP_EXPR. */
2595 resolve_scoped_fn_name (tree scope, tree name)
2598 tree template_args = NULL_TREE;
2599 bool is_template_id = TREE_CODE (name) == TEMPLATE_ID_EXPR;
2603 template_args = TREE_OPERAND (name, 1);
2604 name = TREE_OPERAND (name, 0);
2606 if (TREE_CODE (name) == OVERLOAD)
2607 name = DECL_NAME (get_first_fn (name));
2608 else if (TREE_CODE (name) == LOOKUP_EXPR)
2609 name = TREE_OPERAND (name, 0);
2611 if (TREE_CODE (scope) == NAMESPACE_DECL)
2612 fn = lookup_namespace_name (scope, name);
2615 if (!TYPE_BEING_DEFINED (scope)
2616 && !COMPLETE_TYPE_P (complete_type (scope)))
2618 error ("incomplete type '%T' cannot be used to name a scope",
2620 return error_mark_node;
2623 if (BASELINK_P (name))
2626 fn = lookup_member (scope, name, /*protect=*/1, /*want_type=*/false);
2627 if (fn && current_class_type)
2628 fn = (adjust_result_of_qualified_name_lookup
2629 (fn, scope, current_class_type));
2634 error ("'%D' has no member named '%E'", scope, name);
2635 return error_mark_node;
2641 if (BASELINK_P (fn))
2642 fns = BASELINK_FUNCTIONS (fns);
2643 fns = build_nt (TEMPLATE_ID_EXPR, fns, template_args);
2644 if (BASELINK_P (fn))
2645 BASELINK_FUNCTIONS (fn) = fns;
2653 /* Do any initial processing on the arguments to a function call. */
2656 resolve_args (tree args)
2659 for (t = args; t; t = TREE_CHAIN (t))
2661 tree arg = TREE_VALUE (t);
2663 if (arg == error_mark_node)
2664 return error_mark_node;
2665 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2667 error ("invalid use of void expression");
2668 return error_mark_node;
2670 else if (TREE_CODE (arg) == OFFSET_REF)
2671 arg = resolve_offset_ref (arg);
2672 arg = convert_from_reference (arg);
2673 TREE_VALUE (t) = arg;
2678 /* Return an expression for a call to FN (a namespace-scope function,
2679 or a static member function) with the ARGS. */
2682 build_new_function_call (tree fn, tree args)
2684 struct z_candidate *candidates = 0, *cand;
2685 tree explicit_targs = NULL_TREE;
2686 int template_only = 0;
2688 /* Check FN and ARGS. */
2689 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2690 || TREE_CODE (fn) == TEMPLATE_DECL
2691 || TREE_CODE (fn) == OVERLOAD
2692 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2694 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2697 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2699 explicit_targs = TREE_OPERAND (fn, 1);
2700 fn = TREE_OPERAND (fn, 0);
2704 if (really_overloaded_fn (fn)
2705 || TREE_CODE (fn) == TEMPLATE_DECL)
2709 args = resolve_args (args);
2711 if (args == error_mark_node)
2712 return error_mark_node;
2714 for (t1 = fn; t1; t1 = OVL_NEXT (t1))
2716 tree t = OVL_CURRENT (t1);
2718 my_friendly_assert (!DECL_FUNCTION_MEMBER_P (t), 20020913);
2720 if (TREE_CODE (t) == TEMPLATE_DECL)
2721 candidates = add_template_candidate
2722 (candidates, t, NULL_TREE, explicit_targs, args,
2723 NULL_TREE, /*access_path=*/NULL_TREE,
2724 /*conversion_path=*/NULL_TREE,
2725 LOOKUP_NORMAL, DEDUCE_CALL);
2726 else if (! template_only)
2727 candidates = add_function_candidate
2728 (candidates, t, NULL_TREE, args,
2729 /*access_path=*/NULL_TREE,
2730 /*conversion_path=*/NULL_TREE, LOOKUP_NORMAL);
2733 if (! any_viable (candidates))
2735 if (candidates && ! candidates->next)
2736 return build_function_call (candidates->fn, args);
2737 error ("no matching function for call to `%D(%A)'",
2738 DECL_NAME (OVL_CURRENT (fn)), args);
2740 print_z_candidates (candidates);
2741 return error_mark_node;
2743 candidates = splice_viable (candidates);
2744 cand = tourney (candidates);
2748 error ("call of overloaded `%D(%A)' is ambiguous",
2749 DECL_NAME (OVL_FUNCTION (fn)), args);
2750 print_z_candidates (candidates);
2751 return error_mark_node;
2754 return build_over_call (cand, LOOKUP_NORMAL);
2757 /* This is not really overloaded. */
2758 fn = OVL_CURRENT (fn);
2760 return build_function_call (fn, args);
2764 build_object_call (tree obj, tree args)
2766 struct z_candidate *candidates = 0, *cand;
2767 tree fns, convs, mem_args = NULL_TREE;
2768 tree type = TREE_TYPE (obj);
2770 if (TYPE_PTRMEMFUNC_P (type))
2772 /* It's no good looking for an overloaded operator() on a
2773 pointer-to-member-function. */
2774 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2775 return error_mark_node;
2778 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2779 if (fns == error_mark_node)
2780 return error_mark_node;
2782 args = resolve_args (args);
2784 if (args == error_mark_node)
2785 return error_mark_node;
2789 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2790 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2792 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2794 tree fn = OVL_CURRENT (fns);
2795 if (TREE_CODE (fn) == TEMPLATE_DECL)
2797 = add_template_candidate (candidates, fn, base, NULL_TREE,
2798 mem_args, NULL_TREE,
2801 LOOKUP_NORMAL, DEDUCE_CALL);
2803 candidates = add_function_candidate
2804 (candidates, fn, base, mem_args, TYPE_BINFO (type),
2805 TYPE_BINFO (type), LOOKUP_NORMAL);
2809 convs = lookup_conversions (type);
2811 for (; convs; convs = TREE_CHAIN (convs))
2813 tree fns = TREE_VALUE (convs);
2814 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2816 if ((TREE_CODE (totype) == POINTER_TYPE
2817 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2818 || (TREE_CODE (totype) == REFERENCE_TYPE
2819 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2820 || (TREE_CODE (totype) == REFERENCE_TYPE
2821 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2822 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2823 for (; fns; fns = OVL_NEXT (fns))
2825 tree fn = OVL_CURRENT (fns);
2826 if (TREE_CODE (fn) == TEMPLATE_DECL)
2828 candidates = (add_template_conv_candidate
2829 (candidates, fn, obj, args, totype,
2830 /*access_path=*/NULL_TREE,
2831 /*conversion_path=*/NULL_TREE));
2834 candidates = add_conv_candidate (candidates, fn, obj, args,
2835 /*conversion_path=*/NULL_TREE,
2836 /*access_path=*/NULL_TREE);
2840 if (! any_viable (candidates))
2842 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2843 print_z_candidates (candidates);
2844 return error_mark_node;
2847 candidates = splice_viable (candidates);
2848 cand = tourney (candidates);
2852 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2853 print_z_candidates (candidates);
2854 return error_mark_node;
2857 /* Since cand->fn will be a type, not a function, for a conversion
2858 function, we must be careful not to unconditionally look at
2860 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2861 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2862 return build_over_call (cand, LOOKUP_NORMAL);
2864 obj = convert_like_with_context
2865 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2868 return build_function_call (obj, args);
2872 op_error (enum tree_code code, enum tree_code code2,
2873 tree arg1, tree arg2, tree arg3, const char *problem)
2877 if (code == MODIFY_EXPR)
2878 opname = assignment_operator_name_info[code2].name;
2880 opname = operator_name_info[code].name;
2885 error ("%s for `%T ? %T : %T' operator", problem,
2886 error_type (arg1), error_type (arg2), error_type (arg3));
2888 case POSTINCREMENT_EXPR:
2889 case POSTDECREMENT_EXPR:
2890 error ("%s for `%T %s' operator", problem, error_type (arg1), opname);
2893 error ("%s for `%T [%T]' operator", problem,
2894 error_type (arg1), error_type (arg2));
2898 error ("%s for `%T %s %T' operator", problem,
2899 error_type (arg1), opname, error_type (arg2));
2901 error ("%s for `%s %T' operator", problem, opname, error_type (arg1));
2905 /* Return the implicit conversion sequence that could be used to
2906 convert E1 to E2 in [expr.cond]. */
2909 conditional_conversion (tree e1, tree e2)
2911 tree t1 = non_reference (TREE_TYPE (e1));
2912 tree t2 = non_reference (TREE_TYPE (e2));
2917 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2918 implicitly converted (clause _conv_) to the type "reference to
2919 T2", subject to the constraint that in the conversion the
2920 reference must bind directly (_dcl.init.ref_) to E1. */
2921 if (real_lvalue_p (e2))
2923 conv = implicit_conversion (build_reference_type (t2),
2926 LOOKUP_NO_TEMP_BIND);
2933 If E1 and E2 have class type, and the underlying class types are
2934 the same or one is a base class of the other: E1 can be converted
2935 to match E2 if the class of T2 is the same type as, or a base
2936 class of, the class of T1, and the cv-qualification of T2 is the
2937 same cv-qualification as, or a greater cv-qualification than, the
2938 cv-qualification of T1. If the conversion is applied, E1 is
2939 changed to an rvalue of type T2 that still refers to the original
2940 source class object (or the appropriate subobject thereof). */
2941 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2942 && same_or_base_type_p (TYPE_MAIN_VARIANT (t2),
2943 TYPE_MAIN_VARIANT (t1)))
2945 if (at_least_as_qualified_p (t2, t1))
2947 conv = build1 (IDENTITY_CONV, t1, e1);
2948 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2949 TYPE_MAIN_VARIANT (t2)))
2950 conv = build_conv (BASE_CONV, t2, conv);
2959 E1 can be converted to match E2 if E1 can be implicitly converted
2960 to the type that expression E2 would have if E2 were converted to
2961 an rvalue (or the type it has, if E2 is an rvalue). */
2962 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2965 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2966 arguments to the conditional expression. */
2969 build_conditional_expr (tree arg1, tree arg2, tree arg3)
2974 tree result_type = NULL_TREE;
2975 bool lvalue_p = true;
2976 struct z_candidate *candidates = 0;
2977 struct z_candidate *cand;
2979 /* As a G++ extension, the second argument to the conditional can be
2980 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2981 c'.) If the second operand is omitted, make sure it is
2982 calculated only once. */
2986 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2988 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
2989 if (real_lvalue_p (arg1))
2990 arg2 = arg1 = stabilize_reference (arg1);
2992 arg2 = arg1 = save_expr (arg1);
2997 The first expr ession is implicitly converted to bool (clause
2999 arg1 = cp_convert (boolean_type_node, arg1);
3001 /* If something has already gone wrong, just pass that fact up the
3003 if (arg1 == error_mark_node
3004 || arg2 == error_mark_node
3005 || arg3 == error_mark_node
3006 || TREE_TYPE (arg1) == error_mark_node
3007 || TREE_TYPE (arg2) == error_mark_node
3008 || TREE_TYPE (arg3) == error_mark_node)
3009 return error_mark_node;
3013 If either the second or the third operand has type (possibly
3014 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3015 array-to-pointer (_conv.array_), and function-to-pointer
3016 (_conv.func_) standard conversions are performed on the second
3017 and third operands. */
3018 arg2_type = TREE_TYPE (arg2);
3019 arg3_type = TREE_TYPE (arg3);
3020 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3022 /* Do the conversions. We don't these for `void' type arguments
3023 since it can't have any effect and since decay_conversion
3024 does not handle that case gracefully. */
3025 if (!VOID_TYPE_P (arg2_type))
3026 arg2 = decay_conversion (arg2);
3027 if (!VOID_TYPE_P (arg3_type))
3028 arg3 = decay_conversion (arg3);
3029 arg2_type = TREE_TYPE (arg2);
3030 arg3_type = TREE_TYPE (arg3);
3034 One of the following shall hold:
3036 --The second or the third operand (but not both) is a
3037 throw-expression (_except.throw_); the result is of the
3038 type of the other and is an rvalue.
3040 --Both the second and the third operands have type void; the
3041 result is of type void and is an rvalue. */
3042 if ((TREE_CODE (arg2) == THROW_EXPR)
3043 ^ (TREE_CODE (arg3) == THROW_EXPR))
3044 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
3045 ? arg3_type : arg2_type);
3046 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3047 result_type = void_type_node;
3050 error ("`%E' has type `void' and is not a throw-expression",
3051 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3052 return error_mark_node;
3056 goto valid_operands;
3060 Otherwise, if the second and third operand have different types,
3061 and either has (possibly cv-qualified) class type, an attempt is
3062 made to convert each of those operands to the type of the other. */
3063 else if (!same_type_p (arg2_type, arg3_type)
3064 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3066 tree conv2 = conditional_conversion (arg2, arg3);
3067 tree conv3 = conditional_conversion (arg3, arg2);
3071 If both can be converted, or one can be converted but the
3072 conversion is ambiguous, the program is ill-formed. If
3073 neither can be converted, the operands are left unchanged and
3074 further checking is performed as described below. If exactly
3075 one conversion is possible, that conversion is applied to the
3076 chosen operand and the converted operand is used in place of
3077 the original operand for the remainder of this section. */
3078 if ((conv2 && !ICS_BAD_FLAG (conv2)
3079 && conv3 && !ICS_BAD_FLAG (conv3))
3080 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3081 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3083 error ("operands to ?: have different types");
3084 return error_mark_node;
3086 else if (conv2 && !ICS_BAD_FLAG (conv2))
3088 arg2 = convert_like (conv2, arg2);
3089 arg2 = convert_from_reference (arg2);
3090 /* That may not quite have done the trick. If the two types
3091 are cv-qualified variants of one another, we will have
3092 just used an IDENTITY_CONV. (There's no conversion from
3093 an lvalue of one class type to an lvalue of another type,
3094 even a cv-qualified variant, and we don't want to lose
3095 lvalue-ness here.) So, we manually add a NOP_EXPR here
3097 if (!same_type_p (TREE_TYPE (arg2), arg3_type))
3098 arg2 = build1 (NOP_EXPR, arg3_type, arg2);
3099 arg2_type = TREE_TYPE (arg2);
3101 else if (conv3 && !ICS_BAD_FLAG (conv3))
3103 arg3 = convert_like (conv3, arg3);
3104 arg3 = convert_from_reference (arg3);
3105 if (!same_type_p (TREE_TYPE (arg3), arg2_type))
3106 arg3 = build1 (NOP_EXPR, arg2_type, arg3);
3107 arg3_type = TREE_TYPE (arg3);
3113 If the second and third operands are lvalues and have the same
3114 type, the result is of that type and is an lvalue. */
3115 if (real_lvalue_p (arg2) && real_lvalue_p (arg3) &&
3116 same_type_p (arg2_type, arg3_type))
3118 result_type = arg2_type;
3119 goto valid_operands;
3124 Otherwise, the result is an rvalue. If the second and third
3125 operand do not have the same type, and either has (possibly
3126 cv-qualified) class type, overload resolution is used to
3127 determine the conversions (if any) to be applied to the operands
3128 (_over.match.oper_, _over.built_). */
3130 if (!same_type_p (arg2_type, arg3_type)
3131 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3136 /* Rearrange the arguments so that add_builtin_candidate only has
3137 to know about two args. In build_builtin_candidates, the
3138 arguments are unscrambled. */
3142 candidates = add_builtin_candidates (candidates,
3145 ansi_opname (COND_EXPR),
3151 If the overload resolution fails, the program is
3153 if (!any_viable (candidates))
3155 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3156 print_z_candidates (candidates);
3157 return error_mark_node;
3159 candidates = splice_viable (candidates);
3160 cand = tourney (candidates);
3163 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3164 print_z_candidates (candidates);
3165 return error_mark_node;
3170 Otherwise, the conversions thus determined are applied, and
3171 the converted operands are used in place of the original
3172 operands for the remainder of this section. */
3173 conv = TREE_VEC_ELT (cand->convs, 0);
3174 arg1 = convert_like (conv, arg1);
3175 conv = TREE_VEC_ELT (cand->convs, 1);
3176 arg2 = convert_like (conv, arg2);
3177 conv = TREE_VEC_ELT (cand->convs, 2);
3178 arg3 = convert_like (conv, arg3);
3183 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3184 and function-to-pointer (_conv.func_) standard conversions are
3185 performed on the second and third operands.
3187 We need to force the lvalue-to-rvalue conversion here for class types,
3188 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3189 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3192 We use ocp_convert rather than build_user_type_conversion because the
3193 latter returns NULL_TREE on failure, while the former gives an error. */
3195 if (IS_AGGR_TYPE (TREE_TYPE (arg2)))
3196 arg2 = ocp_convert (TREE_TYPE (arg2), arg2,
3197 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3199 arg2 = decay_conversion (arg2);
3200 arg2_type = TREE_TYPE (arg2);
3202 if (IS_AGGR_TYPE (TREE_TYPE (arg3)))
3203 arg3 = ocp_convert (TREE_TYPE (arg3), arg3,
3204 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
3206 arg3 = decay_conversion (arg3);
3207 arg3_type = TREE_TYPE (arg3);
3209 if (arg2 == error_mark_node || arg3 == error_mark_node)
3210 return error_mark_node;
3214 After those conversions, one of the following shall hold:
3216 --The second and third operands have the same type; the result is of
3218 if (same_type_p (arg2_type, arg3_type))
3219 result_type = arg2_type;
3222 --The second and third operands have arithmetic or enumeration
3223 type; the usual arithmetic conversions are performed to bring
3224 them to a common type, and the result is of that type. */
3225 else if ((ARITHMETIC_TYPE_P (arg2_type)
3226 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3227 && (ARITHMETIC_TYPE_P (arg3_type)
3228 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3230 /* In this case, there is always a common type. */
3231 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3234 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3235 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3236 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3237 arg2_type, arg3_type);
3238 else if (extra_warnings
3239 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3240 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3241 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3242 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3243 warning ("enumeral and non-enumeral type in conditional expression");
3245 arg2 = perform_implicit_conversion (result_type, arg2);
3246 arg3 = perform_implicit_conversion (result_type, arg3);
3250 --The second and third operands have pointer type, or one has
3251 pointer type and the other is a null pointer constant; pointer
3252 conversions (_conv.ptr_) and qualification conversions
3253 (_conv.qual_) are performed to bring them to their composite
3254 pointer type (_expr.rel_). The result is of the composite
3257 --The second and third operands have pointer to member type, or
3258 one has pointer to member type and the other is a null pointer
3259 constant; pointer to member conversions (_conv.mem_) and
3260 qualification conversions (_conv.qual_) are performed to bring
3261 them to a common type, whose cv-qualification shall match the
3262 cv-qualification of either the second or the third operand.
3263 The result is of the common type. */
3264 else if ((null_ptr_cst_p (arg2)
3265 && (TYPE_PTR_P (arg3_type) || TYPE_PTRMEM_P (arg3_type)
3266 || TYPE_PTRMEMFUNC_P (arg3_type)))
3267 || (null_ptr_cst_p (arg3)
3268 && (TYPE_PTR_P (arg2_type) || TYPE_PTRMEM_P (arg2_type)
3269 || TYPE_PTRMEMFUNC_P (arg2_type)))
3270 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3271 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3272 || (TYPE_PTRMEMFUNC_P (arg2_type)
3273 && TYPE_PTRMEMFUNC_P (arg3_type)))
3275 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3276 arg3, "conditional expression");
3277 arg2 = perform_implicit_conversion (result_type, arg2);
3278 arg3 = perform_implicit_conversion (result_type, arg3);
3283 error ("operands to ?: have different types");
3284 return error_mark_node;
3288 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3289 /* Expand both sides into the same slot, hopefully the target of the
3290 ?: expression. We used to check for TARGET_EXPRs here, but now we
3291 sometimes wrap them in NOP_EXPRs so the test would fail. */
3292 if (!lvalue_p && IS_AGGR_TYPE (result_type))
3293 result = build_target_expr_with_type (result, result_type);
3295 /* If this expression is an rvalue, but might be mistaken for an
3296 lvalue, we must add a NON_LVALUE_EXPR. */
3297 if (!lvalue_p && real_lvalue_p (result))
3298 result = build1 (NON_LVALUE_EXPR, result_type, result);
3303 /* OPERAND is an operand to an expression. Perform necessary steps
3304 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3308 prep_operand (tree operand)
3312 if (TREE_CODE (operand) == OFFSET_REF)
3313 operand = resolve_offset_ref (operand);
3314 operand = convert_from_reference (operand);
3315 if (CLASS_TYPE_P (TREE_TYPE (operand))
3316 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3317 /* Make sure the template type is instantiated now. */
3318 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3324 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
3325 OVERLOAD) to the CANDIDATES, returning an updated list of
3326 CANDIDATES. The ARGS are the arguments provided to the call,
3327 without any implicit object parameter. CONVERSION_PATH,
3328 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3330 static struct z_candidate *
3331 add_candidates (tree fns, tree args,
3332 tree conversion_path, tree access_path,
3334 struct z_candidate *candidates)
3337 tree non_static_args;
3339 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3340 /* Delay creating the implicit this parameter until it is needed. */
3341 non_static_args = NULL_TREE;
3348 fn = OVL_CURRENT (fns);
3349 /* Figure out which set of arguments to use. */
3350 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3352 /* If this function is a non-static member, prepend the implicit
3353 object parameter. */
3354 if (!non_static_args)
3355 non_static_args = tree_cons (NULL_TREE,
3356 build_this (TREE_VALUE (args)),
3358 fn_args = non_static_args;
3361 /* Otherwise, just use the list of arguments provided. */
3364 if (TREE_CODE (fn) == TEMPLATE_DECL)
3365 candidates = add_template_candidate (candidates,
3376 candidates = add_function_candidate (candidates,
3383 fns = OVL_NEXT (fns);
3390 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3)
3392 struct z_candidate *candidates = 0, *cand;
3393 tree arglist, fnname;
3395 enum tree_code code2 = NOP_EXPR;
3397 bool viable_candidates;
3399 if (error_operand_p (arg1)
3400 || error_operand_p (arg2)
3401 || error_operand_p (arg3))
3402 return error_mark_node;
3404 if (code == MODIFY_EXPR)
3406 code2 = TREE_CODE (arg3);
3408 fnname = ansi_assopname (code2);
3411 fnname = ansi_opname (code);
3413 arg1 = prep_operand (arg1);
3419 case VEC_DELETE_EXPR:
3421 /* Use build_op_new_call and build_op_delete_call instead. */
3425 return build_object_call (arg1, arg2);
3431 arg2 = prep_operand (arg2);
3432 arg3 = prep_operand (arg3);
3434 if (code == COND_EXPR)
3436 if (arg2 == NULL_TREE
3437 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3438 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3439 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3440 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3443 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3444 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3447 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3448 arg2 = integer_zero_node;
3450 arglist = NULL_TREE;
3452 arglist = tree_cons (NULL_TREE, arg3, arglist);
3454 arglist = tree_cons (NULL_TREE, arg2, arglist);
3455 arglist = tree_cons (NULL_TREE, arg1, arglist);
3457 /* Add namespace-scope operators to the list of functions to
3459 candidates = add_candidates (lookup_function_nonclass (fnname, arglist),
3460 arglist, NULL_TREE, NULL_TREE,
3462 /* Add class-member operators to the candidate set. */
3463 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
3467 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3468 if (fns == error_mark_node)
3471 candidates = add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3472 BASELINK_BINFO (fns),
3473 TYPE_BINFO (TREE_TYPE (arg1)),
3477 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3478 to know about two args; a builtin candidate will always have a first
3479 parameter of type bool. We'll handle that in
3480 build_builtin_candidate. */
3481 if (code == COND_EXPR)
3491 args[2] = NULL_TREE;
3494 candidates = add_builtin_candidates
3495 (candidates, code, code2, fnname, args, flags);
3501 /* For these, the built-in candidates set is empty
3502 [over.match.oper]/3. We don't want non-strict matches
3503 because exact matches are always possible with built-in
3504 operators. The built-in candidate set for COMPONENT_REF
3505 would be empty too, but since there are no such built-in
3506 operators, we accept non-strict matches for them. */
3507 viable_candidates = any_strictly_viable (candidates);
3511 viable_candidates = any_viable (candidates);
3515 if (! viable_candidates)
3519 case POSTINCREMENT_EXPR:
3520 case POSTDECREMENT_EXPR:
3521 /* Look for an `operator++ (int)'. If they didn't have
3522 one, then we fall back to the old way of doing things. */
3523 if (flags & LOOKUP_COMPLAIN)
3524 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3526 operator_name_info[code].name);
3527 if (code == POSTINCREMENT_EXPR)
3528 code = PREINCREMENT_EXPR;
3530 code = PREDECREMENT_EXPR;
3531 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3533 /* The caller will deal with these. */
3542 if (flags & LOOKUP_COMPLAIN)
3544 op_error (code, code2, arg1, arg2, arg3, "no match");
3545 print_z_candidates (candidates);
3547 return error_mark_node;
3549 candidates = splice_viable (candidates);
3550 cand = tourney (candidates);
3554 if (flags & LOOKUP_COMPLAIN)
3556 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3557 print_z_candidates (candidates);
3559 return error_mark_node;
3562 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3565 && fnname == ansi_assopname (NOP_EXPR)
3566 && DECL_ARTIFICIAL (cand->fn)
3568 && ! candidates->next->next)
3570 warning ("using synthesized `%#D' for copy assignment",
3572 cp_warning_at (" where cfront would use `%#D'",
3574 ? candidates->next->fn
3578 return build_over_call (cand, LOOKUP_NORMAL);
3581 /* Check for comparison of different enum types. */
3590 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3591 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3592 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3593 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3595 warning ("comparison between `%#T' and `%#T'",
3596 TREE_TYPE (arg1), TREE_TYPE (arg2));
3603 /* We need to strip any leading REF_BIND so that bitfields don't cause
3604 errors. This should not remove any important conversions, because
3605 builtins don't apply to class objects directly. */
3606 conv = TREE_VEC_ELT (cand->convs, 0);
3607 if (TREE_CODE (conv) == REF_BIND)
3608 conv = TREE_OPERAND (conv, 0);
3609 arg1 = convert_like (conv, arg1);
3612 conv = TREE_VEC_ELT (cand->convs, 1);
3613 if (TREE_CODE (conv) == REF_BIND)
3614 conv = TREE_OPERAND (conv, 0);
3615 arg2 = convert_like (conv, arg2);
3619 conv = TREE_VEC_ELT (cand->convs, 2);
3620 if (TREE_CODE (conv) == REF_BIND)
3621 conv = TREE_OPERAND (conv, 0);
3622 arg3 = convert_like (conv, arg3);
3629 return build_modify_expr (arg1, code2, arg2);
3632 return build_indirect_ref (arg1, "unary *");
3637 case TRUNC_DIV_EXPR:
3648 case TRUNC_MOD_EXPR:
3652 case TRUTH_ANDIF_EXPR:
3653 case TRUTH_ORIF_EXPR:
3654 return cp_build_binary_op (code, arg1, arg2);
3659 case TRUTH_NOT_EXPR:
3660 case PREINCREMENT_EXPR:
3661 case POSTINCREMENT_EXPR:
3662 case PREDECREMENT_EXPR:
3663 case POSTDECREMENT_EXPR:
3666 return build_unary_op (code, arg1, candidates != 0);
3669 return build_array_ref (arg1, arg2);
3672 return build_conditional_expr (arg1, arg2, arg3);
3675 return build_m_component_ref
3676 (build_indirect_ref (arg1, NULL), arg2);
3678 /* The caller will deal with these. */
3690 /* Build a call to operator delete. This has to be handled very specially,
3691 because the restrictions on what signatures match are different from all
3692 other call instances. For a normal delete, only a delete taking (void *)
3693 or (void *, size_t) is accepted. For a placement delete, only an exact
3694 match with the placement new is accepted.
3696 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3697 ADDR is the pointer to be deleted.
3698 SIZE is the size of the memory block to be deleted.
3699 FLAGS are the usual overloading flags.
3700 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3703 build_op_delete_call (enum tree_code code, tree addr, tree size,
3704 int flags, tree placement)
3706 tree fn = NULL_TREE;
3707 tree fns, fnname, fntype, argtypes, args, type;
3710 if (addr == error_mark_node)
3711 return error_mark_node;
3713 type = TREE_TYPE (TREE_TYPE (addr));
3714 while (TREE_CODE (type) == ARRAY_TYPE)
3715 type = TREE_TYPE (type);
3717 fnname = ansi_opname (code);
3719 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3722 If the result of the lookup is ambiguous or inaccessible, or if
3723 the lookup selects a placement deallocation function, the
3724 program is ill-formed.
3726 Therefore, we ask lookup_fnfields to complain ambout ambiguity. */
3728 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3729 if (fns == error_mark_node)
3730 return error_mark_node;
3735 if (fns == NULL_TREE)
3736 fns = lookup_name_nonclass (fnname);
3743 /* Find the allocation function that is being called. */
3744 call_expr = placement;
3745 /* Sometimes we have a COMPOUND_EXPR, rather than a simple
3747 while (TREE_CODE (call_expr) == COMPOUND_EXPR)
3748 call_expr = TREE_OPERAND (call_expr, 1);
3749 /* Extract the function. */
3750 alloc_fn = get_callee_fndecl (call_expr);
3751 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3752 /* Then the second parm type. */
3753 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3754 /* Also the second argument. */
3755 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3759 /* First try it without the size argument. */
3760 argtypes = void_list_node;
3764 /* Strip const and volatile from addr. */
3765 addr = cp_convert (ptr_type_node, addr);
3767 /* We make two tries at finding a matching `operator delete'. On
3768 the first pass, we look for an one-operator (or placement)
3769 operator delete. If we're not doing placement delete, then on
3770 the second pass we look for a two-argument delete. */
3771 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3774 argtypes = tree_cons (NULL_TREE, ptr_type_node, argtypes);
3776 /* Normal delete; now try to find a match including the size
3778 argtypes = tree_cons (NULL_TREE, ptr_type_node,
3779 tree_cons (NULL_TREE, sizetype,
3781 fntype = build_function_type (void_type_node, argtypes);
3783 /* Go through the `operator delete' functions looking for one
3784 with a matching type. */
3785 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3791 /* Exception specifications on the `delete' operator do not
3793 t = build_exception_variant (TREE_TYPE (OVL_CURRENT (fn)),
3795 /* We also don't compare attributes. We're really just
3796 trying to check the types of the first two parameters. */
3797 if (comptypes (t, fntype, COMPARE_NO_ATTRIBUTES))
3801 /* If we found a match, we're done. */
3806 /* If we have a matching function, call it. */
3809 /* Make sure we have the actual function, and not an
3811 fn = OVL_CURRENT (fn);
3813 /* If the FN is a member function, make sure that it is
3815 if (DECL_CLASS_SCOPE_P (fn))
3816 enforce_access (type, fn);
3819 args = tree_cons (NULL_TREE, addr, args);
3821 args = tree_cons (NULL_TREE, addr,
3822 build_tree_list (NULL_TREE, size));
3824 return build_function_call (fn, args);
3827 /* If we are doing placement delete we do nothing if we don't find a
3828 matching op delete. */
3832 error ("no suitable `operator delete' for `%T'", type);
3833 return error_mark_node;
3836 /* If the current scope isn't allowed to access DECL along
3837 BASETYPE_PATH, give an error. The most derived class in
3838 BASETYPE_PATH is the one used to qualify DECL. */
3841 enforce_access (tree basetype_path, tree decl)
3843 if (!accessible_p (basetype_path, decl))
3845 if (TREE_PRIVATE (decl))
3846 cp_error_at ("`%+#D' is private", decl);
3847 else if (TREE_PROTECTED (decl))
3848 cp_error_at ("`%+#D' is protected", decl);
3850 cp_error_at ("`%+#D' is inaccessible", decl);
3851 error ("within this context");
3858 /* Perform the conversions in CONVS on the expression EXPR.
3859 FN and ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3860 indicates the `this' argument of a method. INNER is nonzero when
3861 being called to continue a conversion chain. It is negative when a
3862 reference binding will be applied, positive otherwise. */
3865 convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner)
3869 tree totype = TREE_TYPE (convs);
3871 if (ICS_BAD_FLAG (convs)
3872 && TREE_CODE (convs) != USER_CONV
3873 && TREE_CODE (convs) != AMBIG_CONV
3874 && TREE_CODE (convs) != REF_BIND)
3877 for (; t; t = TREE_OPERAND (t, 0))
3879 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3881 expr = convert_like_real (t, expr, fn, argnum, 1);
3884 else if (TREE_CODE (t) == AMBIG_CONV)
3885 return convert_like_real (t, expr, fn, argnum, 1);
3886 else if (TREE_CODE (t) == IDENTITY_CONV)
3889 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3891 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3892 return cp_convert (totype, expr);
3896 expr = dubious_conversion_warnings
3897 (totype, expr, "argument", fn, argnum);
3898 switch (TREE_CODE (convs))
3902 struct z_candidate *cand
3903 = WRAPPER_ZC (TREE_OPERAND (convs, 1));
3904 tree convfn = cand->fn;
3907 if (DECL_CONSTRUCTOR_P (convfn))
3909 tree t = build_int_2 (0, 0);
3910 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3912 args = build_tree_list (NULL_TREE, expr);
3913 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3914 || DECL_HAS_VTT_PARM_P (convfn))
3915 /* We should never try to call the abstract or base constructor
3918 args = tree_cons (NULL_TREE, t, args);
3921 args = build_this (expr);
3922 expr = build_over_call (cand, LOOKUP_NORMAL);
3924 /* If this is a constructor or a function returning an aggr type,
3925 we need to build up a TARGET_EXPR. */
3926 if (DECL_CONSTRUCTOR_P (convfn))
3927 expr = build_cplus_new (totype, expr);
3929 /* The result of the call is then used to direct-initialize the object
3930 that is the destination of the copy-initialization. [dcl.init]
3932 Note that this step is not reflected in the conversion sequence;
3933 it affects the semantics when we actually perform the
3934 conversion, but is not considered during overload resolution.
3936 If the target is a class, that means call a ctor. */
3937 if (IS_AGGR_TYPE (totype)
3938 && (inner >= 0 || !lvalue_p (expr)))
3940 savew = warningcount, savee = errorcount;
3941 expr = build_special_member_call
3942 (NULL_TREE, complete_ctor_identifier,
3943 build_tree_list (NULL_TREE, expr), TYPE_BINFO (totype),
3944 /* Core issue 84, now a DR, says that we don't allow UDCs
3945 for these args (which deliberately breaks copy-init of an
3946 auto_ptr<Base> from an auto_ptr<Derived>). */
3947 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION);
3949 /* Tell the user where this failing constructor call came from. */
3952 if (warningcount > savew)
3954 (" initializing argument %P of `%D' from result of `%D'",
3955 argnum, fn, convfn);
3956 else if (errorcount > savee)
3958 (" initializing argument %P of `%D' from result of `%D'",
3959 argnum, fn, convfn);
3963 if (warningcount > savew)
3964 warning (" initializing temporary from result of `%D'",
3966 else if (errorcount > savee)
3967 error (" initializing temporary from result of `%D'",
3970 expr = build_cplus_new (totype, expr);
3975 if (type_unknown_p (expr))
3976 expr = instantiate_type (totype, expr, tf_error | tf_warning);
3977 /* Convert a non-array constant variable to its underlying value, unless we
3978 are about to bind it to a reference, in which case we need to
3979 leave it as an lvalue. */
3981 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3982 expr = decl_constant_value (expr);
3985 /* Call build_user_type_conversion again for the error. */
3986 return build_user_type_conversion
3987 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3993 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3994 TREE_CODE (convs) == REF_BIND ? -1 : 1);
3995 if (expr == error_mark_node)
3996 return error_mark_node;
3998 switch (TREE_CODE (convs))
4001 if (! IS_AGGR_TYPE (totype))
4003 /* else fall through */
4005 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4007 /* We are going to bind a reference directly to a base-class
4008 subobject of EXPR. */
4009 tree base_ptr = build_pointer_type (totype);
4011 /* Build an expression for `*((base*) &expr)'. */
4012 expr = build_unary_op (ADDR_EXPR, expr, 0);
4013 expr = perform_implicit_conversion (base_ptr, expr);
4014 expr = build_indirect_ref (expr, "implicit conversion");
4018 /* Copy-initialization where the cv-unqualified version of the source
4019 type is the same class as, or a derived class of, the class of the
4020 destination [is treated as direct-initialization]. [dcl.init] */
4021 savew = warningcount, savee = errorcount;
4022 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4023 build_tree_list (NULL_TREE, expr),
4024 TYPE_BINFO (totype),
4025 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING);
4028 if (warningcount > savew)
4029 warning (" initializing argument %P of `%D'", argnum, fn);
4030 else if (errorcount > savee)
4031 error (" initializing argument %P of `%D'", argnum, fn);
4033 return build_cplus_new (totype, expr);
4037 tree ref_type = totype;
4039 /* If necessary, create a temporary. */
4040 if (NEED_TEMPORARY_P (convs) || !non_cast_lvalue_p (expr))
4042 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4043 expr = build_target_expr_with_type (expr, type);
4046 /* Take the address of the thing to which we will bind the
4048 expr = build_unary_op (ADDR_EXPR, expr, 1);
4049 if (expr == error_mark_node)
4050 return error_mark_node;
4052 /* Convert it to a pointer to the type referred to by the
4053 reference. This will adjust the pointer if a derived to
4054 base conversion is being performed. */
4055 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4057 /* Convert the pointer to the desired reference type. */
4058 expr = build1 (NOP_EXPR, ref_type, expr);
4064 return decay_conversion (expr);
4067 /* Warn about deprecated conversion if appropriate. */
4068 string_conv_p (totype, expr, 1);
4074 return ocp_convert (totype, expr, CONV_IMPLICIT,
4075 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4078 /* Build a call to __builtin_trap which can be used in an expression. */
4081 call_builtin_trap (void)
4083 tree fn = get_identifier ("__builtin_trap");
4084 if (IDENTIFIER_GLOBAL_VALUE (fn))
4085 fn = IDENTIFIER_GLOBAL_VALUE (fn);
4089 fn = build_call (fn, NULL_TREE);
4090 fn = build (COMPOUND_EXPR, integer_type_node, fn, integer_zero_node);
4094 /* ARG is being passed to a varargs function. Perform any conversions
4095 required. Array/function to pointer decay must have already happened.
4096 Return the converted value. */
4099 convert_arg_to_ellipsis (tree arg)
4101 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4102 && (TYPE_PRECISION (TREE_TYPE (arg))
4103 < TYPE_PRECISION (double_type_node)))
4104 /* Convert `float' to `double'. */
4105 arg = cp_convert (double_type_node, arg);
4107 /* Convert `short' and `char' to full-size `int'. */
4108 arg = default_conversion (arg);
4110 arg = require_complete_type (arg);
4112 if (arg != error_mark_node && ! pod_type_p (TREE_TYPE (arg)))
4114 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4115 here and do a bitwise copy, but now cp_expr_size will abort if we
4117 warning ("cannot pass objects of non-POD type `%#T' through `...'; \
4118 call will abort at runtime",
4120 arg = call_builtin_trap ();
4126 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4129 build_x_va_arg (tree expr, tree type)
4131 if (processing_template_decl)
4132 return build_min (VA_ARG_EXPR, type, expr);
4134 type = complete_type_or_else (type, NULL_TREE);
4136 if (expr == error_mark_node || !type)
4137 return error_mark_node;
4139 if (! pod_type_p (type))
4141 /* Undefined behavior [expr.call] 5.2.2/7. */
4142 warning ("cannot receive objects of non-POD type `%#T' through `...'",
4146 return build_va_arg (expr, type);
4149 /* TYPE has been given to va_arg. Apply the default conversions which
4150 would have happened when passed via ellipsis. Return the promoted
4151 type, or the passed type if there is no change. */
4154 cxx_type_promotes_to (tree type)
4158 if (TREE_CODE (type) == ARRAY_TYPE)
4159 return build_pointer_type (TREE_TYPE (type));
4161 if (TREE_CODE (type) == FUNCTION_TYPE)
4162 return build_pointer_type (type);
4164 promote = type_promotes_to (type);
4165 if (same_type_p (type, promote))
4171 /* ARG is a default argument expression being passed to a parameter of
4172 the indicated TYPE, which is a parameter to FN. Do any required
4173 conversions. Return the converted value. */
4176 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4178 /* If the ARG is an unparsed default argument expression, the
4179 conversion cannot be performed. */
4180 if (TREE_CODE (arg) == DEFAULT_ARG)
4182 error ("the default argument for parameter %d of `%D' has "
4183 "not yet been parsed",
4185 return error_mark_node;
4188 if (fn && DECL_TEMPLATE_INFO (fn))
4189 arg = tsubst_default_argument (fn, type, arg);
4191 arg = break_out_target_exprs (arg);
4193 if (TREE_CODE (arg) == CONSTRUCTOR)
4195 arg = digest_init (type, arg, 0);
4196 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4197 "default argument", fn, parmnum);
4201 /* This could get clobbered by the following call. */
4202 if (TREE_HAS_CONSTRUCTOR (arg))
4203 arg = copy_node (arg);
4205 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4206 "default argument", fn, parmnum);
4207 arg = convert_for_arg_passing (type, arg);
4213 /* Returns the type which will really be used for passing an argument of
4217 type_passed_as (tree type)
4219 /* Pass classes with copy ctors by invisible reference. */
4220 if (TREE_ADDRESSABLE (type))
4221 type = build_reference_type (type);
4222 else if (PROMOTE_PROTOTYPES
4223 && INTEGRAL_TYPE_P (type)
4224 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4225 type = integer_type_node;
4230 /* Actually perform the appropriate conversion. */
4233 convert_for_arg_passing (tree type, tree val)
4235 if (val == error_mark_node)
4237 /* Pass classes with copy ctors by invisible reference. */
4238 else if (TREE_ADDRESSABLE (type))
4239 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4240 else if (PROMOTE_PROTOTYPES
4241 && INTEGRAL_TYPE_P (type)
4242 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4243 val = default_conversion (val);
4247 /* Subroutine of the various build_*_call functions. Overload resolution
4248 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4249 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4250 bitmask of various LOOKUP_* flags which apply to the call itself. */
4253 build_over_call (struct z_candidate *cand, int flags)
4256 tree args = cand->args;
4257 tree convs = cand->convs;
4258 tree converted_args = NULL_TREE;
4259 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4260 tree conv, arg, val;
4264 /* Give any warnings we noticed during overload resolution. */
4266 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4267 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4269 if (DECL_FUNCTION_MEMBER_P (fn))
4270 enforce_access (cand->access_path, fn);
4272 if (args && TREE_CODE (args) != TREE_LIST)
4273 args = build_tree_list (NULL_TREE, args);
4276 /* The implicit parameters to a constructor are not considered by overload
4277 resolution, and must be of the proper type. */
4278 if (DECL_CONSTRUCTOR_P (fn))
4280 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4281 arg = TREE_CHAIN (arg);
4282 parm = TREE_CHAIN (parm);
4283 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4284 /* We should never try to call the abstract constructor. */
4286 if (DECL_HAS_VTT_PARM_P (fn))
4288 converted_args = tree_cons
4289 (NULL_TREE, TREE_VALUE (arg), converted_args);
4290 arg = TREE_CHAIN (arg);
4291 parm = TREE_CHAIN (parm);
4294 /* Bypass access control for 'this' parameter. */
4295 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4297 tree parmtype = TREE_VALUE (parm);
4298 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4302 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4303 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4304 TREE_TYPE (argtype), fn);
4306 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4307 X is called for an object that is not of type X, or of a type
4308 derived from X, the behavior is undefined.
4310 So we can assume that anything passed as 'this' is non-null, and
4311 optimize accordingly. */
4312 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4313 /* Convert to the base in which the function was declared. */
4314 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4315 converted_arg = build_base_path (PLUS_EXPR,
4317 cand->conversion_path,
4319 /* If fn was found by a using declaration, the conversion path
4320 will be to the derived class, not the base declaring fn. We
4321 must convert from derived to base. */
4322 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4323 TREE_TYPE (parmtype), ba_ignore, NULL);
4325 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4328 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4329 parm = TREE_CHAIN (parm);
4330 arg = TREE_CHAIN (arg);
4336 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4338 tree type = TREE_VALUE (parm);
4340 conv = TREE_VEC_ELT (convs, i);
4341 val = convert_like_with_context
4342 (conv, TREE_VALUE (arg), fn, i - is_method);
4344 val = convert_for_arg_passing (type, val);
4345 converted_args = tree_cons (NULL_TREE, val, converted_args);
4348 /* Default arguments */
4349 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4351 = tree_cons (NULL_TREE,
4352 convert_default_arg (TREE_VALUE (parm),
4353 TREE_PURPOSE (parm),
4358 for (; arg; arg = TREE_CHAIN (arg))
4360 = tree_cons (NULL_TREE,
4361 convert_arg_to_ellipsis (TREE_VALUE (arg)),
4364 converted_args = nreverse (converted_args);
4367 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4370 /* Avoid actually calling copy constructors and copy assignment operators,
4373 if (! flag_elide_constructors)
4374 /* Do things the hard way. */;
4375 else if (TREE_VEC_LENGTH (convs) == 1
4376 && DECL_COPY_CONSTRUCTOR_P (fn))
4379 arg = skip_artificial_parms_for (fn, converted_args);
4380 arg = TREE_VALUE (arg);
4382 /* Pull out the real argument, disregarding const-correctness. */
4384 while (TREE_CODE (targ) == NOP_EXPR
4385 || TREE_CODE (targ) == NON_LVALUE_EXPR
4386 || TREE_CODE (targ) == CONVERT_EXPR)
4387 targ = TREE_OPERAND (targ, 0);
4388 if (TREE_CODE (targ) == ADDR_EXPR)
4390 targ = TREE_OPERAND (targ, 0);
4391 if (!same_type_ignoring_top_level_qualifiers_p
4392 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4401 arg = build_indirect_ref (arg, 0);
4403 /* [class.copy]: the copy constructor is implicitly defined even if
4404 the implementation elided its use. */
4405 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4408 /* If we're creating a temp and we already have one, don't create a
4409 new one. If we're not creating a temp but we get one, use
4410 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4411 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4412 temp or an INIT_EXPR otherwise. */
4413 if (integer_zerop (TREE_VALUE (args)))
4415 if (TREE_CODE (arg) == TARGET_EXPR)
4417 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4418 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4420 else if (TREE_CODE (arg) == TARGET_EXPR
4421 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4424 tree to = stabilize_reference
4425 (build_indirect_ref (TREE_VALUE (args), 0));
4427 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4428 address = build_unary_op (ADDR_EXPR, val, 0);
4429 /* Avoid a warning about this expression, if the address is
4431 TREE_USED (address) = 1;
4435 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4437 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4439 tree to = stabilize_reference
4440 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4442 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4443 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4449 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4451 tree t, *p = &TREE_VALUE (converted_args);
4452 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4455 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4457 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4458 if (TREE_SIDE_EFFECTS (*p))
4459 *p = save_expr (*p);
4460 t = build_pointer_type (TREE_TYPE (fn));
4461 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4462 fn = build_java_interface_fn_ref (fn, *p);
4464 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4467 else if (DECL_INLINE (fn))
4468 fn = inline_conversion (fn);
4470 fn = build_addr_func (fn);
4472 /* Recognize certain built-in functions so we can make tree-codes
4473 other than CALL_EXPR. We do this when it enables fold-const.c
4474 to do something useful. */
4476 if (TREE_CODE (fn) == ADDR_EXPR
4477 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4478 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4481 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4486 /* Some built-in function calls will be evaluated at
4487 compile-time in fold (). */
4488 fn = fold (build_call (fn, converted_args));
4489 if (VOID_TYPE_P (TREE_TYPE (fn)))
4491 fn = require_complete_type (fn);
4492 if (fn == error_mark_node)
4493 return error_mark_node;
4494 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4495 fn = build_cplus_new (TREE_TYPE (fn), fn);
4496 return convert_from_reference (fn);
4499 static GTY(()) tree java_iface_lookup_fn;
4501 /* Make an expression which yields the address of the Java interface
4502 method FN. This is achieved by generating a call to libjava's
4503 _Jv_LookupInterfaceMethodIdx(). */
4506 build_java_interface_fn_ref (tree fn, tree instance)
4508 tree lookup_args, lookup_fn, method, idx;
4509 tree klass_ref, iface, iface_ref;
4512 if (!java_iface_lookup_fn)
4514 tree endlink = build_void_list_node ();
4515 tree t = tree_cons (NULL_TREE, ptr_type_node,
4516 tree_cons (NULL_TREE, ptr_type_node,
4517 tree_cons (NULL_TREE, java_int_type_node,
4519 java_iface_lookup_fn
4520 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4521 build_function_type (ptr_type_node, t),
4522 0, NOT_BUILT_IN, NULL, NULL_TREE);
4525 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4526 This is the first entry in the vtable. */
4527 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4530 /* Get the java.lang.Class pointer for the interface being called. */
4531 iface = DECL_CONTEXT (fn);
4532 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
4533 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4534 || DECL_CONTEXT (iface_ref) != iface)
4536 error ("could not find class$ field in java interface type `%T'",
4538 return error_mark_node;
4540 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4542 /* Determine the itable index of FN. */
4544 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4546 if (!DECL_VIRTUAL_P (method))
4552 idx = build_int_2 (i, 0);
4554 lookup_args = tree_cons (NULL_TREE, klass_ref,
4555 tree_cons (NULL_TREE, iface_ref,
4556 build_tree_list (NULL_TREE, idx)));
4557 lookup_fn = build1 (ADDR_EXPR,
4558 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4559 java_iface_lookup_fn);
4560 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4563 /* Returns the value to use for the in-charge parameter when making a
4564 call to a function with the indicated NAME. */
4567 in_charge_arg_for_name (tree name)
4569 if (name == base_ctor_identifier
4570 || name == base_dtor_identifier)
4571 return integer_zero_node;
4572 else if (name == complete_ctor_identifier)
4573 return integer_one_node;
4574 else if (name == complete_dtor_identifier)
4575 return integer_two_node;
4576 else if (name == deleting_dtor_identifier)
4577 return integer_three_node;
4579 /* This function should only be called with one of the names listed
4585 /* Build a call to a constructor, destructor, or an assignment
4586 operator for INSTANCE, an expression with class type. NAME
4587 indicates the special member function to call; ARGS are the
4588 arguments. BINFO indicates the base of INSTANCE that is to be
4589 passed as the `this' parameter to the member function called.
4591 FLAGS are the LOOKUP_* flags to use when processing the call.
4593 If NAME indicates a complete object constructor, INSTANCE may be
4594 NULL_TREE. In this case, the caller will call build_cplus_new to
4595 store the newly constructed object into a VAR_DECL. */
4598 build_special_member_call (tree instance, tree name, tree args,
4599 tree binfo, int flags)
4602 /* The type of the subobject to be constructed or destroyed. */
4605 my_friendly_assert (name == complete_ctor_identifier
4606 || name == base_ctor_identifier
4607 || name == complete_dtor_identifier
4608 || name == base_dtor_identifier
4609 || name == deleting_dtor_identifier
4610 || name == ansi_assopname (NOP_EXPR),
4612 my_friendly_assert (binfo != NULL_TREE, 20020712);
4614 class_type = BINFO_TYPE (binfo);
4616 /* Handle the special case where INSTANCE is NULL_TREE. */
4617 if (name == complete_ctor_identifier && !instance)
4619 instance = build_int_2 (0, 0);
4620 TREE_TYPE (instance) = build_pointer_type (class_type);
4621 instance = build1 (INDIRECT_REF, class_type, instance);
4623 else if (name == complete_dtor_identifier
4624 || name == base_dtor_identifier
4625 || name == deleting_dtor_identifier)
4626 my_friendly_assert (args == NULL_TREE, 20020712);
4628 my_friendly_assert (instance != NULL_TREE, 20020712);
4630 /* Resolve the name. */
4631 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4632 return error_mark_node;
4634 fns = lookup_fnfields (binfo, name, 1);
4636 /* When making a call to a constructor or destructor for a subobject
4637 that uses virtual base classes, pass down a pointer to a VTT for
4639 if ((name == base_ctor_identifier
4640 || name == base_dtor_identifier)
4641 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4646 /* If the current function is a complete object constructor
4647 or destructor, then we fetch the VTT directly.
4648 Otherwise, we look it up using the VTT we were given. */
4649 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4650 vtt = decay_conversion (vtt);
4651 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4652 build (EQ_EXPR, boolean_type_node,
4653 current_in_charge_parm, integer_zero_node),
4656 if (TREE_VIA_VIRTUAL (binfo))
4657 binfo = binfo_for_vbase (class_type, current_class_type);
4658 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4659 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4660 BINFO_SUBVTT_INDEX (binfo));
4662 args = tree_cons (NULL_TREE, sub_vtt, args);
4665 return build_new_method_call (instance, fns, args, binfo, flags);
4668 /* Return the NAME, as a C string. The NAME indicates a function that
4669 is a member of TYPE. *FREE_P is set to true if the caller must
4670 free the memory returned.
4672 Rather than go through all of this, we should simply set the names
4673 of constructors and destructors appropriately, and dispense with
4674 ctor_identifier, dtor_identifier, etc. */
4677 name_as_c_string (tree name, tree type, bool *free_p)
4681 /* Assume that we will not allocate memory. */
4683 /* Constructors and destructors are special. */
4684 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4687 = (char *) IDENTIFIER_POINTER (constructor_name (type));
4688 /* For a destructor, add the '~'. */
4689 if (name == complete_dtor_identifier
4690 || name == base_dtor_identifier
4691 || name == deleting_dtor_identifier)
4693 pretty_name = concat ("~", pretty_name, NULL);
4694 /* Remember that we need to free the memory allocated. */
4699 pretty_name = (char *) IDENTIFIER_POINTER (name);
4704 /* Build a call to "INSTANCE.FN (ARGS)". */
4707 build_new_method_call (tree instance, tree fns, tree args,
4708 tree conversion_path, int flags)
4710 struct z_candidate *candidates = 0, *cand;
4711 tree explicit_targs = NULL_TREE;
4712 tree basetype = NULL_TREE;
4715 tree mem_args = NULL_TREE, instance_ptr;
4721 int template_only = 0;
4723 my_friendly_assert (instance != NULL_TREE, 20020729);
4725 if (error_operand_p (instance)
4726 || error_operand_p (fns)
4727 || args == error_mark_node)
4728 return error_mark_node;
4730 /* Process the argument list. */
4732 args = resolve_args (args);
4733 if (args == error_mark_node)
4734 return error_mark_node;
4736 if (TREE_CODE (instance) == OFFSET_REF)
4737 instance = resolve_offset_ref (instance);
4738 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4739 instance = convert_from_reference (instance);
4740 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4741 instance_ptr = build_this (instance);
4743 if (!BASELINK_P (fns))
4745 call = build_field_call (instance_ptr, fns, args);
4748 error ("call to non-function `%D'", fns);
4749 return error_mark_node;
4752 if (!conversion_path)
4753 conversion_path = BASELINK_BINFO (fns);
4754 access_binfo = BASELINK_ACCESS_BINFO (fns);
4755 optype = BASELINK_OPTYPE (fns);
4756 fns = BASELINK_FUNCTIONS (fns);
4758 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4760 explicit_targs = TREE_OPERAND (fns, 1);
4761 fns = TREE_OPERAND (fns, 0);
4765 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
4766 || TREE_CODE (fns) == TEMPLATE_DECL
4767 || TREE_CODE (fns) == OVERLOAD,
4770 /* XXX this should be handled before we get here. */
4771 if (! IS_AGGR_TYPE (basetype))
4773 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4774 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4775 fns, instance, basetype);
4777 return error_mark_node;
4780 fn = get_first_fn (fns);
4781 name = DECL_NAME (fn);
4783 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4785 /* Callers should explicitly indicate whether they want to construct
4786 the complete object or just the part without virtual bases. */
4787 my_friendly_assert (name != ctor_identifier, 20000408);
4788 /* Similarly for destructors. */
4789 my_friendly_assert (name != dtor_identifier, 20000408);
4792 /* It's OK to call destructors on cv-qualified objects. Therefore,
4793 convert the INSTANCE_PTR to the unqualified type, if necessary. */
4794 if (DECL_DESTRUCTOR_P (fn))
4796 tree type = build_pointer_type (basetype);
4797 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
4798 instance_ptr = build1 (NOP_EXPR, type, instance_ptr);
4801 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
4802 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
4804 for (fn = fns; fn; fn = OVL_NEXT (fn))
4806 tree t = OVL_CURRENT (fn);
4809 /* We can end up here for copy-init of same or base class. */
4810 if ((flags & LOOKUP_ONLYCONVERTING)
4811 && DECL_NONCONVERTING_P (t))
4814 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
4815 this_arglist = mem_args;
4817 this_arglist = args;
4819 if (TREE_CODE (t) == TEMPLATE_DECL)
4821 /* A member template. */
4823 add_template_candidate (candidates, t,
4826 this_arglist, optype,
4832 else if (! template_only)
4833 candidates = add_function_candidate (candidates, t,
4841 if (! any_viable (candidates))
4843 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
4844 if (flags & LOOKUP_SPECULATIVELY)
4846 if (!COMPLETE_TYPE_P (basetype))
4847 cxx_incomplete_type_error (instance_ptr, basetype);
4853 pretty_name = name_as_c_string (name, basetype, &free_p);
4854 error ("no matching function for call to `%T::%s(%A)%#V'",
4855 basetype, pretty_name, user_args,
4856 TREE_TYPE (TREE_TYPE (instance_ptr)));
4860 print_z_candidates (candidates);
4861 return error_mark_node;
4863 candidates = splice_viable (candidates);
4864 cand = tourney (candidates);
4871 pretty_name = name_as_c_string (name, basetype, &free_p);
4872 error ("call of overloaded `%s(%A)' is ambiguous", pretty_name,
4874 print_z_candidates (candidates);
4877 return error_mark_node;
4880 if (DECL_PURE_VIRTUAL_P (cand->fn)
4881 && instance == current_class_ref
4882 && (DECL_CONSTRUCTOR_P (current_function_decl)
4883 || DECL_DESTRUCTOR_P (current_function_decl))
4884 && ! (flags & LOOKUP_NONVIRTUAL)
4885 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
4886 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
4887 "abstract virtual `%#D' called from constructor"
4888 : "abstract virtual `%#D' called from destructor"),
4890 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
4891 && is_dummy_object (instance_ptr))
4893 error ("cannot call member function `%D' without object", cand->fn);
4894 return error_mark_node;
4897 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
4898 && resolves_to_fixed_type_p (instance, 0))
4899 flags |= LOOKUP_NONVIRTUAL;
4901 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
4902 call = build_over_call (cand, flags);
4905 call = build_over_call (cand, flags);
4906 /* In an expression of the form `a->f()' where `f' turns out to
4907 be a static member function, `a' is none-the-less evaluated. */
4908 if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
4909 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
4915 /* Returns true iff standard conversion sequence ICS1 is a proper
4916 subsequence of ICS2. */
4919 is_subseq (tree ics1, tree ics2)
4921 /* We can assume that a conversion of the same code
4922 between the same types indicates a subsequence since we only get
4923 here if the types we are converting from are the same. */
4925 while (TREE_CODE (ics1) == RVALUE_CONV
4926 || TREE_CODE (ics1) == LVALUE_CONV)
4927 ics1 = TREE_OPERAND (ics1, 0);
4931 while (TREE_CODE (ics2) == RVALUE_CONV
4932 || TREE_CODE (ics2) == LVALUE_CONV)
4933 ics2 = TREE_OPERAND (ics2, 0);
4935 if (TREE_CODE (ics2) == USER_CONV
4936 || TREE_CODE (ics2) == AMBIG_CONV
4937 || TREE_CODE (ics2) == IDENTITY_CONV)
4938 /* At this point, ICS1 cannot be a proper subsequence of
4939 ICS2. We can get a USER_CONV when we are comparing the
4940 second standard conversion sequence of two user conversion
4944 ics2 = TREE_OPERAND (ics2, 0);
4946 if (TREE_CODE (ics2) == TREE_CODE (ics1)
4947 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
4948 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
4949 TREE_TYPE (TREE_OPERAND (ics1, 0))))
4954 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
4955 be any _TYPE nodes. */
4958 is_properly_derived_from (tree derived, tree base)
4960 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
4961 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
4964 /* We only allow proper derivation here. The DERIVED_FROM_P macro
4965 considers every class derived from itself. */
4966 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
4967 && DERIVED_FROM_P (base, derived));
4970 /* We build the ICS for an implicit object parameter as a pointer
4971 conversion sequence. However, such a sequence should be compared
4972 as if it were a reference conversion sequence. If ICS is the
4973 implicit conversion sequence for an implicit object parameter,
4974 modify it accordingly. */
4977 maybe_handle_implicit_object (tree *ics)
4979 if (ICS_THIS_FLAG (*ics))
4981 /* [over.match.funcs]
4983 For non-static member functions, the type of the
4984 implicit object parameter is "reference to cv X"
4985 where X is the class of which the function is a
4986 member and cv is the cv-qualification on the member
4987 function declaration. */
4989 tree reference_type;
4991 /* The `this' parameter is a pointer to a class type. Make the
4992 implict conversion talk about a reference to that same class
4994 reference_type = TREE_TYPE (TREE_TYPE (*ics));
4995 reference_type = build_reference_type (reference_type);
4997 if (TREE_CODE (t) == QUAL_CONV)
4998 t = TREE_OPERAND (t, 0);
4999 if (TREE_CODE (t) == PTR_CONV)
5000 t = TREE_OPERAND (t, 0);
5001 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5002 t = direct_reference_binding (reference_type, t);
5007 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5008 and return the type to which the reference refers. Otherwise,
5009 leave *ICS unchanged and return NULL_TREE. */
5012 maybe_handle_ref_bind (tree *ics)
5014 if (TREE_CODE (*ics) == REF_BIND)
5016 tree old_ics = *ics;
5017 tree type = TREE_TYPE (TREE_TYPE (old_ics));
5018 *ics = TREE_OPERAND (old_ics, 0);
5019 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5020 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5027 /* Compare two implicit conversion sequences according to the rules set out in
5028 [over.ics.rank]. Return values:
5030 1: ics1 is better than ics2
5031 -1: ics2 is better than ics1
5032 0: ics1 and ics2 are indistinguishable */
5035 compare_ics (tree ics1, tree ics2)
5041 tree deref_from_type1 = NULL_TREE;
5042 tree deref_from_type2 = NULL_TREE;
5043 tree deref_to_type1 = NULL_TREE;
5044 tree deref_to_type2 = NULL_TREE;
5047 /* REF_BINDING is nonzero if the result of the conversion sequence
5048 is a reference type. In that case TARGET_TYPE is the
5049 type referred to by the reference. */
5053 /* Handle implicit object parameters. */
5054 maybe_handle_implicit_object (&ics1);
5055 maybe_handle_implicit_object (&ics2);
5057 /* Handle reference parameters. */
5058 target_type1 = maybe_handle_ref_bind (&ics1);
5059 target_type2 = maybe_handle_ref_bind (&ics2);
5063 When comparing the basic forms of implicit conversion sequences (as
5064 defined in _over.best.ics_)
5066 --a standard conversion sequence (_over.ics.scs_) is a better
5067 conversion sequence than a user-defined conversion sequence
5068 or an ellipsis conversion sequence, and
5070 --a user-defined conversion sequence (_over.ics.user_) is a
5071 better conversion sequence than an ellipsis conversion sequence
5072 (_over.ics.ellipsis_). */
5073 rank1 = ICS_RANK (ics1);
5074 rank2 = ICS_RANK (ics2);
5078 else if (rank1 < rank2)
5081 if (rank1 == BAD_RANK)
5083 /* XXX Isn't this an extension? */
5084 /* Both ICS are bad. We try to make a decision based on what
5085 would have happenned if they'd been good. */
5086 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5087 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5089 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5090 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5093 /* We couldn't make up our minds; try to figure it out below. */
5096 if (ICS_ELLIPSIS_FLAG (ics1))
5097 /* Both conversions are ellipsis conversions. */
5100 /* User-defined conversion sequence U1 is a better conversion sequence
5101 than another user-defined conversion sequence U2 if they contain the
5102 same user-defined conversion operator or constructor and if the sec-
5103 ond standard conversion sequence of U1 is better than the second
5104 standard conversion sequence of U2. */
5106 if (ICS_USER_FLAG (ics1))
5110 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5111 if (TREE_CODE (t1) == AMBIG_CONV)
5113 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5114 if (TREE_CODE (t2) == AMBIG_CONV)
5117 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5120 /* We can just fall through here, after setting up
5121 FROM_TYPE1 and FROM_TYPE2. */
5122 from_type1 = TREE_TYPE (t1);
5123 from_type2 = TREE_TYPE (t2);
5127 /* We're dealing with two standard conversion sequences.
5131 Standard conversion sequence S1 is a better conversion
5132 sequence than standard conversion sequence S2 if
5134 --S1 is a proper subsequence of S2 (comparing the conversion
5135 sequences in the canonical form defined by _over.ics.scs_,
5136 excluding any Lvalue Transformation; the identity
5137 conversion sequence is considered to be a subsequence of
5138 any non-identity conversion sequence */
5141 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5142 from_type1 = TREE_OPERAND (from_type1, 0);
5143 from_type1 = TREE_TYPE (from_type1);
5146 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5147 from_type2 = TREE_OPERAND (from_type2, 0);
5148 from_type2 = TREE_TYPE (from_type2);
5151 if (same_type_p (from_type1, from_type2))
5153 if (is_subseq (ics1, ics2))
5155 if (is_subseq (ics2, ics1))
5158 /* Otherwise, one sequence cannot be a subsequence of the other; they
5159 don't start with the same type. This can happen when comparing the
5160 second standard conversion sequence in two user-defined conversion
5167 --the rank of S1 is better than the rank of S2 (by the rules
5170 Standard conversion sequences are ordered by their ranks: an Exact
5171 Match is a better conversion than a Promotion, which is a better
5172 conversion than a Conversion.
5174 Two conversion sequences with the same rank are indistinguishable
5175 unless one of the following rules applies:
5177 --A conversion that is not a conversion of a pointer, or pointer
5178 to member, to bool is better than another conversion that is such
5181 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5182 so that we do not have to check it explicitly. */
5183 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5185 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5188 to_type1 = TREE_TYPE (ics1);
5189 to_type2 = TREE_TYPE (ics2);
5191 if (TYPE_PTR_P (from_type1)
5192 && TYPE_PTR_P (from_type2)
5193 && TYPE_PTR_P (to_type1)
5194 && TYPE_PTR_P (to_type2))
5196 deref_from_type1 = TREE_TYPE (from_type1);
5197 deref_from_type2 = TREE_TYPE (from_type2);
5198 deref_to_type1 = TREE_TYPE (to_type1);
5199 deref_to_type2 = TREE_TYPE (to_type2);
5201 /* The rules for pointers to members A::* are just like the rules
5202 for pointers A*, except opposite: if B is derived from A then
5203 A::* converts to B::*, not vice versa. For that reason, we
5204 switch the from_ and to_ variables here. */
5205 else if (TYPE_PTRMEM_P (from_type1)
5206 && TYPE_PTRMEM_P (from_type2)
5207 && TYPE_PTRMEM_P (to_type1)
5208 && TYPE_PTRMEM_P (to_type2))
5210 deref_to_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type1));
5211 deref_to_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (from_type2));
5212 deref_from_type1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type1));
5213 deref_from_type2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (to_type2));
5215 else if (TYPE_PTRMEMFUNC_P (from_type1)
5216 && TYPE_PTRMEMFUNC_P (from_type2)
5217 && TYPE_PTRMEMFUNC_P (to_type1)
5218 && TYPE_PTRMEMFUNC_P (to_type2))
5220 deref_to_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type1);
5221 deref_to_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (from_type2);
5222 deref_from_type1 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type1);
5223 deref_from_type2 = TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type2);
5226 if (deref_from_type1 != NULL_TREE
5227 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5228 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5230 /* This was one of the pointer or pointer-like conversions.
5234 --If class B is derived directly or indirectly from class A,
5235 conversion of B* to A* is better than conversion of B* to
5236 void*, and conversion of A* to void* is better than
5237 conversion of B* to void*. */
5238 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5239 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5241 if (is_properly_derived_from (deref_from_type1,
5244 else if (is_properly_derived_from (deref_from_type2,
5248 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5249 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5251 if (same_type_p (deref_from_type1, deref_from_type2))
5253 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5255 if (is_properly_derived_from (deref_from_type1,
5259 /* We know that DEREF_TO_TYPE1 is `void' here. */
5260 else if (is_properly_derived_from (deref_from_type1,
5265 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5266 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5270 --If class B is derived directly or indirectly from class A
5271 and class C is derived directly or indirectly from B,
5273 --conversion of C* to B* is better than conversion of C* to
5276 --conversion of B* to A* is better than conversion of C* to
5278 if (same_type_p (deref_from_type1, deref_from_type2))
5280 if (is_properly_derived_from (deref_to_type1,
5283 else if (is_properly_derived_from (deref_to_type2,
5287 else if (same_type_p (deref_to_type1, deref_to_type2))
5289 if (is_properly_derived_from (deref_from_type2,
5292 else if (is_properly_derived_from (deref_from_type1,
5298 else if (CLASS_TYPE_P (non_reference (from_type1))
5299 && same_type_p (from_type1, from_type2))
5301 tree from = non_reference (from_type1);
5305 --binding of an expression of type C to a reference of type
5306 B& is better than binding an expression of type C to a
5307 reference of type A&
5309 --conversion of C to B is better than conversion of C to A, */
5310 if (is_properly_derived_from (from, to_type1)
5311 && is_properly_derived_from (from, to_type2))
5313 if (is_properly_derived_from (to_type1, to_type2))
5315 else if (is_properly_derived_from (to_type2, to_type1))
5319 else if (CLASS_TYPE_P (non_reference (to_type1))
5320 && same_type_p (to_type1, to_type2))
5322 tree to = non_reference (to_type1);
5326 --binding of an expression of type B to a reference of type
5327 A& is better than binding an expression of type C to a
5328 reference of type A&,
5330 --onversion of B to A is better than conversion of C to A */
5331 if (is_properly_derived_from (from_type1, to)
5332 && is_properly_derived_from (from_type2, to))
5334 if (is_properly_derived_from (from_type2, from_type1))
5336 else if (is_properly_derived_from (from_type1, from_type2))
5343 --S1 and S2 differ only in their qualification conversion and yield
5344 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5345 qualification signature of type T1 is a proper subset of the cv-
5346 qualification signature of type T2 */
5347 if (TREE_CODE (ics1) == QUAL_CONV
5348 && TREE_CODE (ics2) == QUAL_CONV
5349 && same_type_p (from_type1, from_type2))
5350 return comp_cv_qual_signature (to_type1, to_type2);
5354 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5355 types to which the references refer are the same type except for
5356 top-level cv-qualifiers, and the type to which the reference
5357 initialized by S2 refers is more cv-qualified than the type to
5358 which the reference initialized by S1 refers */
5360 if (target_type1 && target_type2
5361 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5362 return comp_cv_qualification (target_type2, target_type1);
5364 /* Neither conversion sequence is better than the other. */
5368 /* The source type for this standard conversion sequence. */
5371 source_type (tree t)
5373 for (;; t = TREE_OPERAND (t, 0))
5375 if (TREE_CODE (t) == USER_CONV
5376 || TREE_CODE (t) == AMBIG_CONV
5377 || TREE_CODE (t) == IDENTITY_CONV)
5378 return TREE_TYPE (t);
5383 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5384 a pointer to LOSER and re-running joust to produce the warning if WINNER
5385 is actually used. */
5388 add_warning (struct z_candidate *winner, struct z_candidate *loser)
5390 winner->warnings = tree_cons (NULL_TREE,
5391 build_zc_wrapper (loser),
5395 /* Returns true iff functions are equivalent. Equivalent functions are
5396 not '==' only if one is a function-local extern function or if
5397 both are extern "C". */
5400 equal_functions (tree fn1, tree fn2)
5402 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
5403 || DECL_EXTERN_C_FUNCTION_P (fn1))
5404 return decls_match (fn1, fn2);
5408 /* Compare two candidates for overloading as described in
5409 [over.match.best]. Return values:
5411 1: cand1 is better than cand2
5412 -1: cand2 is better than cand1
5413 0: cand1 and cand2 are indistinguishable */
5416 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5419 int i, off1 = 0, off2 = 0, len;
5421 /* Candidates that involve bad conversions are always worse than those
5423 if (cand1->viable > cand2->viable)
5425 if (cand1->viable < cand2->viable)
5428 /* If we have two pseudo-candidates for conversions to the same type,
5429 or two candidates for the same function, arbitrarily pick one. */
5430 if (cand1->fn == cand2->fn
5431 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5434 /* a viable function F1
5435 is defined to be a better function than another viable function F2 if
5436 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5437 ICSi(F2), and then */
5439 /* for some argument j, ICSj(F1) is a better conversion sequence than
5442 /* For comparing static and non-static member functions, we ignore
5443 the implicit object parameter of the non-static function. The
5444 standard says to pretend that the static function has an object
5445 parm, but that won't work with operator overloading. */
5446 len = TREE_VEC_LENGTH (cand1->convs);
5447 if (len != TREE_VEC_LENGTH (cand2->convs))
5449 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5450 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5452 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5453 && DECL_STATIC_FUNCTION_P (cand2->fn))
5462 for (i = 0; i < len; ++i)
5464 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5465 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5466 int comp = compare_ics (t1, t2);
5471 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5472 && TREE_CODE (t1) == STD_CONV
5473 && TREE_CODE (t2) == STD_CONV
5474 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5475 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5476 && (TYPE_PRECISION (TREE_TYPE (t1))
5477 == TYPE_PRECISION (TREE_TYPE (t2)))
5478 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5479 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5482 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5484 struct z_candidate *w, *l;
5486 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5487 w = cand1, l = cand2;
5489 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5490 w = cand2, l = cand1;
5494 warning ("passing `%T' chooses `%T' over `%T'",
5495 type, type1, type2);
5496 warning (" in call to `%D'", w->fn);
5502 if (winner && comp != winner)
5511 /* warn about confusing overload resolution for user-defined conversions,
5512 either between a constructor and a conversion op, or between two
5514 if (winner && cand1->second_conv
5515 && ((DECL_CONSTRUCTOR_P (cand1->fn)
5516 != DECL_CONSTRUCTOR_P (cand2->fn))
5517 /* Don't warn if the two conv ops convert to the same type... */
5518 || (! DECL_CONSTRUCTOR_P (cand1->fn)
5519 && ! same_type_p (TREE_TYPE (TREE_TYPE (cand1->fn)),
5520 TREE_TYPE (TREE_TYPE (cand2->fn))))))
5522 int comp = compare_ics (cand1->second_conv, cand2->second_conv);
5525 struct z_candidate *w, *l;
5528 w = cand1, l = cand2;
5530 w = cand2, l = cand1;
5531 if (DECL_CONTEXT (cand1->fn) == DECL_CONTEXT (cand2->fn)
5532 && ! DECL_CONSTRUCTOR_P (cand1->fn)
5533 && ! DECL_CONSTRUCTOR_P (cand2->fn)
5534 && (convn = standard_conversion
5535 (TREE_TYPE (TREE_TYPE (l->fn)),
5536 TREE_TYPE (TREE_TYPE (w->fn)), NULL_TREE))
5537 && TREE_CODE (convn) == QUAL_CONV)
5538 /* Don't complain about `operator char *()' beating
5539 `operator const char *() const'. */;
5542 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5543 if (! DECL_CONSTRUCTOR_P (w->fn))
5544 source = TREE_TYPE (source);
5545 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5546 warning (" for conversion from `%T' to `%T'",
5547 source, TREE_TYPE (w->second_conv));
5548 warning (" because conversion sequence for the argument is better");
5559 F1 is a non-template function and F2 is a template function
5562 if (! cand1->template && cand2->template)
5564 else if (cand1->template && ! cand2->template)
5568 F1 and F2 are template functions and the function template for F1 is
5569 more specialized than the template for F2 according to the partial
5572 if (cand1->template && cand2->template)
5574 winner = more_specialized
5575 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5577 /* Tell the deduction code how many real function arguments
5578 we saw, not counting the implicit 'this' argument. But,
5579 add_function_candidate() suppresses the "this" argument
5582 [temp.func.order]: The presence of unused ellipsis and default
5583 arguments has no effect on the partial ordering of function
5585 TREE_VEC_LENGTH (cand1->convs)
5586 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5587 - DECL_CONSTRUCTOR_P (cand1->fn)));
5594 the context is an initialization by user-defined conversion (see
5595 _dcl.init_ and _over.match.user_) and the standard conversion
5596 sequence from the return type of F1 to the destination type (i.e.,
5597 the type of the entity being initialized) is a better conversion
5598 sequence than the standard conversion sequence from the return type
5599 of F2 to the destination type. */
5601 if (cand1->second_conv)
5603 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5608 /* Check whether we can discard a builtin candidate, either because we
5609 have two identical ones or matching builtin and non-builtin candidates.
5611 (Pedantically in the latter case the builtin which matched the user
5612 function should not be added to the overload set, but we spot it here.
5615 ... the builtin candidates include ...
5616 - do not have the same parameter type list as any non-template
5617 non-member candidate. */
5619 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5620 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5622 for (i = 0; i < len; ++i)
5623 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5624 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5626 if (i == TREE_VEC_LENGTH (cand1->convs))
5628 if (cand1->fn == cand2->fn)
5629 /* Two built-in candidates; arbitrarily pick one. */
5631 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5632 /* cand1 is built-in; prefer cand2. */
5635 /* cand2 is built-in; prefer cand1. */
5640 /* If the two functions are the same (this can happen with declarations
5641 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5642 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5643 && equal_functions (cand1->fn, cand2->fn))
5648 /* Extension: If the worst conversion for one candidate is worse than the
5649 worst conversion for the other, take the first. */
5652 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5653 struct z_candidate *w = 0, *l = 0;
5655 for (i = 0; i < len; ++i)
5657 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5658 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5659 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5660 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5663 winner = 1, w = cand1, l = cand2;
5665 winner = -1, w = cand2, l = cand1;
5670 pedwarn ("choosing `%D' over `%D'", w->fn, l->fn);
5672 " because worst conversion for the former is better than worst conversion for the latter");
5680 my_friendly_assert (!winner, 20010121);
5684 /* Given a list of candidates for overloading, find the best one, if any.
5685 This algorithm has a worst case of O(2n) (winner is last), and a best
5686 case of O(n/2) (totally ambiguous); much better than a sorting
5689 static struct z_candidate *
5690 tourney (struct z_candidate *candidates)
5692 struct z_candidate *champ = candidates, *challenger;
5694 int champ_compared_to_predecessor = 0;
5696 /* Walk through the list once, comparing each current champ to the next
5697 candidate, knocking out a candidate or two with each comparison. */
5699 for (challenger = champ->next; challenger; )
5701 fate = joust (champ, challenger, 0);
5703 challenger = challenger->next;
5708 champ = challenger->next;
5711 champ_compared_to_predecessor = 0;
5716 champ_compared_to_predecessor = 1;
5719 challenger = champ->next;
5723 /* Make sure the champ is better than all the candidates it hasn't yet
5724 been compared to. */
5726 for (challenger = candidates;
5728 && !(champ_compared_to_predecessor && challenger->next == champ);
5729 challenger = challenger->next)
5731 fate = joust (champ, challenger, 0);
5739 /* Returns nonzero if things of type FROM can be converted to TO. */
5742 can_convert (tree to, tree from)
5744 return can_convert_arg (to, from, NULL_TREE);
5747 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
5750 can_convert_arg (tree to, tree from, tree arg)
5752 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5753 return (t && ! ICS_BAD_FLAG (t));
5756 /* Like can_convert_arg, but allows dubious conversions as well. */
5759 can_convert_arg_bad (tree to, tree from, tree arg)
5761 return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
5764 /* Convert EXPR to TYPE. Return the converted expression.
5766 Note that we allow bad conversions here because by the time we get to
5767 this point we are committed to doing the conversion. If we end up
5768 doing a bad conversion, convert_like will complain. */
5771 perform_implicit_conversion (tree type, tree expr)
5775 if (error_operand_p (expr))
5776 return error_mark_node;
5777 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5781 error ("could not convert `%E' to `%T'", expr, type);
5782 return error_mark_node;
5785 return convert_like (conv, expr);
5788 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
5789 initializing a variable of that TYPE. Return the converted
5793 initialize_reference (tree type, tree expr)
5797 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
5798 if (!conv || ICS_BAD_FLAG (conv))
5800 error ("could not convert `%E' to `%T'", expr, type);
5801 return error_mark_node;
5804 return convert_like (conv, expr);
5807 #include "gt-cp-call.h"