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, 2003, 2004 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"
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 /*issue_conversion_warnings=*/true)
52 #define convert_like_with_context(CONV, EXPR, FN, ARGNO) \
53 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
54 /*issue_conversion_warnings=*/true)
55 static tree convert_like_real (tree, tree, tree, int, int, bool);
56 static void op_error (enum tree_code, enum tree_code, tree, tree,
58 static tree build_object_call (tree, tree);
59 static tree resolve_args (tree);
60 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
61 static void print_z_candidate (const char *, struct z_candidate *);
62 static void print_z_candidates (struct z_candidate *);
63 static tree build_this (tree);
64 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
65 static bool any_strictly_viable (struct z_candidate *);
66 static struct z_candidate *add_template_candidate
67 (struct z_candidate **, tree, tree, tree, tree, tree,
68 tree, tree, int, unification_kind_t);
69 static struct z_candidate *add_template_candidate_real
70 (struct z_candidate **, tree, tree, tree, tree, tree,
71 tree, tree, int, tree, unification_kind_t);
72 static struct z_candidate *add_template_conv_candidate
73 (struct z_candidate **, tree, tree, tree, tree, tree, tree);
74 static void add_builtin_candidates
75 (struct z_candidate **, enum tree_code, enum tree_code,
77 static void add_builtin_candidate
78 (struct z_candidate **, enum tree_code, enum tree_code,
79 tree, tree, tree, tree *, tree *, int);
80 static bool is_complete (tree);
81 static void build_builtin_candidate
82 (struct z_candidate **, tree, tree, tree, tree *, tree *,
84 static struct z_candidate *add_conv_candidate
85 (struct z_candidate **, tree, tree, tree, tree, tree);
86 static struct z_candidate *add_function_candidate
87 (struct z_candidate **, tree, tree, tree, tree, tree, int);
88 static tree implicit_conversion (tree, tree, tree, int);
89 static tree standard_conversion (tree, tree, tree);
90 static tree reference_binding (tree, tree, tree, int);
91 static tree build_conv (enum tree_code, tree, tree);
92 static bool is_subseq (tree, tree);
93 static tree maybe_handle_ref_bind (tree *);
94 static void maybe_handle_implicit_object (tree *);
95 static struct z_candidate *add_candidate
96 (struct z_candidate **, tree, tree, tree, tree, tree, int);
97 static tree source_type (tree);
98 static void add_warning (struct z_candidate *, struct z_candidate *);
99 static bool reference_related_p (tree, tree);
100 static bool reference_compatible_p (tree, tree);
101 static tree convert_class_to_reference (tree, tree, tree);
102 static tree direct_reference_binding (tree, tree);
103 static bool promoted_arithmetic_type_p (tree);
104 static tree conditional_conversion (tree, tree);
105 static char *name_as_c_string (tree, tree, bool *);
106 static tree call_builtin_trap (void);
107 static tree prep_operand (tree);
108 static void add_candidates (tree, tree, tree, bool, tree, tree,
109 int, struct z_candidate **);
110 static tree merge_conversion_sequences (tree, tree);
111 static bool magic_varargs_p (tree);
114 build_vfield_ref (tree datum, tree type)
116 if (datum == error_mark_node)
117 return error_mark_node;
119 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
120 datum = convert_from_reference (datum);
122 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
123 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
124 datum = convert_to_base (datum, type, /*check_access=*/false);
126 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
127 datum, TYPE_VFIELD (type));
130 /* Returns nonzero iff the destructor name specified in NAME
131 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
135 check_dtor_name (tree basetype, tree name)
137 name = TREE_OPERAND (name, 0);
139 /* Just accept something we've already complained about. */
140 if (name == error_mark_node)
143 if (TREE_CODE (name) == TYPE_DECL)
144 name = TREE_TYPE (name);
145 else if (TYPE_P (name))
147 else if (TREE_CODE (name) == IDENTIFIER_NODE)
149 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
150 || (TREE_CODE (basetype) == ENUMERAL_TYPE
151 && name == TYPE_IDENTIFIER (basetype)))
154 name = get_type_value (name);
158 template <class T> struct S { ~S(); };
162 NAME will be a class template. */
163 else if (DECL_CLASS_TEMPLATE_P (name))
168 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
173 /* We want the address of a function or method. We avoid creating a
174 pointer-to-member function. */
177 build_addr_func (tree function)
179 tree type = TREE_TYPE (function);
181 /* We have to do these by hand to avoid real pointer to member
183 if (TREE_CODE (type) == METHOD_TYPE)
185 if (TREE_CODE (function) == OFFSET_REF)
187 tree object = build_address (TREE_OPERAND (function, 0));
188 return get_member_function_from_ptrfunc (&object,
189 TREE_OPERAND (function, 1));
191 function = build_address (function);
194 function = decay_conversion (function);
199 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
200 POINTER_TYPE to those. Note, pointer to member function types
201 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
204 build_call (tree function, tree parms)
206 int is_constructor = 0;
213 function = build_addr_func (function);
215 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
217 sorry ("unable to call pointer to member function here");
218 return error_mark_node;
221 fntype = TREE_TYPE (TREE_TYPE (function));
222 result_type = TREE_TYPE (fntype);
224 if (TREE_CODE (function) == ADDR_EXPR
225 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
226 decl = TREE_OPERAND (function, 0);
230 /* We check both the decl and the type; a function may be known not to
231 throw without being declared throw(). */
232 nothrow = ((decl && TREE_NOTHROW (decl))
233 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
235 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
236 current_function_returns_abnormally = 1;
238 if (decl && TREE_DEPRECATED (decl))
239 warn_deprecated_use (decl);
240 require_complete_eh_spec_types (fntype, decl);
242 if (decl && DECL_CONSTRUCTOR_P (decl))
245 if (decl && ! TREE_USED (decl))
247 /* We invoke build_call directly for several library functions.
248 These may have been declared normally if we're building libgcc,
249 so we can't just check DECL_ARTIFICIAL. */
250 if (DECL_ARTIFICIAL (decl)
251 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
257 /* Don't pass empty class objects by value. This is useful
258 for tags in STL, which are used to control overload resolution.
259 We don't need to handle other cases of copying empty classes. */
260 if (! decl || ! DECL_BUILT_IN (decl))
261 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
262 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
263 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
265 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
266 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
267 TREE_VALUE (tmp), t);
270 function = build (CALL_EXPR, result_type, function, parms);
271 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
272 TREE_NOTHROW (function) = nothrow;
277 /* Build something of the form ptr->method (args)
278 or object.method (args). This can also build
279 calls to constructors, and find friends.
281 Member functions always take their class variable
284 INSTANCE is a class instance.
286 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
288 PARMS help to figure out what that NAME really refers to.
290 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
291 down to the real instance type to use for access checking. We need this
292 information to get protected accesses correct.
294 FLAGS is the logical disjunction of zero or more LOOKUP_
295 flags. See cp-tree.h for more info.
297 If this is all OK, calls build_function_call with the resolved
300 This function must also handle being called to perform
301 initialization, promotion/coercion of arguments, and
302 instantiation of default parameters.
304 Note that NAME may refer to an instance variable name. If
305 `operator()()' is defined for the type of that field, then we return
308 /* New overloading code. */
310 struct z_candidate GTY(()) {
311 /* The FUNCTION_DECL that will be called if this candidate is
312 selected by overload resolution. */
314 /* The arguments to use when calling this function. */
316 /* The implicit conversion sequences for each of the arguments to
319 /* If FN is a user-defined conversion, the standard conversion
320 sequence from the type returned by FN to the desired destination
324 /* If FN is a member function, the binfo indicating the path used to
325 qualify the name of FN at the call site. This path is used to
326 determine whether or not FN is accessible if it is selected by
327 overload resolution. The DECL_CONTEXT of FN will always be a
328 (possibly improper) base of this binfo. */
330 /* If FN is a non-static member function, the binfo indicating the
331 subobject to which the `this' pointer should be converted if FN
332 is selected by overload resolution. The type pointed to the by
333 the `this' pointer must correspond to the most derived class
334 indicated by the CONVERSION_PATH. */
335 tree conversion_path;
338 struct z_candidate *next;
341 #define IDENTITY_RANK 0
347 #define ELLIPSIS_RANK 6
350 #define ICS_RANK(NODE) \
351 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
352 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
353 : ICS_USER_FLAG (NODE) ? USER_RANK \
354 : ICS_STD_RANK (NODE))
356 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
358 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
359 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
360 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
361 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
363 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
364 should be created to hold the result of the conversion. */
365 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
367 /* TRUE in an IDENTITY_CONV or BASE_CONV if the copy constructor must
368 be accessible, even though it is not being used. */
369 #define CHECK_COPY_CONSTRUCTOR_P(NODE) TREE_LANG_FLAG_5 (NODE)
371 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
372 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
375 null_ptr_cst_p (tree t)
379 A null pointer constant is an integral constant expression
380 (_expr.const_) rvalue of integer type that evaluates to zero. */
382 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
388 /* Returns nonzero if PARMLIST consists of only default parms and/or
392 sufficient_parms_p (tree parmlist)
394 for (; parmlist && parmlist != void_list_node;
395 parmlist = TREE_CHAIN (parmlist))
396 if (!TREE_PURPOSE (parmlist))
402 build_conv (enum tree_code code, tree type, tree from)
405 int rank = ICS_STD_RANK (from);
407 /* We can't use buildl1 here because CODE could be USER_CONV, which
408 takes two arguments. In that case, the caller is responsible for
409 filling in the second argument. */
410 t = make_node (code);
411 TREE_TYPE (t) = type;
412 TREE_OPERAND (t, 0) = from;
425 if (rank < EXACT_RANK)
431 ICS_STD_RANK (t) = rank;
432 ICS_USER_FLAG (t) = (code == USER_CONV || ICS_USER_FLAG (from));
433 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
438 strip_top_quals (tree t)
440 if (TREE_CODE (t) == ARRAY_TYPE)
442 return cp_build_qualified_type (t, 0);
445 /* Returns the standard conversion path (see [conv]) from type FROM to type
446 TO, if any. For proper handling of null pointer constants, you must
447 also pass the expression EXPR to convert from. */
450 standard_conversion (tree to, tree from, tree expr)
452 enum tree_code fcode, tcode;
454 bool fromref = false;
456 to = non_reference (to);
457 if (TREE_CODE (from) == REFERENCE_TYPE)
460 from = TREE_TYPE (from);
462 to = strip_top_quals (to);
463 from = strip_top_quals (from);
465 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
466 && expr && type_unknown_p (expr))
468 expr = instantiate_type (to, expr, tf_conv);
469 if (expr == error_mark_node)
471 from = TREE_TYPE (expr);
474 fcode = TREE_CODE (from);
475 tcode = TREE_CODE (to);
477 conv = build1 (IDENTITY_CONV, from, expr);
479 if (fcode == FUNCTION_TYPE)
481 from = build_pointer_type (from);
482 fcode = TREE_CODE (from);
483 conv = build_conv (LVALUE_CONV, from, conv);
485 else if (fcode == ARRAY_TYPE)
487 from = build_pointer_type (TREE_TYPE (from));
488 fcode = TREE_CODE (from);
489 conv = build_conv (LVALUE_CONV, from, conv);
491 else if (fromref || (expr && lvalue_p (expr)))
492 conv = build_conv (RVALUE_CONV, from, conv);
494 /* Allow conversion between `__complex__' data types. */
495 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
497 /* The standard conversion sequence to convert FROM to TO is
498 the standard conversion sequence to perform componentwise
500 tree part_conv = standard_conversion
501 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
505 conv = build_conv (TREE_CODE (part_conv), to, conv);
506 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
514 if (same_type_p (from, to))
517 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
518 && expr && null_ptr_cst_p (expr))
519 conv = build_conv (STD_CONV, to, conv);
520 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE
521 && TREE_CODE (TREE_TYPE (to)) == VECTOR_TYPE
522 && TREE_CODE (TREE_TYPE (from)) == VECTOR_TYPE
523 && ((*targetm.vector_opaque_p) (TREE_TYPE (to))
524 || (*targetm.vector_opaque_p) (TREE_TYPE (from))))
525 conv = build_conv (STD_CONV, to, conv);
526 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
527 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
529 /* For backwards brain damage compatibility, allow interconversion of
530 pointers and integers with a pedwarn. */
531 conv = build_conv (STD_CONV, to, conv);
532 ICS_BAD_FLAG (conv) = 1;
534 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE)
536 /* For backwards brain damage compatibility, allow interconversion of
537 enums and integers with a pedwarn. */
538 conv = build_conv (STD_CONV, to, conv);
539 ICS_BAD_FLAG (conv) = 1;
541 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
542 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
547 if (tcode == POINTER_TYPE
548 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
551 else if (VOID_TYPE_P (TREE_TYPE (to))
552 && !TYPE_PTRMEM_P (from)
553 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
555 from = build_pointer_type
556 (cp_build_qualified_type (void_type_node,
557 cp_type_quals (TREE_TYPE (from))));
558 conv = build_conv (PTR_CONV, from, conv);
560 else if (TYPE_PTRMEM_P (from))
562 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
563 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
565 if (DERIVED_FROM_P (fbase, tbase)
566 && (same_type_ignoring_top_level_qualifiers_p
567 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
568 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
570 from = build_ptrmem_type (tbase,
571 TYPE_PTRMEM_POINTED_TO_TYPE (from));
572 conv = build_conv (PMEM_CONV, from, conv);
575 else if (IS_AGGR_TYPE (TREE_TYPE (from))
576 && IS_AGGR_TYPE (TREE_TYPE (to))
579 An rvalue of type "pointer to cv D," where D is a
580 class type, can be converted to an rvalue of type
581 "pointer to cv B," where B is a base class (clause
582 _class.derived_) of D. If B is an inaccessible
583 (clause _class.access_) or ambiguous
584 (_class.member.lookup_) base class of D, a program
585 that necessitates this conversion is ill-formed. */
586 /* Therefore, we use DERIVED_FROM_P, and not
587 ACESSIBLY_UNIQUELY_DERIVED_FROM_P, in this test. */
588 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
591 cp_build_qualified_type (TREE_TYPE (to),
592 cp_type_quals (TREE_TYPE (from)));
593 from = build_pointer_type (from);
594 conv = build_conv (PTR_CONV, from, conv);
597 if (tcode == POINTER_TYPE)
599 to_pointee = TREE_TYPE (to);
600 from_pointee = TREE_TYPE (from);
604 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
605 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
608 if (same_type_p (from, to))
610 else if (comp_ptr_ttypes (to_pointee, from_pointee))
611 conv = build_conv (QUAL_CONV, to, conv);
612 else if (expr && string_conv_p (to, expr, 0))
613 /* converting from string constant to char *. */
614 conv = build_conv (QUAL_CONV, to, conv);
615 else if (ptr_reasonably_similar (to_pointee, from_pointee))
617 conv = build_conv (PTR_CONV, to, conv);
618 ICS_BAD_FLAG (conv) = 1;
625 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
627 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
628 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
629 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
630 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
632 if (!DERIVED_FROM_P (fbase, tbase)
633 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
634 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
635 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
636 || cp_type_quals (fbase) != cp_type_quals (tbase))
639 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
640 from = build_method_type_directly (from,
642 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
643 from = build_ptrmemfunc_type (build_pointer_type (from));
644 conv = build_conv (PMEM_CONV, from, conv);
646 else if (tcode == BOOLEAN_TYPE)
650 An rvalue of arithmetic, enumeration, pointer, or pointer to
651 member type can be converted to an rvalue of type bool. */
652 if (ARITHMETIC_TYPE_P (from)
653 || fcode == ENUMERAL_TYPE
654 || fcode == POINTER_TYPE
655 || TYPE_PTR_TO_MEMBER_P (from))
657 conv = build_conv (STD_CONV, to, conv);
658 if (fcode == POINTER_TYPE
659 || TYPE_PTRMEM_P (from)
660 || (TYPE_PTRMEMFUNC_P (from)
661 && ICS_STD_RANK (conv) < PBOOL_RANK))
662 ICS_STD_RANK (conv) = PBOOL_RANK;
668 /* We don't check for ENUMERAL_TYPE here because there are no standard
669 conversions to enum type. */
670 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
671 || tcode == REAL_TYPE)
673 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
675 conv = build_conv (STD_CONV, to, conv);
677 /* Give this a better rank if it's a promotion. */
678 if (same_type_p (to, type_promotes_to (from))
679 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
680 ICS_STD_RANK (conv) = PROMO_RANK;
682 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
683 && ((*targetm.vector_opaque_p) (from)
684 || (*targetm.vector_opaque_p) (to)))
685 return build_conv (STD_CONV, to, conv);
686 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
687 && is_properly_derived_from (from, to))
689 if (TREE_CODE (conv) == RVALUE_CONV)
690 conv = TREE_OPERAND (conv, 0);
691 conv = build_conv (BASE_CONV, to, conv);
692 /* The derived-to-base conversion indicates the initialization
693 of a parameter with base type from an object of a derived
694 type. A temporary object is created to hold the result of
696 NEED_TEMPORARY_P (conv) = 1;
704 /* Returns nonzero if T1 is reference-related to T2. */
707 reference_related_p (tree t1, tree t2)
709 t1 = TYPE_MAIN_VARIANT (t1);
710 t2 = TYPE_MAIN_VARIANT (t2);
714 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
715 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
717 return (same_type_p (t1, t2)
718 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
719 && DERIVED_FROM_P (t1, t2)));
722 /* Returns nonzero if T1 is reference-compatible with T2. */
725 reference_compatible_p (tree t1, tree t2)
729 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
730 reference-related to T2 and cv1 is the same cv-qualification as,
731 or greater cv-qualification than, cv2. */
732 return (reference_related_p (t1, t2)
733 && at_least_as_qualified_p (t1, t2));
736 /* Determine whether or not the EXPR (of class type S) can be
737 converted to T as in [over.match.ref]. */
740 convert_class_to_reference (tree t, tree s, tree expr)
746 struct z_candidate *candidates;
747 struct z_candidate *cand;
750 conversions = lookup_conversions (s);
756 Assuming that "cv1 T" is the underlying type of the reference
757 being initialized, and "cv S" is the type of the initializer
758 expression, with S a class type, the candidate functions are
761 --The conversion functions of S and its base classes are
762 considered. Those that are not hidden within S and yield type
763 "reference to cv2 T2", where "cv1 T" is reference-compatible
764 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
766 The argument list has one argument, which is the initializer
771 /* Conceptually, we should take the address of EXPR and put it in
772 the argument list. Unfortunately, however, that can result in
773 error messages, which we should not issue now because we are just
774 trying to find a conversion operator. Therefore, we use NULL,
775 cast to the appropriate type. */
776 arglist = build_int_2 (0, 0);
777 TREE_TYPE (arglist) = build_pointer_type (s);
778 arglist = build_tree_list (NULL_TREE, arglist);
780 reference_type = build_reference_type (t);
784 tree fns = TREE_VALUE (conversions);
786 for (; fns; fns = OVL_NEXT (fns))
788 tree f = OVL_CURRENT (fns);
789 tree t2 = TREE_TYPE (TREE_TYPE (f));
793 /* If this is a template function, try to get an exact
795 if (TREE_CODE (f) == TEMPLATE_DECL)
797 cand = add_template_candidate (&candidates,
803 TREE_PURPOSE (conversions),
809 /* Now, see if the conversion function really returns
810 an lvalue of the appropriate type. From the
811 point of view of unification, simply returning an
812 rvalue of the right type is good enough. */
814 t2 = TREE_TYPE (TREE_TYPE (f));
815 if (TREE_CODE (t2) != REFERENCE_TYPE
816 || !reference_compatible_p (t, TREE_TYPE (t2)))
818 candidates = candidates->next;
823 else if (TREE_CODE (t2) == REFERENCE_TYPE
824 && reference_compatible_p (t, TREE_TYPE (t2)))
825 cand = add_function_candidate (&candidates, f, s, arglist,
827 TREE_PURPOSE (conversions),
832 /* Build a standard conversion sequence indicating the
833 binding from the reference type returned by the
834 function to the desired REFERENCE_TYPE. */
836 = (direct_reference_binding
838 build1 (IDENTITY_CONV,
839 TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))),
841 ICS_BAD_FLAG (cand->second_conv)
842 |= ICS_BAD_FLAG (TREE_VEC_ELT (cand->convs, 0));
845 conversions = TREE_CHAIN (conversions);
848 candidates = splice_viable (candidates, pedantic, &any_viable_p);
849 /* If none of the conversion functions worked out, let our caller
854 cand = tourney (candidates);
858 /* Now that we know that this is the function we're going to use fix
859 the dummy first argument. */
860 cand->args = tree_cons (NULL_TREE,
862 TREE_CHAIN (cand->args));
864 /* Build a user-defined conversion sequence representing the
866 conv = build_conv (USER_CONV,
867 TREE_TYPE (TREE_TYPE (cand->fn)),
868 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
869 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
871 /* Merge it with the standard conversion sequence from the
872 conversion function's return type to the desired type. */
873 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
875 if (cand->viable == -1)
876 ICS_BAD_FLAG (conv) = 1;
878 return cand->second_conv;
881 /* A reference of the indicated TYPE is being bound directly to the
882 expression represented by the implicit conversion sequence CONV.
883 Return a conversion sequence for this binding. */
886 direct_reference_binding (tree type, tree conv)
890 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
891 my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
894 t = TREE_TYPE (type);
898 When a parameter of reference type binds directly
899 (_dcl.init.ref_) to an argument expression, the implicit
900 conversion sequence is the identity conversion, unless the
901 argument expression has a type that is a derived class of the
902 parameter type, in which case the implicit conversion sequence is
903 a derived-to-base Conversion.
905 If the parameter binds directly to the result of applying a
906 conversion function to the argument expression, the implicit
907 conversion sequence is a user-defined conversion sequence
908 (_over.ics.user_), with the second standard conversion sequence
909 either an identity conversion or, if the conversion function
910 returns an entity of a type that is a derived class of the
911 parameter type, a derived-to-base conversion. */
912 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
914 /* Represent the derived-to-base conversion. */
915 conv = build_conv (BASE_CONV, t, conv);
916 /* We will actually be binding to the base-class subobject in
917 the derived class, so we mark this conversion appropriately.
918 That way, convert_like knows not to generate a temporary. */
919 NEED_TEMPORARY_P (conv) = 0;
921 return build_conv (REF_BIND, type, conv);
924 /* Returns the conversion path from type FROM to reference type TO for
925 purposes of reference binding. For lvalue binding, either pass a
926 reference type to FROM or an lvalue expression to EXPR. If the
927 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
928 the conversion returned. */
931 reference_binding (tree rto, tree rfrom, tree expr, int flags)
933 tree conv = NULL_TREE;
934 tree to = TREE_TYPE (rto);
938 cp_lvalue_kind lvalue_p = clk_none;
940 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
942 expr = instantiate_type (to, expr, tf_none);
943 if (expr == error_mark_node)
945 from = TREE_TYPE (expr);
948 if (TREE_CODE (from) == REFERENCE_TYPE)
950 /* Anything with reference type is an lvalue. */
951 lvalue_p = clk_ordinary;
952 from = TREE_TYPE (from);
955 lvalue_p = real_lvalue_p (expr);
957 /* Figure out whether or not the types are reference-related and
958 reference compatible. We have do do this after stripping
959 references from FROM. */
960 related_p = reference_related_p (to, from);
961 compatible_p = reference_compatible_p (to, from);
963 if (lvalue_p && compatible_p)
967 If the initializer expression
969 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
970 is reference-compatible with "cv2 T2,"
972 the reference is bound directly to the initializer expression
974 conv = build1 (IDENTITY_CONV, from, expr);
975 conv = direct_reference_binding (rto, conv);
976 if ((lvalue_p & clk_bitfield) != 0
977 || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
978 /* For the purposes of overload resolution, we ignore the fact
979 this expression is a bitfield or packed field. (In particular,
980 [over.ics.ref] says specifically that a function with a
981 non-const reference parameter is viable even if the
982 argument is a bitfield.)
984 However, when we actually call the function we must create
985 a temporary to which to bind the reference. If the
986 reference is volatile, or isn't const, then we cannot make
987 a temporary, so we just issue an error when the conversion
989 NEED_TEMPORARY_P (conv) = 1;
993 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
997 If the initializer expression
999 -- has a class type (i.e., T2 is a class type) can be
1000 implicitly converted to an lvalue of type "cv3 T3," where
1001 "cv1 T1" is reference-compatible with "cv3 T3". (this
1002 conversion is selected by enumerating the applicable
1003 conversion functions (_over.match.ref_) and choosing the
1004 best one through overload resolution. (_over.match_).
1006 the reference is bound to the lvalue result of the conversion
1007 in the second case. */
1008 conv = convert_class_to_reference (to, from, expr);
1013 /* From this point on, we conceptually need temporaries, even if we
1014 elide them. Only the cases above are "direct bindings". */
1015 if (flags & LOOKUP_NO_TEMP_BIND)
1020 When a parameter of reference type is not bound directly to an
1021 argument expression, the conversion sequence is the one required
1022 to convert the argument expression to the underlying type of the
1023 reference according to _over.best.ics_. Conceptually, this
1024 conversion sequence corresponds to copy-initializing a temporary
1025 of the underlying type with the argument expression. Any
1026 difference in top-level cv-qualification is subsumed by the
1027 initialization itself and does not constitute a conversion. */
1031 Otherwise, the reference shall be to a non-volatile const type. */
1032 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1037 If the initializer expression is an rvalue, with T2 a class type,
1038 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1039 is bound in one of the following ways:
1041 -- The reference is bound to the object represented by the rvalue
1042 or to a sub-object within that object.
1046 We use the first alternative. The implicit conversion sequence
1047 is supposed to be same as we would obtain by generating a
1048 temporary. Fortunately, if the types are reference compatible,
1049 then this is either an identity conversion or the derived-to-base
1050 conversion, just as for direct binding. */
1051 if (CLASS_TYPE_P (from) && compatible_p)
1053 conv = build1 (IDENTITY_CONV, from, expr);
1054 conv = direct_reference_binding (rto, conv);
1055 CHECK_COPY_CONSTRUCTOR_P (TREE_OPERAND (conv, 0)) = 1;
1061 Otherwise, a temporary of type "cv1 T1" is created and
1062 initialized from the initializer expression using the rules for a
1063 non-reference copy initialization. If T1 is reference-related to
1064 T2, cv1 must be the same cv-qualification as, or greater
1065 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1066 if (related_p && !at_least_as_qualified_p (to, from))
1069 conv = implicit_conversion (to, from, expr, flags);
1073 conv = build_conv (REF_BIND, rto, conv);
1074 /* This reference binding, unlike those above, requires the
1075 creation of a temporary. */
1076 NEED_TEMPORARY_P (conv) = 1;
1081 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1082 to type TO. The optional expression EXPR may affect the conversion.
1083 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1087 implicit_conversion (tree to, tree from, tree expr, int flags)
1091 if (from == error_mark_node || to == error_mark_node
1092 || expr == error_mark_node)
1095 if (TREE_CODE (to) == REFERENCE_TYPE)
1096 conv = reference_binding (to, from, expr, flags);
1098 conv = standard_conversion (to, from, expr);
1103 if (expr != NULL_TREE
1104 && (IS_AGGR_TYPE (from)
1105 || IS_AGGR_TYPE (to))
1106 && (flags & LOOKUP_NO_CONVERSION) == 0)
1108 struct z_candidate *cand;
1110 cand = build_user_type_conversion_1
1111 (to, expr, LOOKUP_ONLYCONVERTING);
1113 conv = cand->second_conv;
1115 /* We used to try to bind a reference to a temporary here, but that
1116 is now handled by the recursive call to this function at the end
1117 of reference_binding. */
1124 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1127 static struct z_candidate *
1128 add_candidate (struct z_candidate **candidates,
1129 tree fn, tree args, tree convs, tree access_path,
1130 tree conversion_path, int viable)
1132 struct z_candidate *cand = ggc_alloc_cleared (sizeof (struct z_candidate));
1136 cand->convs = convs;
1137 cand->access_path = access_path;
1138 cand->conversion_path = conversion_path;
1139 cand->viable = viable;
1140 cand->next = *candidates;
1146 /* Create an overload candidate for the function or method FN called with
1147 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1148 to implicit_conversion.
1150 CTYPE, if non-NULL, is the type we want to pretend this function
1151 comes from for purposes of overload resolution. */
1153 static struct z_candidate *
1154 add_function_candidate (struct z_candidate **candidates,
1155 tree fn, tree ctype, tree arglist,
1156 tree access_path, tree conversion_path,
1159 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1162 tree parmnode, argnode;
1166 /* Built-in functions that haven't been declared don't really
1168 if (DECL_ANTICIPATED (fn))
1171 /* The `this', `in_chrg' and VTT arguments to constructors are not
1172 considered in overload resolution. */
1173 if (DECL_CONSTRUCTOR_P (fn))
1175 parmlist = skip_artificial_parms_for (fn, parmlist);
1176 orig_arglist = arglist;
1177 arglist = skip_artificial_parms_for (fn, arglist);
1180 orig_arglist = arglist;
1182 len = list_length (arglist);
1183 convs = make_tree_vec (len);
1185 /* 13.3.2 - Viable functions [over.match.viable]
1186 First, to be a viable function, a candidate function shall have enough
1187 parameters to agree in number with the arguments in the list.
1189 We need to check this first; otherwise, checking the ICSes might cause
1190 us to produce an ill-formed template instantiation. */
1192 parmnode = parmlist;
1193 for (i = 0; i < len; ++i)
1195 if (parmnode == NULL_TREE || parmnode == void_list_node)
1197 parmnode = TREE_CHAIN (parmnode);
1200 if (i < len && parmnode)
1203 /* Make sure there are default args for the rest of the parms. */
1204 else if (!sufficient_parms_p (parmnode))
1210 /* Second, for F to be a viable function, there shall exist for each
1211 argument an implicit conversion sequence that converts that argument
1212 to the corresponding parameter of F. */
1214 parmnode = parmlist;
1217 for (i = 0; i < len; ++i)
1219 tree arg = TREE_VALUE (argnode);
1220 tree argtype = lvalue_type (arg);
1224 if (parmnode == void_list_node)
1227 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1228 && ! DECL_CONSTRUCTOR_P (fn));
1232 tree parmtype = TREE_VALUE (parmnode);
1234 /* The type of the implicit object parameter ('this') for
1235 overload resolution is not always the same as for the
1236 function itself; conversion functions are considered to
1237 be members of the class being converted, and functions
1238 introduced by a using-declaration are considered to be
1239 members of the class that uses them.
1241 Since build_over_call ignores the ICS for the `this'
1242 parameter, we can just change the parm type. */
1243 if (ctype && is_this)
1246 = build_qualified_type (ctype,
1247 TYPE_QUALS (TREE_TYPE (parmtype)));
1248 parmtype = build_pointer_type (parmtype);
1251 t = implicit_conversion (parmtype, argtype, arg, flags);
1255 t = build1 (IDENTITY_CONV, argtype, arg);
1256 ICS_ELLIPSIS_FLAG (t) = 1;
1260 ICS_THIS_FLAG (t) = 1;
1262 TREE_VEC_ELT (convs, i) = t;
1269 if (ICS_BAD_FLAG (t))
1273 parmnode = TREE_CHAIN (parmnode);
1274 argnode = TREE_CHAIN (argnode);
1278 return add_candidate (candidates, fn, orig_arglist, convs, access_path,
1279 conversion_path, viable);
1282 /* Create an overload candidate for the conversion function FN which will
1283 be invoked for expression OBJ, producing a pointer-to-function which
1284 will in turn be called with the argument list ARGLIST, and add it to
1285 CANDIDATES. FLAGS is passed on to implicit_conversion.
1287 Actually, we don't really care about FN; we care about the type it
1288 converts to. There may be multiple conversion functions that will
1289 convert to that type, and we rely on build_user_type_conversion_1 to
1290 choose the best one; so when we create our candidate, we record the type
1291 instead of the function. */
1293 static struct z_candidate *
1294 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1295 tree arglist, tree access_path, tree conversion_path)
1297 tree totype = TREE_TYPE (TREE_TYPE (fn));
1298 int i, len, viable, flags;
1299 tree parmlist, convs, parmnode, argnode;
1301 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1302 parmlist = TREE_TYPE (parmlist);
1303 parmlist = TYPE_ARG_TYPES (parmlist);
1305 len = list_length (arglist) + 1;
1306 convs = make_tree_vec (len);
1307 parmnode = parmlist;
1310 flags = LOOKUP_NORMAL;
1312 /* Don't bother looking up the same type twice. */
1313 if (*candidates && (*candidates)->fn == totype)
1316 for (i = 0; i < len; ++i)
1318 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1319 tree argtype = lvalue_type (arg);
1323 t = implicit_conversion (totype, argtype, arg, flags);
1324 else if (parmnode == void_list_node)
1327 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1330 t = build1 (IDENTITY_CONV, argtype, arg);
1331 ICS_ELLIPSIS_FLAG (t) = 1;
1334 TREE_VEC_ELT (convs, i) = t;
1338 if (ICS_BAD_FLAG (t))
1345 parmnode = TREE_CHAIN (parmnode);
1346 argnode = TREE_CHAIN (argnode);
1352 if (!sufficient_parms_p (parmnode))
1355 return add_candidate (candidates, totype, arglist, convs, access_path,
1356 conversion_path, viable);
1360 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1361 tree type1, tree type2, tree *args, tree *argtypes,
1371 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1373 for (i = 0; i < 2; ++i)
1378 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1382 /* We need something for printing the candidate. */
1383 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1385 else if (ICS_BAD_FLAG (t))
1387 TREE_VEC_ELT (convs, i) = t;
1390 /* For COND_EXPR we rearranged the arguments; undo that now. */
1393 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1394 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1395 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1397 TREE_VEC_ELT (convs, 0) = t;
1402 add_candidate (candidates, fnname, /*args=*/NULL_TREE, convs,
1403 /*access_path=*/NULL_TREE,
1404 /*conversion_path=*/NULL_TREE,
1409 is_complete (tree t)
1411 return COMPLETE_TYPE_P (complete_type (t));
1414 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1417 promoted_arithmetic_type_p (tree type)
1421 In this section, the term promoted integral type is used to refer
1422 to those integral types which are preserved by integral promotion
1423 (including e.g. int and long but excluding e.g. char).
1424 Similarly, the term promoted arithmetic type refers to promoted
1425 integral types plus floating types. */
1426 return ((INTEGRAL_TYPE_P (type)
1427 && same_type_p (type_promotes_to (type), type))
1428 || TREE_CODE (type) == REAL_TYPE);
1431 /* Create any builtin operator overload candidates for the operator in
1432 question given the converted operand types TYPE1 and TYPE2. The other
1433 args are passed through from add_builtin_candidates to
1434 build_builtin_candidate.
1436 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1437 If CODE is requires candidates operands of the same type of the kind
1438 of which TYPE1 and TYPE2 are, we add both candidates
1439 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1442 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1443 enum tree_code code2, tree fnname, tree type1,
1444 tree type2, tree *args, tree *argtypes, int flags)
1448 case POSTINCREMENT_EXPR:
1449 case POSTDECREMENT_EXPR:
1450 args[1] = integer_zero_node;
1451 type2 = integer_type_node;
1460 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1461 and VQ is either volatile or empty, there exist candidate operator
1462 functions of the form
1463 VQ T& operator++(VQ T&);
1464 T operator++(VQ T&, int);
1465 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1466 type other than bool, and VQ is either volatile or empty, there exist
1467 candidate operator functions of the form
1468 VQ T& operator--(VQ T&);
1469 T operator--(VQ T&, int);
1470 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1471 complete object type, and VQ is either volatile or empty, there exist
1472 candidate operator functions of the form
1473 T*VQ& operator++(T*VQ&);
1474 T*VQ& operator--(T*VQ&);
1475 T* operator++(T*VQ&, int);
1476 T* operator--(T*VQ&, int); */
1478 case POSTDECREMENT_EXPR:
1479 case PREDECREMENT_EXPR:
1480 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1482 case POSTINCREMENT_EXPR:
1483 case PREINCREMENT_EXPR:
1484 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1486 type1 = build_reference_type (type1);
1491 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1492 exist candidate operator functions of the form
1496 8 For every function type T, there exist candidate operator functions of
1498 T& operator*(T*); */
1501 if (TREE_CODE (type1) == POINTER_TYPE
1502 && (TYPE_PTROB_P (type1)
1503 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1507 /* 9 For every type T, there exist candidate operator functions of the form
1510 10For every promoted arithmetic type T, there exist candidate operator
1511 functions of the form
1515 case CONVERT_EXPR: /* unary + */
1516 if (TREE_CODE (type1) == POINTER_TYPE)
1519 if (ARITHMETIC_TYPE_P (type1))
1523 /* 11For every promoted integral type T, there exist candidate operator
1524 functions of the form
1528 if (INTEGRAL_TYPE_P (type1))
1532 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1533 is the same type as C2 or is a derived class of C2, T is a complete
1534 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1535 there exist candidate operator functions of the form
1536 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1537 where CV12 is the union of CV1 and CV2. */
1540 if (TREE_CODE (type1) == POINTER_TYPE
1541 && TYPE_PTR_TO_MEMBER_P (type2))
1543 tree c1 = TREE_TYPE (type1);
1544 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
1546 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1547 && (TYPE_PTRMEMFUNC_P (type2)
1548 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1553 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1554 didate operator functions of the form
1559 bool operator<(L, R);
1560 bool operator>(L, R);
1561 bool operator<=(L, R);
1562 bool operator>=(L, R);
1563 bool operator==(L, R);
1564 bool operator!=(L, R);
1565 where LR is the result of the usual arithmetic conversions between
1568 14For every pair of types T and I, where T is a cv-qualified or cv-
1569 unqualified complete object type and I is a promoted integral type,
1570 there exist candidate operator functions of the form
1571 T* operator+(T*, I);
1572 T& operator[](T*, I);
1573 T* operator-(T*, I);
1574 T* operator+(I, T*);
1575 T& operator[](I, T*);
1577 15For every T, where T is a pointer to complete object type, there exist
1578 candidate operator functions of the form112)
1579 ptrdiff_t operator-(T, T);
1581 16For every pointer or enumeration type T, there exist candidate operator
1582 functions of the form
1583 bool operator<(T, T);
1584 bool operator>(T, T);
1585 bool operator<=(T, T);
1586 bool operator>=(T, T);
1587 bool operator==(T, T);
1588 bool operator!=(T, T);
1590 17For every pointer to member type T, there exist candidate operator
1591 functions of the form
1592 bool operator==(T, T);
1593 bool operator!=(T, T); */
1596 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1598 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1600 type2 = ptrdiff_type_node;
1604 case TRUNC_DIV_EXPR:
1605 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1611 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1612 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1614 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
1619 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
1631 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1633 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1635 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1637 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1642 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1650 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1653 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1655 type1 = ptrdiff_type_node;
1658 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1660 type2 = ptrdiff_type_node;
1665 /* 18For every pair of promoted integral types L and R, there exist candi-
1666 date operator functions of the form
1673 where LR is the result of the usual arithmetic conversions between
1676 case TRUNC_MOD_EXPR:
1682 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1686 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1687 type, VQ is either volatile or empty, and R is a promoted arithmetic
1688 type, there exist candidate operator functions of the form
1689 VQ L& operator=(VQ L&, R);
1690 VQ L& operator*=(VQ L&, R);
1691 VQ L& operator/=(VQ L&, R);
1692 VQ L& operator+=(VQ L&, R);
1693 VQ L& operator-=(VQ L&, R);
1695 20For every pair T, VQ), where T is any type and VQ is either volatile
1696 or empty, there exist candidate operator functions of the form
1697 T*VQ& operator=(T*VQ&, T*);
1699 21For every pair T, VQ), where T is a pointer to member type and VQ is
1700 either volatile or empty, there exist candidate operator functions of
1702 VQ T& operator=(VQ T&, T);
1704 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1705 unqualified complete object type, VQ is either volatile or empty, and
1706 I is a promoted integral type, there exist candidate operator func-
1708 T*VQ& operator+=(T*VQ&, I);
1709 T*VQ& operator-=(T*VQ&, I);
1711 23For every triple L, VQ, R), where L is an integral or enumeration
1712 type, VQ is either volatile or empty, and R is a promoted integral
1713 type, there exist candidate operator functions of the form
1715 VQ L& operator%=(VQ L&, R);
1716 VQ L& operator<<=(VQ L&, R);
1717 VQ L& operator>>=(VQ L&, R);
1718 VQ L& operator&=(VQ L&, R);
1719 VQ L& operator^=(VQ L&, R);
1720 VQ L& operator|=(VQ L&, R); */
1727 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1729 type2 = ptrdiff_type_node;
1733 case TRUNC_DIV_EXPR:
1734 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1738 case TRUNC_MOD_EXPR:
1744 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1749 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1751 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1752 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1753 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1754 || ((TYPE_PTRMEMFUNC_P (type1)
1755 || TREE_CODE (type1) == POINTER_TYPE)
1756 && null_ptr_cst_p (args[1])))
1766 type1 = build_reference_type (type1);
1772 For every pair of promoted arithmetic types L and R, there
1773 exist candidate operator functions of the form
1775 LR operator?(bool, L, R);
1777 where LR is the result of the usual arithmetic conversions
1778 between types L and R.
1780 For every type T, where T is a pointer or pointer-to-member
1781 type, there exist candidate operator functions of the form T
1782 operator?(bool, T, T); */
1784 if (promoted_arithmetic_type_p (type1)
1785 && promoted_arithmetic_type_p (type2))
1789 /* Otherwise, the types should be pointers. */
1790 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1791 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1794 /* We don't check that the two types are the same; the logic
1795 below will actually create two candidates; one in which both
1796 parameter types are TYPE1, and one in which both parameter
1804 /* If we're dealing with two pointer types or two enumeral types,
1805 we need candidates for both of them. */
1806 if (type2 && !same_type_p (type1, type2)
1807 && TREE_CODE (type1) == TREE_CODE (type2)
1808 && (TREE_CODE (type1) == REFERENCE_TYPE
1809 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1810 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1811 || TYPE_PTRMEMFUNC_P (type1)
1812 || IS_AGGR_TYPE (type1)
1813 || TREE_CODE (type1) == ENUMERAL_TYPE))
1815 build_builtin_candidate
1816 (candidates, fnname, type1, type1, args, argtypes, flags);
1817 build_builtin_candidate
1818 (candidates, fnname, type2, type2, args, argtypes, flags);
1822 build_builtin_candidate
1823 (candidates, fnname, type1, type2, args, argtypes, flags);
1827 type_decays_to (tree type)
1829 if (TREE_CODE (type) == ARRAY_TYPE)
1830 return build_pointer_type (TREE_TYPE (type));
1831 if (TREE_CODE (type) == FUNCTION_TYPE)
1832 return build_pointer_type (type);
1836 /* There are three conditions of builtin candidates:
1838 1) bool-taking candidates. These are the same regardless of the input.
1839 2) pointer-pair taking candidates. These are generated for each type
1840 one of the input types converts to.
1841 3) arithmetic candidates. According to the standard, we should generate
1842 all of these, but I'm trying not to...
1844 Here we generate a superset of the possible candidates for this particular
1845 case. That is a subset of the full set the standard defines, plus some
1846 other cases which the standard disallows. add_builtin_candidate will
1847 filter out the invalid set. */
1850 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
1851 enum tree_code code2, tree fnname, tree *args,
1856 tree type, argtypes[3];
1857 /* TYPES[i] is the set of possible builtin-operator parameter types
1858 we will consider for the Ith argument. These are represented as
1859 a TREE_LIST; the TREE_VALUE of each node is the potential
1863 for (i = 0; i < 3; ++i)
1866 argtypes[i] = lvalue_type (args[i]);
1868 argtypes[i] = NULL_TREE;
1873 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1874 and VQ is either volatile or empty, there exist candidate operator
1875 functions of the form
1876 VQ T& operator++(VQ T&); */
1878 case POSTINCREMENT_EXPR:
1879 case PREINCREMENT_EXPR:
1880 case POSTDECREMENT_EXPR:
1881 case PREDECREMENT_EXPR:
1886 /* 24There also exist candidate operator functions of the form
1887 bool operator!(bool);
1888 bool operator&&(bool, bool);
1889 bool operator||(bool, bool); */
1891 case TRUTH_NOT_EXPR:
1892 build_builtin_candidate
1893 (candidates, fnname, boolean_type_node,
1894 NULL_TREE, args, argtypes, flags);
1897 case TRUTH_ORIF_EXPR:
1898 case TRUTH_ANDIF_EXPR:
1899 build_builtin_candidate
1900 (candidates, fnname, boolean_type_node,
1901 boolean_type_node, args, argtypes, flags);
1923 types[0] = types[1] = NULL_TREE;
1925 for (i = 0; i < 2; ++i)
1929 else if (IS_AGGR_TYPE (argtypes[i]))
1933 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1936 convs = lookup_conversions (argtypes[i]);
1938 if (code == COND_EXPR)
1940 if (real_lvalue_p (args[i]))
1941 types[i] = tree_cons
1942 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1944 types[i] = tree_cons
1945 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1951 for (; convs; convs = TREE_CHAIN (convs))
1953 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
1956 && (TREE_CODE (type) != REFERENCE_TYPE
1957 || CP_TYPE_CONST_P (TREE_TYPE (type))))
1960 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1961 types[i] = tree_cons (NULL_TREE, type, types[i]);
1963 type = non_reference (type);
1964 if (i != 0 || ! ref1)
1966 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1967 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
1968 types[i] = tree_cons (NULL_TREE, type, types[i]);
1969 if (INTEGRAL_TYPE_P (type))
1970 type = type_promotes_to (type);
1973 if (! value_member (type, types[i]))
1974 types[i] = tree_cons (NULL_TREE, type, types[i]);
1979 if (code == COND_EXPR && real_lvalue_p (args[i]))
1980 types[i] = tree_cons
1981 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1982 type = non_reference (argtypes[i]);
1983 if (i != 0 || ! ref1)
1985 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1986 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
1987 types[i] = tree_cons (NULL_TREE, type, types[i]);
1988 if (INTEGRAL_TYPE_P (type))
1989 type = type_promotes_to (type);
1991 types[i] = tree_cons (NULL_TREE, type, types[i]);
1995 /* Run through the possible parameter types of both arguments,
1996 creating candidates with those parameter types. */
1997 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2000 for (type = types[1]; type; type = TREE_CHAIN (type))
2001 add_builtin_candidate
2002 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2003 TREE_VALUE (type), args, argtypes, flags);
2005 add_builtin_candidate
2006 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2007 NULL_TREE, args, argtypes, flags);
2014 /* If TMPL can be successfully instantiated as indicated by
2015 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2017 TMPL is the template. EXPLICIT_TARGS are any explicit template
2018 arguments. ARGLIST is the arguments provided at the call-site.
2019 The RETURN_TYPE is the desired type for conversion operators. If
2020 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2021 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2022 add_conv_candidate. */
2024 static struct z_candidate*
2025 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2026 tree ctype, tree explicit_targs, tree arglist,
2027 tree return_type, tree access_path,
2028 tree conversion_path, int flags, tree obj,
2029 unification_kind_t strict)
2031 int ntparms = DECL_NTPARMS (tmpl);
2032 tree targs = make_tree_vec (ntparms);
2033 tree args_without_in_chrg = arglist;
2034 struct z_candidate *cand;
2038 /* We don't do deduction on the in-charge parameter, the VTT
2039 parameter or 'this'. */
2040 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2041 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2043 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2044 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2045 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2046 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2048 i = fn_type_unification (tmpl, explicit_targs, targs,
2049 args_without_in_chrg,
2050 return_type, strict, -1);
2055 fn = instantiate_template (tmpl, targs, tf_none);
2056 if (fn == error_mark_node)
2061 A member function template is never instantiated to perform the
2062 copy of a class object to an object of its class type.
2064 It's a little unclear what this means; the standard explicitly
2065 does allow a template to be used to copy a class. For example,
2070 template <class T> A(const T&);
2073 void g () { A a (f ()); }
2075 the member template will be used to make the copy. The section
2076 quoted above appears in the paragraph that forbids constructors
2077 whose only parameter is (a possibly cv-qualified variant of) the
2078 class type, and a logical interpretation is that the intent was
2079 to forbid the instantiation of member templates which would then
2081 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2083 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2084 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2089 if (obj != NULL_TREE)
2090 /* Aha, this is a conversion function. */
2091 cand = add_conv_candidate (candidates, fn, obj, access_path,
2092 conversion_path, arglist);
2094 cand = add_function_candidate (candidates, fn, ctype,
2095 arglist, access_path,
2096 conversion_path, flags);
2097 if (DECL_TI_TEMPLATE (fn) != tmpl)
2098 /* This situation can occur if a member template of a template
2099 class is specialized. Then, instantiate_template might return
2100 an instantiation of the specialization, in which case the
2101 DECL_TI_TEMPLATE field will point at the original
2102 specialization. For example:
2104 template <class T> struct S { template <class U> void f(U);
2105 template <> void f(int) {}; };
2109 Here, TMPL will be template <class U> S<double>::f(U).
2110 And, instantiate template will give us the specialization
2111 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2112 for this will point at template <class T> template <> S<T>::f(int),
2113 so that we can find the definition. For the purposes of
2114 overload resolution, however, we want the original TMPL. */
2115 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2117 cand->template = DECL_TEMPLATE_INFO (fn);
2123 static struct z_candidate *
2124 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2125 tree explicit_targs, tree arglist, tree return_type,
2126 tree access_path, tree conversion_path, int flags,
2127 unification_kind_t strict)
2130 add_template_candidate_real (candidates, tmpl, ctype,
2131 explicit_targs, arglist, return_type,
2132 access_path, conversion_path,
2133 flags, NULL_TREE, strict);
2137 static struct z_candidate *
2138 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2139 tree obj, tree arglist, tree return_type,
2140 tree access_path, tree conversion_path)
2143 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2144 arglist, return_type, access_path,
2145 conversion_path, 0, obj, DEDUCE_CONV);
2148 /* The CANDS are the set of candidates that were considered for
2149 overload resolution. Return the set of viable candidates. If none
2150 of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P
2151 is true if a candidate should be considered viable only if it is
2154 static struct z_candidate*
2155 splice_viable (struct z_candidate *cands,
2159 struct z_candidate *viable;
2160 struct z_candidate **last_viable;
2161 struct z_candidate **cand;
2164 last_viable = &viable;
2165 *any_viable_p = false;
2170 struct z_candidate *c = *cand;
2171 if (strict_p ? c->viable == 1 : c->viable)
2176 last_viable = &c->next;
2177 *any_viable_p = true;
2183 return viable ? viable : cands;
2187 any_strictly_viable (struct z_candidate *cands)
2189 for (; cands; cands = cands->next)
2190 if (cands->viable == 1)
2196 build_this (tree obj)
2198 /* Fix this to work on non-lvalues. */
2199 return build_unary_op (ADDR_EXPR, obj, 0);
2202 /* Returns true iff functions are equivalent. Equivalent functions are
2203 not '==' only if one is a function-local extern function or if
2204 both are extern "C". */
2207 equal_functions (tree fn1, tree fn2)
2209 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2210 || DECL_EXTERN_C_FUNCTION_P (fn1))
2211 return decls_match (fn1, fn2);
2215 /* Print information about one overload candidate CANDIDATE. MSGSTR
2216 is the text to print before the candidate itself.
2218 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2219 to have been run through gettext by the caller. This wart makes
2220 life simpler in print_z_candidates and for the translators. */
2223 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2225 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2227 if (TREE_VEC_LENGTH (candidate->convs) == 3)
2228 inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2229 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2230 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)),
2231 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 2)));
2232 else if (TREE_VEC_LENGTH (candidate->convs) == 2)
2233 inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2234 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2235 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)));
2237 inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2238 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)));
2240 else if (TYPE_P (candidate->fn))
2241 inform ("%s %T <conversion>", msgstr, candidate->fn);
2242 else if (candidate->viable == -1)
2243 inform ("%J%s %+#D <near match>", candidate->fn, msgstr, candidate->fn);
2245 inform ("%J%s %+#D", candidate->fn, msgstr, candidate->fn);
2249 print_z_candidates (struct z_candidate *candidates)
2252 struct z_candidate *cand1;
2253 struct z_candidate **cand2;
2255 /* There may be duplicates in the set of candidates. We put off
2256 checking this condition as long as possible, since we have no way
2257 to eliminate duplicates from a set of functions in less than n^2
2258 time. Now we are about to emit an error message, so it is more
2259 permissible to go slowly. */
2260 for (cand1 = candidates; cand1; cand1 = cand1->next)
2262 tree fn = cand1->fn;
2263 /* Skip builtin candidates and conversion functions. */
2264 if (TREE_CODE (fn) != FUNCTION_DECL)
2266 cand2 = &cand1->next;
2269 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2270 && equal_functions (fn, (*cand2)->fn))
2271 *cand2 = (*cand2)->next;
2273 cand2 = &(*cand2)->next;
2280 str = _("candidates are:");
2281 print_z_candidate (str, candidates);
2282 if (candidates->next)
2284 /* Indent successive candidates by the width of the translation
2285 of the above string. */
2286 size_t len = gcc_gettext_width (str) + 1;
2287 char *spaces = alloca (len);
2288 memset (spaces, ' ', len-1);
2289 spaces[len - 1] = '\0';
2291 candidates = candidates->next;
2294 print_z_candidate (spaces, candidates);
2295 candidates = candidates->next;
2301 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2302 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2303 the result of the conversion function to convert it to the final
2304 desired type. Merge the the two sequences into a single sequence,
2305 and return the merged sequence. */
2308 merge_conversion_sequences (tree user_seq, tree std_seq)
2312 my_friendly_assert (TREE_CODE (user_seq) == USER_CONV,
2315 /* Find the end of the second conversion sequence. */
2317 while (TREE_CODE (*t) != IDENTITY_CONV)
2318 t = &TREE_OPERAND (*t, 0);
2320 /* Replace the identity conversion with the user conversion
2324 /* The entire sequence is a user-conversion sequence. */
2325 ICS_USER_FLAG (std_seq) = 1;
2330 /* Returns the best overload candidate to perform the requested
2331 conversion. This function is used for three the overloading situations
2332 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2333 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2334 per [dcl.init.ref], so we ignore temporary bindings. */
2336 static struct z_candidate *
2337 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2339 struct z_candidate *candidates, *cand;
2340 tree fromtype = TREE_TYPE (expr);
2341 tree ctors = NULL_TREE, convs = NULL_TREE;
2342 tree args = NULL_TREE;
2345 /* We represent conversion within a hierarchy using RVALUE_CONV and
2346 BASE_CONV, as specified by [over.best.ics]; these become plain
2347 constructor calls, as specified in [dcl.init]. */
2348 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2349 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2351 if (IS_AGGR_TYPE (totype))
2352 ctors = lookup_fnfields (TYPE_BINFO (totype),
2353 complete_ctor_identifier,
2356 if (IS_AGGR_TYPE (fromtype))
2357 convs = lookup_conversions (fromtype);
2360 flags |= LOOKUP_NO_CONVERSION;
2366 ctors = BASELINK_FUNCTIONS (ctors);
2368 t = build_int_2 (0, 0);
2369 TREE_TYPE (t) = build_pointer_type (totype);
2370 args = build_tree_list (NULL_TREE, expr);
2371 /* We should never try to call the abstract or base constructor
2373 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2374 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2376 args = tree_cons (NULL_TREE, t, args);
2378 for (; ctors; ctors = OVL_NEXT (ctors))
2380 tree ctor = OVL_CURRENT (ctors);
2381 if (DECL_NONCONVERTING_P (ctor))
2384 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2385 cand = add_template_candidate (&candidates, ctor, totype,
2386 NULL_TREE, args, NULL_TREE,
2387 TYPE_BINFO (totype),
2388 TYPE_BINFO (totype),
2392 cand = add_function_candidate (&candidates, ctor, totype,
2393 args, TYPE_BINFO (totype),
2394 TYPE_BINFO (totype),
2398 cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2402 args = build_tree_list (NULL_TREE, build_this (expr));
2404 for (; convs; convs = TREE_CHAIN (convs))
2407 tree conversion_path = TREE_PURPOSE (convs);
2408 int convflags = LOOKUP_NO_CONVERSION;
2410 /* If we are called to convert to a reference type, we are trying to
2411 find an lvalue binding, so don't even consider temporaries. If
2412 we don't find an lvalue binding, the caller will try again to
2413 look for a temporary binding. */
2414 if (TREE_CODE (totype) == REFERENCE_TYPE)
2415 convflags |= LOOKUP_NO_TEMP_BIND;
2417 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2419 tree fn = OVL_CURRENT (fns);
2421 /* [over.match.funcs] For conversion functions, the function
2422 is considered to be a member of the class of the implicit
2423 object argument for the purpose of defining the type of
2424 the implicit object parameter.
2426 So we pass fromtype as CTYPE to add_*_candidate. */
2428 if (TREE_CODE (fn) == TEMPLATE_DECL)
2429 cand = add_template_candidate (&candidates, fn, fromtype,
2432 TYPE_BINFO (fromtype),
2437 cand = add_function_candidate (&candidates, fn, fromtype,
2439 TYPE_BINFO (fromtype),
2445 tree ics = implicit_conversion (totype,
2446 TREE_TYPE (TREE_TYPE (cand->fn)),
2449 cand->second_conv = ics;
2451 if (ics == NULL_TREE)
2453 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2459 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2463 cand = tourney (candidates);
2466 if (flags & LOOKUP_COMPLAIN)
2468 error ("conversion from `%T' to `%T' is ambiguous",
2470 print_z_candidates (candidates);
2473 cand = candidates; /* any one will do */
2474 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2475 ICS_USER_FLAG (cand->second_conv) = 1;
2476 if (!any_strictly_viable (candidates))
2477 ICS_BAD_FLAG (cand->second_conv) = 1;
2478 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2479 ambiguous conversion is no worse than another user-defined
2485 /* Build the user conversion sequence. */
2488 (DECL_CONSTRUCTOR_P (cand->fn)
2489 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2490 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
2491 TREE_OPERAND (convs, 1) = build_zc_wrapper (cand);
2493 /* Combine it with the second conversion sequence. */
2494 cand->second_conv = merge_conversion_sequences (convs,
2497 if (cand->viable == -1)
2498 ICS_BAD_FLAG (cand->second_conv) = 1;
2504 build_user_type_conversion (tree totype, tree expr, int flags)
2506 struct z_candidate *cand
2507 = build_user_type_conversion_1 (totype, expr, flags);
2511 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2512 return error_mark_node;
2513 return convert_from_reference (convert_like (cand->second_conv, expr));
2518 /* Do any initial processing on the arguments to a function call. */
2521 resolve_args (tree args)
2524 for (t = args; t; t = TREE_CHAIN (t))
2526 tree arg = TREE_VALUE (t);
2528 if (arg == error_mark_node)
2529 return error_mark_node;
2530 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2532 error ("invalid use of void expression");
2533 return error_mark_node;
2535 arg = convert_from_reference (arg);
2536 TREE_VALUE (t) = arg;
2541 /* Perform overload resolution on FN, which is called with the ARGS.
2543 Return the candidate function selected by overload resolution, or
2544 NULL if the event that overload resolution failed. In the case
2545 that overload resolution fails, *CANDIDATES will be the set of
2546 candidates considered, and ANY_VIABLE_P will be set to true or
2547 false to indicate whether or not any of the candidates were
2550 The ARGS should already have gone through RESOLVE_ARGS before this
2551 function is called. */
2553 static struct z_candidate *
2554 perform_overload_resolution (tree fn,
2556 struct z_candidate **candidates,
2559 struct z_candidate *cand;
2560 tree explicit_targs = NULL_TREE;
2561 int template_only = 0;
2564 *any_viable_p = true;
2566 /* Check FN and ARGS. */
2567 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2568 || TREE_CODE (fn) == TEMPLATE_DECL
2569 || TREE_CODE (fn) == OVERLOAD
2570 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2572 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2575 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2577 explicit_targs = TREE_OPERAND (fn, 1);
2578 fn = TREE_OPERAND (fn, 0);
2582 /* Add the various candidate functions. */
2583 add_candidates (fn, args, explicit_targs, template_only,
2584 /*conversion_path=*/NULL_TREE,
2585 /*access_path=*/NULL_TREE,
2589 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2593 cand = tourney (*candidates);
2597 /* Return an expression for a call to FN (a namespace-scope function,
2598 or a static member function) with the ARGS. */
2601 build_new_function_call (tree fn, tree args)
2603 struct z_candidate *candidates, *cand;
2606 args = resolve_args (args);
2607 if (args == error_mark_node)
2608 return error_mark_node;
2610 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2614 if (!any_viable_p && candidates && ! candidates->next)
2615 return build_function_call (candidates->fn, args);
2616 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2617 fn = TREE_OPERAND (fn, 0);
2619 error ("no matching function for call to `%D(%A)'",
2620 DECL_NAME (OVL_CURRENT (fn)), args);
2622 error ("call of overloaded `%D(%A)' is ambiguous",
2623 DECL_NAME (OVL_CURRENT (fn)), args);
2625 print_z_candidates (candidates);
2626 return error_mark_node;
2629 return build_over_call (cand, LOOKUP_NORMAL);
2632 /* Build a call to a global operator new. FNNAME is the name of the
2633 operator (either "operator new" or "operator new[]") and ARGS are
2634 the arguments provided. *SIZE points to the total number of bytes
2635 required by the allocation, and is updated if that is changed here.
2636 *COOKIE_SIZE is non-NULL if a cookie should be used. If this
2637 function determines that no cookie should be used, after all,
2638 *COOKIE_SIZE is set to NULL_TREE. */
2641 build_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
2644 struct z_candidate *candidates;
2645 struct z_candidate *cand;
2648 args = tree_cons (NULL_TREE, *size, args);
2649 args = resolve_args (args);
2650 if (args == error_mark_node)
2653 fns = lookup_function_nonclass (fnname, args);
2655 /* Figure out what function is being called. */
2656 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2658 /* If no suitable function could be found, issue an error message
2663 error ("no matching function for call to `%D(%A)'",
2664 DECL_NAME (OVL_CURRENT (fns)), args);
2666 error ("call of overloaded `%D(%A)' is ambiguous",
2667 DECL_NAME (OVL_CURRENT (fns)), args);
2669 print_z_candidates (candidates);
2670 return error_mark_node;
2673 /* If a cookie is required, add some extra space. Whether
2674 or not a cookie is required cannot be determined until
2675 after we know which function was called. */
2678 bool use_cookie = true;
2679 if (!abi_version_at_least (2))
2681 tree placement = TREE_CHAIN (args);
2682 /* In G++ 3.2, the check was implemented incorrectly; it
2683 looked at the placement expression, rather than the
2684 type of the function. */
2685 if (placement && !TREE_CHAIN (placement)
2686 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2694 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2695 /* Skip the size_t parameter. */
2696 arg_types = TREE_CHAIN (arg_types);
2697 /* Check the remaining parameters (if any). */
2699 && TREE_CHAIN (arg_types) == void_list_node
2700 && same_type_p (TREE_VALUE (arg_types),
2704 /* If we need a cookie, adjust the number of bytes allocated. */
2707 /* Update the total size. */
2708 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2709 /* Update the argument list to reflect the adjusted size. */
2710 TREE_VALUE (args) = *size;
2713 *cookie_size = NULL_TREE;
2716 /* Build the CALL_EXPR. */
2717 return build_over_call (cand, LOOKUP_NORMAL);
2721 build_object_call (tree obj, tree args)
2723 struct z_candidate *candidates = 0, *cand;
2724 tree fns, convs, mem_args = NULL_TREE;
2725 tree type = TREE_TYPE (obj);
2728 if (TYPE_PTRMEMFUNC_P (type))
2730 /* It's no good looking for an overloaded operator() on a
2731 pointer-to-member-function. */
2732 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2733 return error_mark_node;
2736 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2737 if (fns == error_mark_node)
2738 return error_mark_node;
2740 args = resolve_args (args);
2742 if (args == error_mark_node)
2743 return error_mark_node;
2747 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2748 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2750 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2752 tree fn = OVL_CURRENT (fns);
2753 if (TREE_CODE (fn) == TEMPLATE_DECL)
2754 add_template_candidate (&candidates, fn, base, NULL_TREE,
2755 mem_args, NULL_TREE,
2758 LOOKUP_NORMAL, DEDUCE_CALL);
2760 add_function_candidate
2761 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
2762 TYPE_BINFO (type), LOOKUP_NORMAL);
2766 convs = lookup_conversions (type);
2768 for (; convs; convs = TREE_CHAIN (convs))
2770 tree fns = TREE_VALUE (convs);
2771 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2773 if ((TREE_CODE (totype) == POINTER_TYPE
2774 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2775 || (TREE_CODE (totype) == REFERENCE_TYPE
2776 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2777 || (TREE_CODE (totype) == REFERENCE_TYPE
2778 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2779 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2780 for (; fns; fns = OVL_NEXT (fns))
2782 tree fn = OVL_CURRENT (fns);
2783 if (TREE_CODE (fn) == TEMPLATE_DECL)
2784 add_template_conv_candidate
2785 (&candidates, fn, obj, args, totype,
2786 /*access_path=*/NULL_TREE,
2787 /*conversion_path=*/NULL_TREE);
2789 add_conv_candidate (&candidates, fn, obj, args,
2790 /*conversion_path=*/NULL_TREE,
2791 /*access_path=*/NULL_TREE);
2795 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2798 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2799 print_z_candidates (candidates);
2800 return error_mark_node;
2803 cand = tourney (candidates);
2806 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2807 print_z_candidates (candidates);
2808 return error_mark_node;
2811 /* Since cand->fn will be a type, not a function, for a conversion
2812 function, we must be careful not to unconditionally look at
2814 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2815 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2816 return build_over_call (cand, LOOKUP_NORMAL);
2818 obj = convert_like_with_context
2819 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2822 return build_function_call (obj, args);
2826 op_error (enum tree_code code, enum tree_code code2,
2827 tree arg1, tree arg2, tree arg3, const char *problem)
2831 if (code == MODIFY_EXPR)
2832 opname = assignment_operator_name_info[code2].name;
2834 opname = operator_name_info[code].name;
2839 error ("%s for ternary 'operator?:' in '%E ? %E : %E'",
2840 problem, arg1, arg2, arg3);
2843 case POSTINCREMENT_EXPR:
2844 case POSTDECREMENT_EXPR:
2845 error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname);
2849 error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2);
2854 error ("%s for '%s' in '%s %E'", problem, opname, opname, arg1);
2859 error ("%s for 'operator%s' in '%E %s %E'",
2860 problem, opname, arg1, opname, arg2);
2862 error ("%s for 'operator%s' in '%s%E'",
2863 problem, opname, opname, arg1);
2868 /* Return the implicit conversion sequence that could be used to
2869 convert E1 to E2 in [expr.cond]. */
2872 conditional_conversion (tree e1, tree e2)
2874 tree t1 = non_reference (TREE_TYPE (e1));
2875 tree t2 = non_reference (TREE_TYPE (e2));
2881 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2882 implicitly converted (clause _conv_) to the type "reference to
2883 T2", subject to the constraint that in the conversion the
2884 reference must bind directly (_dcl.init.ref_) to E1. */
2885 if (real_lvalue_p (e2))
2887 conv = implicit_conversion (build_reference_type (t2),
2890 LOOKUP_NO_TEMP_BIND);
2897 If E1 and E2 have class type, and the underlying class types are
2898 the same or one is a base class of the other: E1 can be converted
2899 to match E2 if the class of T2 is the same type as, or a base
2900 class of, the class of T1, and the cv-qualification of T2 is the
2901 same cv-qualification as, or a greater cv-qualification than, the
2902 cv-qualification of T1. If the conversion is applied, E1 is
2903 changed to an rvalue of type T2 that still refers to the original
2904 source class object (or the appropriate subobject thereof). */
2905 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2906 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
2908 if (good_base && at_least_as_qualified_p (t2, t1))
2910 conv = build1 (IDENTITY_CONV, t1, e1);
2911 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2912 TYPE_MAIN_VARIANT (t2)))
2913 conv = build_conv (BASE_CONV, t2, conv);
2915 conv = build_conv (RVALUE_CONV, t2, conv);
2924 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
2925 converted to the type that expression E2 would have if E2 were
2926 converted to an rvalue (or the type it has, if E2 is an rvalue). */
2927 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2930 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2931 arguments to the conditional expression. */
2934 build_conditional_expr (tree arg1, tree arg2, tree arg3)
2939 tree result_type = NULL_TREE;
2940 bool lvalue_p = true;
2941 struct z_candidate *candidates = 0;
2942 struct z_candidate *cand;
2944 /* As a G++ extension, the second argument to the conditional can be
2945 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2946 c'.) If the second operand is omitted, make sure it is
2947 calculated only once. */
2951 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2953 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
2954 if (real_lvalue_p (arg1))
2955 arg2 = arg1 = stabilize_reference (arg1);
2957 arg2 = arg1 = save_expr (arg1);
2962 The first expr ession is implicitly converted to bool (clause
2964 arg1 = perform_implicit_conversion (boolean_type_node, arg1);
2966 /* If something has already gone wrong, just pass that fact up the
2968 if (error_operand_p (arg1)
2969 || error_operand_p (arg2)
2970 || error_operand_p (arg3))
2971 return error_mark_node;
2975 If either the second or the third operand has type (possibly
2976 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2977 array-to-pointer (_conv.array_), and function-to-pointer
2978 (_conv.func_) standard conversions are performed on the second
2979 and third operands. */
2980 arg2_type = TREE_TYPE (arg2);
2981 arg3_type = TREE_TYPE (arg3);
2982 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2984 /* Do the conversions. We don't these for `void' type arguments
2985 since it can't have any effect and since decay_conversion
2986 does not handle that case gracefully. */
2987 if (!VOID_TYPE_P (arg2_type))
2988 arg2 = decay_conversion (arg2);
2989 if (!VOID_TYPE_P (arg3_type))
2990 arg3 = decay_conversion (arg3);
2991 arg2_type = TREE_TYPE (arg2);
2992 arg3_type = TREE_TYPE (arg3);
2996 One of the following shall hold:
2998 --The second or the third operand (but not both) is a
2999 throw-expression (_except.throw_); the result is of the
3000 type of the other and is an rvalue.
3002 --Both the second and the third operands have type void; the
3003 result is of type void and is an rvalue.
3005 We must avoid calling force_rvalue for expressions of type
3006 "void" because it will complain that their value is being
3008 if (TREE_CODE (arg2) == THROW_EXPR
3009 && TREE_CODE (arg3) != THROW_EXPR)
3011 if (!VOID_TYPE_P (arg3_type))
3012 arg3 = force_rvalue (arg3);
3013 arg3_type = TREE_TYPE (arg3);
3014 result_type = arg3_type;
3016 else if (TREE_CODE (arg2) != THROW_EXPR
3017 && TREE_CODE (arg3) == THROW_EXPR)
3019 if (!VOID_TYPE_P (arg2_type))
3020 arg2 = force_rvalue (arg2);
3021 arg2_type = TREE_TYPE (arg2);
3022 result_type = arg2_type;
3024 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3025 result_type = void_type_node;
3028 error ("`%E' has type `void' and is not a throw-expression",
3029 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3030 return error_mark_node;
3034 goto valid_operands;
3038 Otherwise, if the second and third operand have different types,
3039 and either has (possibly cv-qualified) class type, an attempt is
3040 made to convert each of those operands to the type of the other. */
3041 else if (!same_type_p (arg2_type, arg3_type)
3042 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3044 tree conv2 = conditional_conversion (arg2, arg3);
3045 tree conv3 = conditional_conversion (arg3, arg2);
3049 If both can be converted, or one can be converted but the
3050 conversion is ambiguous, the program is ill-formed. If
3051 neither can be converted, the operands are left unchanged and
3052 further checking is performed as described below. If exactly
3053 one conversion is possible, that conversion is applied to the
3054 chosen operand and the converted operand is used in place of
3055 the original operand for the remainder of this section. */
3056 if ((conv2 && !ICS_BAD_FLAG (conv2)
3057 && conv3 && !ICS_BAD_FLAG (conv3))
3058 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3059 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3061 error ("operands to ?: have different types");
3062 return error_mark_node;
3064 else if (conv2 && !ICS_BAD_FLAG (conv2))
3066 arg2 = convert_like (conv2, arg2);
3067 arg2 = convert_from_reference (arg2);
3068 arg2_type = TREE_TYPE (arg2);
3070 else if (conv3 && !ICS_BAD_FLAG (conv3))
3072 arg3 = convert_like (conv3, arg3);
3073 arg3 = convert_from_reference (arg3);
3074 arg3_type = TREE_TYPE (arg3);
3077 /* If, after the conversion, both operands have class type,
3078 treat the cv-qualification of both operands as if it were the
3079 union of the cv-qualification of the operands.
3081 The standard is not clear about what to do in this
3082 circumstance. For example, if the first operand has type
3083 "const X" and the second operand has a user-defined
3084 conversion to "volatile X", what is the type of the second
3085 operand after this step? Making it be "const X" (matching
3086 the first operand) seems wrong, as that discards the
3087 qualification without actuall performing a copy. Leaving it
3088 as "volatile X" seems wrong as that will result in the
3089 conditional expression failing altogether, even though,
3090 according to this step, the one operand could be converted to
3091 the type of the other. */
3092 if ((conv2 || conv3)
3093 && CLASS_TYPE_P (arg2_type)
3094 && TYPE_QUALS (arg2_type) != TYPE_QUALS (arg3_type))
3095 arg2_type = arg3_type =
3096 cp_build_qualified_type (arg2_type,
3097 TYPE_QUALS (arg2_type)
3098 | TYPE_QUALS (arg3_type));
3103 If the second and third operands are lvalues and have the same
3104 type, the result is of that type and is an lvalue. */
3105 if (real_lvalue_p (arg2)
3106 && real_lvalue_p (arg3)
3107 && same_type_p (arg2_type, arg3_type))
3109 result_type = arg2_type;
3110 goto valid_operands;
3115 Otherwise, the result is an rvalue. If the second and third
3116 operand do not have the same type, and either has (possibly
3117 cv-qualified) class type, overload resolution is used to
3118 determine the conversions (if any) to be applied to the operands
3119 (_over.match.oper_, _over.built_). */
3121 if (!same_type_p (arg2_type, arg3_type)
3122 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3128 /* Rearrange the arguments so that add_builtin_candidate only has
3129 to know about two args. In build_builtin_candidates, the
3130 arguments are unscrambled. */
3134 add_builtin_candidates (&candidates,
3137 ansi_opname (COND_EXPR),
3143 If the overload resolution fails, the program is
3145 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3148 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3149 print_z_candidates (candidates);
3150 return error_mark_node;
3152 cand = tourney (candidates);
3155 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3156 print_z_candidates (candidates);
3157 return error_mark_node;
3162 Otherwise, the conversions thus determined are applied, and
3163 the converted operands are used in place of the original
3164 operands for the remainder of this section. */
3165 conv = TREE_VEC_ELT (cand->convs, 0);
3166 arg1 = convert_like (conv, arg1);
3167 conv = TREE_VEC_ELT (cand->convs, 1);
3168 arg2 = convert_like (conv, arg2);
3169 conv = TREE_VEC_ELT (cand->convs, 2);
3170 arg3 = convert_like (conv, arg3);
3175 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3176 and function-to-pointer (_conv.func_) standard conversions are
3177 performed on the second and third operands.
3179 We need to force the lvalue-to-rvalue conversion here for class types,
3180 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3181 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3184 arg2 = force_rvalue (arg2);
3185 if (!CLASS_TYPE_P (arg2_type))
3186 arg2_type = TREE_TYPE (arg2);
3188 arg3 = force_rvalue (arg3);
3189 if (!CLASS_TYPE_P (arg2_type))
3190 arg3_type = TREE_TYPE (arg3);
3192 if (arg2 == error_mark_node || arg3 == error_mark_node)
3193 return error_mark_node;
3197 After those conversions, one of the following shall hold:
3199 --The second and third operands have the same type; the result is of
3201 if (same_type_p (arg2_type, arg3_type))
3202 result_type = arg2_type;
3205 --The second and third operands have arithmetic or enumeration
3206 type; the usual arithmetic conversions are performed to bring
3207 them to a common type, and the result is of that type. */
3208 else if ((ARITHMETIC_TYPE_P (arg2_type)
3209 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3210 && (ARITHMETIC_TYPE_P (arg3_type)
3211 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3213 /* In this case, there is always a common type. */
3214 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3217 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3218 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3219 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3220 arg2_type, arg3_type);
3221 else if (extra_warnings
3222 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3223 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3224 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3225 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3226 warning ("enumeral and non-enumeral type in conditional expression");
3228 arg2 = perform_implicit_conversion (result_type, arg2);
3229 arg3 = perform_implicit_conversion (result_type, arg3);
3233 --The second and third operands have pointer type, or one has
3234 pointer type and the other is a null pointer constant; pointer
3235 conversions (_conv.ptr_) and qualification conversions
3236 (_conv.qual_) are performed to bring them to their composite
3237 pointer type (_expr.rel_). The result is of the composite
3240 --The second and third operands have pointer to member type, or
3241 one has pointer to member type and the other is a null pointer
3242 constant; pointer to member conversions (_conv.mem_) and
3243 qualification conversions (_conv.qual_) are performed to bring
3244 them to a common type, whose cv-qualification shall match the
3245 cv-qualification of either the second or the third operand.
3246 The result is of the common type. */
3247 else if ((null_ptr_cst_p (arg2)
3248 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
3249 || (null_ptr_cst_p (arg3)
3250 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
3251 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3252 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3253 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
3255 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3256 arg3, "conditional expression");
3257 if (result_type == error_mark_node)
3258 return error_mark_node;
3259 arg2 = perform_implicit_conversion (result_type, arg2);
3260 arg3 = perform_implicit_conversion (result_type, arg3);
3265 error ("operands to ?: have different types");
3266 return error_mark_node;
3270 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3271 /* We can't use result_type below, as fold might have returned a
3274 /* Expand both sides into the same slot, hopefully the target of the
3275 ?: expression. We used to check for TARGET_EXPRs here, but now we
3276 sometimes wrap them in NOP_EXPRs so the test would fail. */
3277 if (!lvalue_p && CLASS_TYPE_P (TREE_TYPE (result)))
3278 result = get_target_expr (result);
3280 /* If this expression is an rvalue, but might be mistaken for an
3281 lvalue, we must add a NON_LVALUE_EXPR. */
3282 if (!lvalue_p && real_lvalue_p (result))
3283 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
3288 /* OPERAND is an operand to an expression. Perform necessary steps
3289 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3293 prep_operand (tree operand)
3297 operand = convert_from_reference (operand);
3298 if (CLASS_TYPE_P (TREE_TYPE (operand))
3299 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3300 /* Make sure the template type is instantiated now. */
3301 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3307 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
3308 OVERLOAD) to the CANDIDATES, returning an updated list of
3309 CANDIDATES. The ARGS are the arguments provided to the call,
3310 without any implicit object parameter. The EXPLICIT_TARGS are
3311 explicit template arguments provided. TEMPLATE_ONLY is true if
3312 only template functions should be considered. CONVERSION_PATH,
3313 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3316 add_candidates (tree fns, tree args,
3317 tree explicit_targs, bool template_only,
3318 tree conversion_path, tree access_path,
3320 struct z_candidate **candidates)
3323 tree non_static_args;
3325 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3326 /* Delay creating the implicit this parameter until it is needed. */
3327 non_static_args = NULL_TREE;
3334 fn = OVL_CURRENT (fns);
3335 /* Figure out which set of arguments to use. */
3336 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3338 /* If this function is a non-static member, prepend the implicit
3339 object parameter. */
3340 if (!non_static_args)
3341 non_static_args = tree_cons (NULL_TREE,
3342 build_this (TREE_VALUE (args)),
3344 fn_args = non_static_args;
3347 /* Otherwise, just use the list of arguments provided. */
3350 if (TREE_CODE (fn) == TEMPLATE_DECL)
3351 add_template_candidate (candidates,
3361 else if (!template_only)
3362 add_function_candidate (candidates,
3369 fns = OVL_NEXT (fns);
3374 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
3377 struct z_candidate *candidates = 0, *cand;
3378 tree arglist, fnname;
3380 enum tree_code code2 = NOP_EXPR;
3385 if (error_operand_p (arg1)
3386 || error_operand_p (arg2)
3387 || error_operand_p (arg3))
3388 return error_mark_node;
3390 if (code == MODIFY_EXPR)
3392 code2 = TREE_CODE (arg3);
3394 fnname = ansi_assopname (code2);
3397 fnname = ansi_opname (code);
3399 arg1 = prep_operand (arg1);
3405 case VEC_DELETE_EXPR:
3407 /* Use build_op_new_call and build_op_delete_call instead. */
3411 return build_object_call (arg1, arg2);
3417 arg2 = prep_operand (arg2);
3418 arg3 = prep_operand (arg3);
3420 if (code == COND_EXPR)
3422 if (arg2 == NULL_TREE
3423 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3424 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3425 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3426 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3429 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3430 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3433 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3434 arg2 = integer_zero_node;
3436 arglist = NULL_TREE;
3438 arglist = tree_cons (NULL_TREE, arg3, arglist);
3440 arglist = tree_cons (NULL_TREE, arg2, arglist);
3441 arglist = tree_cons (NULL_TREE, arg1, arglist);
3443 /* Add namespace-scope operators to the list of functions to
3445 add_candidates (lookup_function_nonclass (fnname, arglist),
3446 arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3447 flags, &candidates);
3448 /* Add class-member operators to the candidate set. */
3449 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
3453 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3454 if (fns == error_mark_node)
3457 add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3459 BASELINK_BINFO (fns),
3460 TYPE_BINFO (TREE_TYPE (arg1)),
3461 flags, &candidates);
3464 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3465 to know about two args; a builtin candidate will always have a first
3466 parameter of type bool. We'll handle that in
3467 build_builtin_candidate. */
3468 if (code == COND_EXPR)
3478 args[2] = NULL_TREE;
3481 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
3487 /* For these, the built-in candidates set is empty
3488 [over.match.oper]/3. We don't want non-strict matches
3489 because exact matches are always possible with built-in
3490 operators. The built-in candidate set for COMPONENT_REF
3491 would be empty too, but since there are no such built-in
3492 operators, we accept non-strict matches for them. */
3497 strict_p = pedantic;
3501 candidates = splice_viable (candidates, strict_p, &any_viable_p);
3506 case POSTINCREMENT_EXPR:
3507 case POSTDECREMENT_EXPR:
3508 /* Look for an `operator++ (int)'. If they didn't have
3509 one, then we fall back to the old way of doing things. */
3510 if (flags & LOOKUP_COMPLAIN)
3511 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3513 operator_name_info[code].name);
3514 if (code == POSTINCREMENT_EXPR)
3515 code = PREINCREMENT_EXPR;
3517 code = PREDECREMENT_EXPR;
3518 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
3521 /* The caller will deal with these. */
3530 if (flags & LOOKUP_COMPLAIN)
3532 op_error (code, code2, arg1, arg2, arg3, "no match");
3533 print_z_candidates (candidates);
3535 return error_mark_node;
3538 cand = tourney (candidates);
3541 if (flags & LOOKUP_COMPLAIN)
3543 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3544 print_z_candidates (candidates);
3546 return error_mark_node;
3549 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3552 *overloaded_p = true;
3555 && fnname == ansi_assopname (NOP_EXPR)
3556 && DECL_ARTIFICIAL (cand->fn)
3558 && ! candidates->next->next)
3560 warning ("using synthesized `%#D' for copy assignment",
3562 cp_warning_at (" where cfront would use `%#D'",
3564 ? candidates->next->fn
3568 return build_over_call (cand, LOOKUP_NORMAL);
3571 /* Check for comparison of different enum types. */
3580 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3581 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3582 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3583 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3585 warning ("comparison between `%#T' and `%#T'",
3586 TREE_TYPE (arg1), TREE_TYPE (arg2));
3593 /* We need to strip any leading REF_BIND so that bitfields don't cause
3594 errors. This should not remove any important conversions, because
3595 builtins don't apply to class objects directly. */
3596 conv = TREE_VEC_ELT (cand->convs, 0);
3597 if (TREE_CODE (conv) == REF_BIND)
3598 conv = TREE_OPERAND (conv, 0);
3599 arg1 = convert_like (conv, arg1);
3602 conv = TREE_VEC_ELT (cand->convs, 1);
3603 if (TREE_CODE (conv) == REF_BIND)
3604 conv = TREE_OPERAND (conv, 0);
3605 arg2 = convert_like (conv, arg2);
3609 conv = TREE_VEC_ELT (cand->convs, 2);
3610 if (TREE_CODE (conv) == REF_BIND)
3611 conv = TREE_OPERAND (conv, 0);
3612 arg3 = convert_like (conv, arg3);
3619 return build_modify_expr (arg1, code2, arg2);
3622 return build_indirect_ref (arg1, "unary *");
3627 case TRUNC_DIV_EXPR:
3638 case TRUNC_MOD_EXPR:
3642 case TRUTH_ANDIF_EXPR:
3643 case TRUTH_ORIF_EXPR:
3644 return cp_build_binary_op (code, arg1, arg2);
3649 case TRUTH_NOT_EXPR:
3650 case PREINCREMENT_EXPR:
3651 case POSTINCREMENT_EXPR:
3652 case PREDECREMENT_EXPR:
3653 case POSTDECREMENT_EXPR:
3656 return build_unary_op (code, arg1, candidates != 0);
3659 return build_array_ref (arg1, arg2);
3662 return build_conditional_expr (arg1, arg2, arg3);
3665 return build_m_component_ref
3666 (build_indirect_ref (arg1, NULL), arg2);
3668 /* The caller will deal with these. */
3680 /* Build a call to operator delete. This has to be handled very specially,
3681 because the restrictions on what signatures match are different from all
3682 other call instances. For a normal delete, only a delete taking (void *)
3683 or (void *, size_t) is accepted. For a placement delete, only an exact
3684 match with the placement new is accepted.
3686 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3687 ADDR is the pointer to be deleted.
3688 SIZE is the size of the memory block to be deleted.
3689 FLAGS are the usual overloading flags.
3690 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3693 build_op_delete_call (enum tree_code code, tree addr, tree size,
3694 int flags, tree placement)
3696 tree fn = NULL_TREE;
3697 tree fns, fnname, argtypes, args, type;
3700 if (addr == error_mark_node)
3701 return error_mark_node;
3703 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
3705 fnname = ansi_opname (code);
3707 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3710 If the result of the lookup is ambiguous or inaccessible, or if
3711 the lookup selects a placement deallocation function, the
3712 program is ill-formed.
3714 Therefore, we ask lookup_fnfields to complain about ambiguity. */
3716 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3717 if (fns == error_mark_node)
3718 return error_mark_node;
3723 if (fns == NULL_TREE)
3724 fns = lookup_name_nonclass (fnname);
3731 /* Find the allocation function that is being called. */
3732 call_expr = placement;
3733 /* Extract the function. */
3734 alloc_fn = get_callee_fndecl (call_expr);
3735 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3736 /* Then the second parm type. */
3737 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3738 /* Also the second argument. */
3739 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3743 /* First try it without the size argument. */
3744 argtypes = void_list_node;
3748 /* Strip const and volatile from addr. */
3749 addr = cp_convert (ptr_type_node, addr);
3751 /* We make two tries at finding a matching `operator delete'. On
3752 the first pass, we look for a one-operator (or placement)
3753 operator delete. If we're not doing placement delete, then on
3754 the second pass we look for a two-argument delete. */
3755 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3757 /* Go through the `operator delete' functions looking for one
3758 with a matching type. */
3759 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3765 /* The first argument must be "void *". */
3766 t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
3767 if (!same_type_p (TREE_VALUE (t), ptr_type_node))
3770 /* On the first pass, check the rest of the arguments. */
3776 if (!same_type_p (TREE_VALUE (a), TREE_VALUE (t)))
3784 /* On the second pass, the second argument must be
3787 && same_type_p (TREE_VALUE (t), sizetype)
3788 && TREE_CHAIN (t) == void_list_node)
3792 /* If we found a match, we're done. */
3797 /* If we have a matching function, call it. */
3800 /* Make sure we have the actual function, and not an
3802 fn = OVL_CURRENT (fn);
3804 /* If the FN is a member function, make sure that it is
3806 if (DECL_CLASS_SCOPE_P (fn))
3807 perform_or_defer_access_check (TYPE_BINFO (type), fn);
3810 args = tree_cons (NULL_TREE, addr, args);
3812 args = tree_cons (NULL_TREE, addr,
3813 build_tree_list (NULL_TREE, size));
3817 /* The placement args might not be suitable for overload
3818 resolution at this point, so build the call directly. */
3820 return build_cxx_call (fn, args, args);
3823 return build_function_call (fn, args);
3826 /* If we are doing placement delete we do nothing if we don't find a
3827 matching op delete. */
3831 error ("no suitable `operator %s' for `%T'",
3832 operator_name_info[(int)code].name, 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 my_friendly_assert (TREE_CODE (basetype_path) == TREE_VEC, 20030624);
3845 if (!accessible_p (basetype_path, decl))
3847 if (TREE_PRIVATE (decl))
3848 cp_error_at ("`%+#D' is private", decl);
3849 else if (TREE_PROTECTED (decl))
3850 cp_error_at ("`%+#D' is protected", decl);
3852 cp_error_at ("`%+#D' is inaccessible", decl);
3853 error ("within this context");
3860 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
3861 bitwise or of LOOKUP_* values. If any errors are warnings are
3862 generated, set *DIAGNOSTIC_FN to "error" or "warning",
3863 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
3867 build_temp (tree expr, tree type, int flags,
3868 void (**diagnostic_fn)(const char *, ...))
3872 savew = warningcount, savee = errorcount;
3873 expr = build_special_member_call (NULL_TREE,
3874 complete_ctor_identifier,
3875 build_tree_list (NULL_TREE, expr),
3878 if (warningcount > savew)
3879 *diagnostic_fn = warning;
3880 else if (errorcount > savee)
3881 *diagnostic_fn = error;
3883 *diagnostic_fn = NULL;
3888 /* Perform the conversions in CONVS on the expression EXPR. FN and
3889 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3890 indicates the `this' argument of a method. INNER is nonzero when
3891 being called to continue a conversion chain. It is negative when a
3892 reference binding will be applied, positive otherwise. If
3893 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
3894 conversions will be emitted if appropriate. */
3897 convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
3898 bool issue_conversion_warnings)
3900 tree totype = TREE_TYPE (convs);
3901 void (*diagnostic_fn)(const char *, ...);
3903 if (ICS_BAD_FLAG (convs)
3904 && TREE_CODE (convs) != USER_CONV
3905 && TREE_CODE (convs) != AMBIG_CONV
3906 && TREE_CODE (convs) != REF_BIND)
3909 for (; t; t = TREE_OPERAND (t, 0))
3911 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3913 expr = convert_like_real (t, expr, fn, argnum, 1,
3914 /*issue_conversion_warnings=*/false);
3917 else if (TREE_CODE (t) == AMBIG_CONV)
3918 return convert_like_real (t, expr, fn, argnum, 1,
3919 /*issue_conversion_warnings=*/false);
3920 else if (TREE_CODE (t) == IDENTITY_CONV)
3923 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3925 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3926 return cp_convert (totype, expr);
3929 if (issue_conversion_warnings)
3930 expr = dubious_conversion_warnings
3931 (totype, expr, "converting", fn, argnum);
3932 switch (TREE_CODE (convs))
3936 struct z_candidate *cand = USER_CONV_CAND (convs);
3937 tree convfn = cand->fn;
3940 if (DECL_CONSTRUCTOR_P (convfn))
3942 tree t = build_int_2 (0, 0);
3943 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3945 args = build_tree_list (NULL_TREE, expr);
3946 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3947 || DECL_HAS_VTT_PARM_P (convfn))
3948 /* We should never try to call the abstract or base constructor
3951 args = tree_cons (NULL_TREE, t, args);
3954 args = build_this (expr);
3955 expr = build_over_call (cand, LOOKUP_NORMAL);
3957 /* If this is a constructor or a function returning an aggr type,
3958 we need to build up a TARGET_EXPR. */
3959 if (DECL_CONSTRUCTOR_P (convfn))
3960 expr = build_cplus_new (totype, expr);
3962 /* The result of the call is then used to direct-initialize the object
3963 that is the destination of the copy-initialization. [dcl.init]
3965 Note that this step is not reflected in the conversion sequence;
3966 it affects the semantics when we actually perform the
3967 conversion, but is not considered during overload resolution.
3969 If the target is a class, that means call a ctor. */
3970 if (IS_AGGR_TYPE (totype)
3971 && (inner >= 0 || !lvalue_p (expr)))
3975 /* Core issue 84, now a DR, says that we don't
3976 allow UDCs for these args (which deliberately
3977 breaks copy-init of an auto_ptr<Base> from an
3978 auto_ptr<Derived>). */
3979 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
3986 (" initializing argument %P of `%D' from result of `%D'",
3987 argnum, fn, convfn);
3990 (" initializing temporary from result of `%D'", convfn);
3992 expr = build_cplus_new (totype, expr);
3997 if (type_unknown_p (expr))
3998 expr = instantiate_type (totype, expr, tf_error | tf_warning);
3999 /* Convert a non-array constant variable to its underlying
4000 value, unless we are about to bind it to a reference, in
4001 which case we need to leave it as an lvalue. */
4003 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4004 expr = decl_constant_value (expr);
4005 if (CHECK_COPY_CONSTRUCTOR_P (convs))
4006 /* Generate a temporary copy purely to generate the required
4010 (build_qualified_type (totype, TYPE_QUAL_CONST)),
4011 totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING, &diagnostic_fn);
4014 /* Call build_user_type_conversion again for the error. */
4015 return build_user_type_conversion
4016 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
4022 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
4023 TREE_CODE (convs) == REF_BIND ? -1 : 1,
4024 /*issue_conversion_warnings=*/false);
4025 if (expr == error_mark_node)
4026 return error_mark_node;
4028 switch (TREE_CODE (convs))
4031 if (! IS_AGGR_TYPE (totype))
4033 /* Else fall through. */
4035 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4037 /* We are going to bind a reference directly to a base-class
4038 subobject of EXPR. */
4039 if (CHECK_COPY_CONSTRUCTOR_P (convs))
4040 /* Generate a temporary copy purely to generate the required
4042 build_temp (build_dummy_object (TREE_TYPE (expr)),
4044 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4046 /* Build an expression for `*((base*) &expr)'. */
4047 expr = build_unary_op (ADDR_EXPR, expr, 0);
4048 expr = perform_implicit_conversion (build_pointer_type (totype),
4050 expr = build_indirect_ref (expr, "implicit conversion");
4054 /* Copy-initialization where the cv-unqualified version of the source
4055 type is the same class as, or a derived class of, the class of the
4056 destination [is treated as direct-initialization]. [dcl.init] */
4057 expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4059 if (diagnostic_fn && fn)
4060 diagnostic_fn (" initializing argument %P of `%D'", argnum, fn);
4061 return build_cplus_new (totype, expr);
4065 tree ref_type = totype;
4067 /* If necessary, create a temporary. */
4068 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
4070 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4072 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4074 /* If the reference is volatile or non-const, we
4075 cannot create a temporary. */
4076 cp_lvalue_kind lvalue = real_lvalue_p (expr);
4078 if (lvalue & clk_bitfield)
4079 error ("cannot bind bitfield `%E' to `%T'",
4081 else if (lvalue & clk_packed)
4082 error ("cannot bind packed field `%E' to `%T'",
4085 error ("cannot bind rvalue `%E' to `%T'", expr, ref_type);
4086 return error_mark_node;
4088 expr = build_target_expr_with_type (expr, type);
4091 /* Take the address of the thing to which we will bind the
4093 expr = build_unary_op (ADDR_EXPR, expr, 1);
4094 if (expr == error_mark_node)
4095 return error_mark_node;
4097 /* Convert it to a pointer to the type referred to by the
4098 reference. This will adjust the pointer if a derived to
4099 base conversion is being performed. */
4100 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4102 /* Convert the pointer to the desired reference type. */
4103 return build_nop (ref_type, expr);
4107 return decay_conversion (expr);
4110 /* Warn about deprecated conversion if appropriate. */
4111 string_conv_p (totype, expr, 1);
4117 return ocp_convert (totype, expr, CONV_IMPLICIT,
4118 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4121 /* Build a call to __builtin_trap. */
4124 call_builtin_trap (void)
4126 tree fn = IDENTIFIER_GLOBAL_VALUE (get_identifier ("__builtin_trap"));
4128 my_friendly_assert (fn != NULL, 20030927);
4129 fn = build_call (fn, NULL_TREE);
4133 /* ARG is being passed to a varargs function. Perform any conversions
4134 required. Return the converted value. */
4137 convert_arg_to_ellipsis (tree arg)
4141 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4142 standard conversions are performed. */
4143 arg = decay_conversion (arg);
4146 If the argument has integral or enumeration type that is subject
4147 to the integral promotions (_conv.prom_), or a floating point
4148 type that is subject to the floating point promotion
4149 (_conv.fpprom_), the value of the argument is converted to the
4150 promoted type before the call. */
4151 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4152 && (TYPE_PRECISION (TREE_TYPE (arg))
4153 < TYPE_PRECISION (double_type_node)))
4154 arg = convert_to_real (double_type_node, arg);
4155 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4156 arg = perform_integral_promotions (arg);
4158 arg = require_complete_type (arg);
4160 if (arg != error_mark_node
4161 && !pod_type_p (TREE_TYPE (arg)))
4163 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4164 here and do a bitwise copy, but now cp_expr_size will abort if we
4166 If the call appears in the context of a sizeof expression,
4167 there is no need to emit a warning, since the expression won't be
4168 evaluated. We keep the builtin_trap just as a safety check. */
4169 if (!skip_evaluation)
4170 warning ("cannot pass objects of non-POD type `%#T' through `...'; "
4171 "call will abort at runtime", TREE_TYPE (arg));
4172 arg = call_builtin_trap ();
4173 arg = build (COMPOUND_EXPR, integer_type_node, arg,
4180 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4183 build_x_va_arg (tree expr, tree type)
4185 if (processing_template_decl)
4186 return build_min (VA_ARG_EXPR, type, expr);
4188 type = complete_type_or_else (type, NULL_TREE);
4190 if (expr == error_mark_node || !type)
4191 return error_mark_node;
4193 if (! pod_type_p (type))
4195 /* Undefined behavior [expr.call] 5.2.2/7. */
4196 warning ("cannot receive objects of non-POD type `%#T' through `...'; \
4197 call will abort at runtime",
4199 expr = convert (build_pointer_type (type), null_node);
4200 expr = build (COMPOUND_EXPR, TREE_TYPE (expr),
4201 call_builtin_trap (), expr);
4202 expr = build_indirect_ref (expr, NULL);
4206 return build_va_arg (expr, type);
4209 /* TYPE has been given to va_arg. Apply the default conversions which
4210 would have happened when passed via ellipsis. Return the promoted
4211 type, or the passed type if there is no change. */
4214 cxx_type_promotes_to (tree type)
4218 /* Perform the array-to-pointer and function-to-pointer
4220 type = type_decays_to (type);
4222 promote = type_promotes_to (type);
4223 if (same_type_p (type, promote))
4229 /* ARG is a default argument expression being passed to a parameter of
4230 the indicated TYPE, which is a parameter to FN. Do any required
4231 conversions. Return the converted value. */
4234 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4236 /* If the ARG is an unparsed default argument expression, the
4237 conversion cannot be performed. */
4238 if (TREE_CODE (arg) == DEFAULT_ARG)
4240 error ("the default argument for parameter %d of `%D' has "
4241 "not yet been parsed",
4243 return error_mark_node;
4246 if (fn && DECL_TEMPLATE_INFO (fn))
4247 arg = tsubst_default_argument (fn, type, arg);
4249 arg = break_out_target_exprs (arg);
4251 if (TREE_CODE (arg) == CONSTRUCTOR)
4253 arg = digest_init (type, arg, 0);
4254 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4255 "default argument", fn, parmnum);
4259 /* This could get clobbered by the following call. */
4260 if (TREE_HAS_CONSTRUCTOR (arg))
4261 arg = copy_node (arg);
4263 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4264 "default argument", fn, parmnum);
4265 arg = convert_for_arg_passing (type, arg);
4271 /* Returns the type which will really be used for passing an argument of
4275 type_passed_as (tree type)
4277 /* Pass classes with copy ctors by invisible reference. */
4278 if (TREE_ADDRESSABLE (type))
4279 type = build_reference_type (type);
4280 else if (PROMOTE_PROTOTYPES
4281 && INTEGRAL_TYPE_P (type)
4282 && COMPLETE_TYPE_P (type)
4283 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4284 TYPE_SIZE (integer_type_node)))
4285 type = integer_type_node;
4290 /* Actually perform the appropriate conversion. */
4293 convert_for_arg_passing (tree type, tree val)
4295 if (val == error_mark_node)
4297 /* Pass classes with copy ctors by invisible reference. */
4298 else if (TREE_ADDRESSABLE (type))
4299 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4300 else if (PROMOTE_PROTOTYPES
4301 && INTEGRAL_TYPE_P (type)
4302 && COMPLETE_TYPE_P (type)
4303 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4304 TYPE_SIZE (integer_type_node)))
4305 val = perform_integral_promotions (val);
4309 /* Returns true iff FN is a function with magic varargs, i.e. ones for
4310 which no conversions at all should be done. This is true for some
4311 builtins which don't act like normal functions. */
4314 magic_varargs_p (tree fn)
4316 if (DECL_BUILT_IN (fn))
4317 switch (DECL_FUNCTION_CODE (fn))
4319 case BUILT_IN_CLASSIFY_TYPE:
4320 case BUILT_IN_CONSTANT_P:
4321 case BUILT_IN_NEXT_ARG:
4322 case BUILT_IN_STDARG_START:
4323 case BUILT_IN_VA_START:
4332 /* Subroutine of the various build_*_call functions. Overload resolution
4333 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4334 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4335 bitmask of various LOOKUP_* flags which apply to the call itself. */
4338 build_over_call (struct z_candidate *cand, int flags)
4341 tree args = cand->args;
4342 tree convs = cand->convs;
4343 tree converted_args = NULL_TREE;
4344 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4345 tree conv, arg, val;
4349 /* In a template, there is no need to perform all of the work that
4350 is normally done. We are only interested in the type of the call
4351 expression, i.e., the return type of the function. Any semantic
4352 errors will be deferred until the template is instantiated. */
4353 if (processing_template_decl)
4357 return_type = TREE_TYPE (TREE_TYPE (fn));
4358 expr = build (CALL_EXPR, return_type, fn, args);
4359 if (TREE_THIS_VOLATILE (fn) && cfun)
4360 current_function_returns_abnormally = 1;
4361 if (!VOID_TYPE_P (return_type))
4362 require_complete_type (return_type);
4363 return convert_from_reference (expr);
4366 /* Give any warnings we noticed during overload resolution. */
4368 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4369 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4371 if (DECL_FUNCTION_MEMBER_P (fn))
4373 /* If FN is a template function, two cases must be considered.
4378 template <class T> void f();
4380 template <class T> struct B {
4384 struct C : A, B<int> {
4386 using B<int>::g; // #2
4389 In case #1 where `A::f' is a member template, DECL_ACCESS is
4390 recorded in the primary template but not in its specialization.
4391 We check access of FN using its primary template.
4393 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4394 because it is a member of class template B, DECL_ACCESS is
4395 recorded in the specialization `B<int>::g'. We cannot use its
4396 primary template because `B<T>::g' and `B<int>::g' may have
4397 different access. */
4398 if (DECL_TEMPLATE_INFO (fn)
4399 && is_member_template (DECL_TI_TEMPLATE (fn)))
4400 perform_or_defer_access_check (cand->access_path,
4401 DECL_TI_TEMPLATE (fn));
4403 perform_or_defer_access_check (cand->access_path, fn);
4406 if (args && TREE_CODE (args) != TREE_LIST)
4407 args = build_tree_list (NULL_TREE, args);
4410 /* The implicit parameters to a constructor are not considered by overload
4411 resolution, and must be of the proper type. */
4412 if (DECL_CONSTRUCTOR_P (fn))
4414 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4415 arg = TREE_CHAIN (arg);
4416 parm = TREE_CHAIN (parm);
4417 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4418 /* We should never try to call the abstract constructor. */
4420 if (DECL_HAS_VTT_PARM_P (fn))
4422 converted_args = tree_cons
4423 (NULL_TREE, TREE_VALUE (arg), converted_args);
4424 arg = TREE_CHAIN (arg);
4425 parm = TREE_CHAIN (parm);
4428 /* Bypass access control for 'this' parameter. */
4429 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4431 tree parmtype = TREE_VALUE (parm);
4432 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4436 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4437 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4438 TREE_TYPE (argtype), fn);
4440 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4441 X is called for an object that is not of type X, or of a type
4442 derived from X, the behavior is undefined.
4444 So we can assume that anything passed as 'this' is non-null, and
4445 optimize accordingly. */
4446 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4447 /* Convert to the base in which the function was declared. */
4448 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4449 converted_arg = build_base_path (PLUS_EXPR,
4451 cand->conversion_path,
4453 /* Check that the base class is accessible. */
4454 if (!accessible_base_p (TREE_TYPE (argtype),
4455 BINFO_TYPE (cand->conversion_path)))
4456 error ("`%T' is not an accessible base of `%T'",
4457 BINFO_TYPE (cand->conversion_path),
4458 TREE_TYPE (argtype));
4459 /* If fn was found by a using declaration, the conversion path
4460 will be to the derived class, not the base declaring fn. We
4461 must convert from derived to base. */
4462 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4463 TREE_TYPE (parmtype), ba_ignore, NULL);
4464 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4467 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4468 parm = TREE_CHAIN (parm);
4469 arg = TREE_CHAIN (arg);
4475 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4477 tree type = TREE_VALUE (parm);
4479 conv = TREE_VEC_ELT (convs, i);
4480 val = convert_like_with_context
4481 (conv, TREE_VALUE (arg), fn, i - is_method);
4483 val = convert_for_arg_passing (type, val);
4484 converted_args = tree_cons (NULL_TREE, val, converted_args);
4487 /* Default arguments */
4488 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4490 = tree_cons (NULL_TREE,
4491 convert_default_arg (TREE_VALUE (parm),
4492 TREE_PURPOSE (parm),
4497 for (; arg; arg = TREE_CHAIN (arg))
4499 tree a = TREE_VALUE (arg);
4500 if (magic_varargs_p (fn))
4501 /* Do no conversions for magic varargs. */;
4503 a = convert_arg_to_ellipsis (a);
4504 converted_args = tree_cons (NULL_TREE, a, converted_args);
4507 converted_args = nreverse (converted_args);
4510 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4513 /* Avoid actually calling copy constructors and copy assignment operators,
4516 if (! flag_elide_constructors)
4517 /* Do things the hard way. */;
4518 else if (TREE_VEC_LENGTH (convs) == 1
4519 && DECL_COPY_CONSTRUCTOR_P (fn))
4522 arg = skip_artificial_parms_for (fn, converted_args);
4523 arg = TREE_VALUE (arg);
4525 /* Pull out the real argument, disregarding const-correctness. */
4527 while (TREE_CODE (targ) == NOP_EXPR
4528 || TREE_CODE (targ) == NON_LVALUE_EXPR
4529 || TREE_CODE (targ) == CONVERT_EXPR)
4530 targ = TREE_OPERAND (targ, 0);
4531 if (TREE_CODE (targ) == ADDR_EXPR)
4533 targ = TREE_OPERAND (targ, 0);
4534 if (!same_type_ignoring_top_level_qualifiers_p
4535 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4544 arg = build_indirect_ref (arg, 0);
4546 /* [class.copy]: the copy constructor is implicitly defined even if
4547 the implementation elided its use. */
4548 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4551 /* If we're creating a temp and we already have one, don't create a
4552 new one. If we're not creating a temp but we get one, use
4553 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4554 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4555 temp or an INIT_EXPR otherwise. */
4556 if (integer_zerop (TREE_VALUE (args)))
4558 if (TREE_CODE (arg) == TARGET_EXPR)
4560 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4561 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4563 else if (TREE_CODE (arg) == TARGET_EXPR
4564 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4566 tree to = stabilize_reference
4567 (build_indirect_ref (TREE_VALUE (args), 0));
4569 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4573 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4575 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4577 tree to = stabilize_reference
4578 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4579 tree type = TREE_TYPE (to);
4580 tree as_base = CLASSTYPE_AS_BASE (type);
4582 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4583 if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
4584 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4587 /* We must only copy the non-tail padding parts. Use
4588 CLASSTYPE_AS_BASE for the bitwise copy. */
4589 tree to_ptr, arg_ptr, to_as_base, arg_as_base, base_ptr_type;
4592 to_ptr = save_expr (build_unary_op (ADDR_EXPR, to, 0));
4593 arg_ptr = build_unary_op (ADDR_EXPR, arg, 0);
4595 base_ptr_type = build_pointer_type (as_base);
4596 to_as_base = build_nop (base_ptr_type, to_ptr);
4597 to_as_base = build_indirect_ref (to_as_base, 0);
4598 arg_as_base = build_nop (base_ptr_type, arg_ptr);
4599 arg_as_base = build_indirect_ref (arg_as_base, 0);
4601 save_to = build_indirect_ref (to_ptr, 0);
4603 val = build (MODIFY_EXPR, as_base, to_as_base, arg_as_base);
4604 val = convert_to_void (val, NULL);
4605 val = build (COMPOUND_EXPR, type, val, save_to);
4606 TREE_NO_UNUSED_WARNING (val) = 1;
4614 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4616 tree t, *p = &TREE_VALUE (converted_args);
4617 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4620 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4622 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4623 if (TREE_SIDE_EFFECTS (*p))
4624 *p = save_expr (*p);
4625 t = build_pointer_type (TREE_TYPE (fn));
4626 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4627 fn = build_java_interface_fn_ref (fn, *p);
4629 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4632 else if (DECL_INLINE (fn))
4633 fn = inline_conversion (fn);
4635 fn = build_addr_func (fn);
4637 return build_cxx_call (fn, args, converted_args);
4640 /* Build and return a call to FN, using the the CONVERTED_ARGS. ARGS
4641 gives the original form of the arguments. This function performs
4642 no overload resolution, conversion, or other high-level
4646 build_cxx_call(tree fn, tree args, tree converted_args)
4650 /* Recognize certain built-in functions so we can make tree-codes
4651 other than CALL_EXPR. We do this when it enables fold-const.c
4652 to do something useful. */
4653 if (TREE_CODE (fn) == ADDR_EXPR
4654 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4655 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4658 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4663 fn = build_call (fn, converted_args);
4665 /* If this call might throw an exception, note that fact. */
4666 fndecl = get_callee_fndecl (fn);
4667 if ((!fndecl || !TREE_NOTHROW (fndecl))
4668 && at_function_scope_p ()
4670 cp_function_chain->can_throw = 1;
4672 /* Some built-in function calls will be evaluated at compile-time in
4676 if (VOID_TYPE_P (TREE_TYPE (fn)))
4679 fn = require_complete_type (fn);
4680 if (fn == error_mark_node)
4681 return error_mark_node;
4683 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4684 fn = build_cplus_new (TREE_TYPE (fn), fn);
4685 return convert_from_reference (fn);
4688 static GTY(()) tree java_iface_lookup_fn;
4690 /* Make an expression which yields the address of the Java interface
4691 method FN. This is achieved by generating a call to libjava's
4692 _Jv_LookupInterfaceMethodIdx(). */
4695 build_java_interface_fn_ref (tree fn, tree instance)
4697 tree lookup_args, lookup_fn, method, idx;
4698 tree klass_ref, iface, iface_ref;
4701 if (!java_iface_lookup_fn)
4703 tree endlink = build_void_list_node ();
4704 tree t = tree_cons (NULL_TREE, ptr_type_node,
4705 tree_cons (NULL_TREE, ptr_type_node,
4706 tree_cons (NULL_TREE, java_int_type_node,
4708 java_iface_lookup_fn
4709 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4710 build_function_type (ptr_type_node, t),
4711 0, NOT_BUILT_IN, NULL, NULL_TREE);
4714 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4715 This is the first entry in the vtable. */
4716 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4719 /* Get the java.lang.Class pointer for the interface being called. */
4720 iface = DECL_CONTEXT (fn);
4721 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
4722 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4723 || DECL_CONTEXT (iface_ref) != iface)
4725 error ("could not find class$ field in java interface type `%T'",
4727 return error_mark_node;
4729 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4731 /* Determine the itable index of FN. */
4733 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4735 if (!DECL_VIRTUAL_P (method))
4741 idx = build_int_2 (i, 0);
4743 lookup_args = tree_cons (NULL_TREE, klass_ref,
4744 tree_cons (NULL_TREE, iface_ref,
4745 build_tree_list (NULL_TREE, idx)));
4746 lookup_fn = build1 (ADDR_EXPR,
4747 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4748 java_iface_lookup_fn);
4749 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4752 /* Returns the value to use for the in-charge parameter when making a
4753 call to a function with the indicated NAME. */
4756 in_charge_arg_for_name (tree name)
4758 if (name == base_ctor_identifier
4759 || name == base_dtor_identifier)
4760 return integer_zero_node;
4761 else if (name == complete_ctor_identifier)
4762 return integer_one_node;
4763 else if (name == complete_dtor_identifier)
4764 return integer_two_node;
4765 else if (name == deleting_dtor_identifier)
4766 return integer_three_node;
4768 /* This function should only be called with one of the names listed
4774 /* Build a call to a constructor, destructor, or an assignment
4775 operator for INSTANCE, an expression with class type. NAME
4776 indicates the special member function to call; ARGS are the
4777 arguments. BINFO indicates the base of INSTANCE that is to be
4778 passed as the `this' parameter to the member function called.
4780 FLAGS are the LOOKUP_* flags to use when processing the call.
4782 If NAME indicates a complete object constructor, INSTANCE may be
4783 NULL_TREE. In this case, the caller will call build_cplus_new to
4784 store the newly constructed object into a VAR_DECL. */
4787 build_special_member_call (tree instance, tree name, tree args,
4788 tree binfo, int flags)
4791 /* The type of the subobject to be constructed or destroyed. */
4794 my_friendly_assert (name == complete_ctor_identifier
4795 || name == base_ctor_identifier
4796 || name == complete_dtor_identifier
4797 || name == base_dtor_identifier
4798 || name == deleting_dtor_identifier
4799 || name == ansi_assopname (NOP_EXPR),
4801 my_friendly_assert (binfo != NULL_TREE, 20020712);
4803 class_type = BINFO_TYPE (binfo);
4805 /* Handle the special case where INSTANCE is NULL_TREE. */
4806 if (name == complete_ctor_identifier && !instance)
4808 instance = build_int_2 (0, 0);
4809 TREE_TYPE (instance) = build_pointer_type (class_type);
4810 instance = build1 (INDIRECT_REF, class_type, instance);
4814 if (name == complete_dtor_identifier
4815 || name == base_dtor_identifier
4816 || name == deleting_dtor_identifier)
4817 my_friendly_assert (args == NULL_TREE, 20020712);
4819 /* Convert to the base class, if necessary. */
4820 if (!same_type_ignoring_top_level_qualifiers_p
4821 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
4823 if (name != ansi_assopname (NOP_EXPR))
4824 /* For constructors and destructors, either the base is
4825 non-virtual, or it is virtual but we are doing the
4826 conversion from a constructor or destructor for the
4827 complete object. In either case, we can convert
4829 instance = convert_to_base_statically (instance, binfo);
4831 /* However, for assignment operators, we must convert
4832 dynamically if the base is virtual. */
4833 instance = build_base_path (PLUS_EXPR, instance,
4834 binfo, /*nonnull=*/1);
4838 my_friendly_assert (instance != NULL_TREE, 20020712);
4840 /* Resolve the name. */
4841 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4842 return error_mark_node;
4844 fns = lookup_fnfields (binfo, name, 1);
4846 /* When making a call to a constructor or destructor for a subobject
4847 that uses virtual base classes, pass down a pointer to a VTT for
4849 if ((name == base_ctor_identifier
4850 || name == base_dtor_identifier)
4851 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4856 /* If the current function is a complete object constructor
4857 or destructor, then we fetch the VTT directly.
4858 Otherwise, we look it up using the VTT we were given. */
4859 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4860 vtt = decay_conversion (vtt);
4861 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4862 build (EQ_EXPR, boolean_type_node,
4863 current_in_charge_parm, integer_zero_node),
4866 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4867 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4868 BINFO_SUBVTT_INDEX (binfo));
4870 args = tree_cons (NULL_TREE, sub_vtt, args);
4873 return build_new_method_call (instance, fns, args,
4874 TYPE_BINFO (BINFO_TYPE (binfo)),
4878 /* Return the NAME, as a C string. The NAME indicates a function that
4879 is a member of TYPE. *FREE_P is set to true if the caller must
4880 free the memory returned.
4882 Rather than go through all of this, we should simply set the names
4883 of constructors and destructors appropriately, and dispense with
4884 ctor_identifier, dtor_identifier, etc. */
4887 name_as_c_string (tree name, tree type, bool *free_p)
4891 /* Assume that we will not allocate memory. */
4893 /* Constructors and destructors are special. */
4894 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4897 = (char *) IDENTIFIER_POINTER (constructor_name (type));
4898 /* For a destructor, add the '~'. */
4899 if (name == complete_dtor_identifier
4900 || name == base_dtor_identifier
4901 || name == deleting_dtor_identifier)
4903 pretty_name = concat ("~", pretty_name, NULL);
4904 /* Remember that we need to free the memory allocated. */
4908 else if (IDENTIFIER_TYPENAME_P (name))
4910 pretty_name = concat ("operator ",
4911 type_as_string (TREE_TYPE (name),
4912 TFF_PLAIN_IDENTIFIER),
4914 /* Remember that we need to free the memory allocated. */
4918 pretty_name = (char *) IDENTIFIER_POINTER (name);
4923 /* Build a call to "INSTANCE.FN (ARGS)". */
4926 build_new_method_call (tree instance, tree fns, tree args,
4927 tree conversion_path, int flags)
4929 struct z_candidate *candidates = 0, *cand;
4930 tree explicit_targs = NULL_TREE;
4931 tree basetype = NULL_TREE;
4934 tree mem_args = NULL_TREE, instance_ptr;
4940 int template_only = 0;
4946 my_friendly_assert (instance != NULL_TREE, 20020729);
4948 if (error_operand_p (instance)
4949 || error_operand_p (fns)
4950 || args == error_mark_node)
4951 return error_mark_node;
4953 orig_instance = instance;
4957 if (processing_template_decl)
4959 instance = build_non_dependent_expr (instance);
4960 if (!BASELINK_P (fns)
4961 && TREE_CODE (fns) != PSEUDO_DTOR_EXPR
4962 && TREE_TYPE (fns) != unknown_type_node)
4963 fns = build_non_dependent_expr (fns);
4964 args = build_non_dependent_args (orig_args);
4967 /* Process the argument list. */
4969 args = resolve_args (args);
4970 if (args == error_mark_node)
4971 return error_mark_node;
4973 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4974 instance = convert_from_reference (instance);
4975 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4976 instance_ptr = build_this (instance);
4978 if (!BASELINK_P (fns))
4980 error ("call to non-function `%D'", fns);
4981 return error_mark_node;
4984 if (!conversion_path)
4985 conversion_path = BASELINK_BINFO (fns);
4986 access_binfo = BASELINK_ACCESS_BINFO (fns);
4987 optype = BASELINK_OPTYPE (fns);
4988 fns = BASELINK_FUNCTIONS (fns);
4990 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4992 explicit_targs = TREE_OPERAND (fns, 1);
4993 fns = TREE_OPERAND (fns, 0);
4997 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
4998 || TREE_CODE (fns) == TEMPLATE_DECL
4999 || TREE_CODE (fns) == OVERLOAD,
5002 /* XXX this should be handled before we get here. */
5003 if (! IS_AGGR_TYPE (basetype))
5005 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
5006 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5007 fns, instance, basetype);
5009 return error_mark_node;
5012 fn = get_first_fn (fns);
5013 name = DECL_NAME (fn);
5015 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5017 /* Callers should explicitly indicate whether they want to construct
5018 the complete object or just the part without virtual bases. */
5019 my_friendly_assert (name != ctor_identifier, 20000408);
5020 /* Similarly for destructors. */
5021 my_friendly_assert (name != dtor_identifier, 20000408);
5024 /* It's OK to call destructors on cv-qualified objects. Therefore,
5025 convert the INSTANCE_PTR to the unqualified type, if necessary. */
5026 if (DECL_DESTRUCTOR_P (fn))
5028 tree type = build_pointer_type (basetype);
5029 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
5030 instance_ptr = build_nop (type, instance_ptr);
5033 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5034 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
5036 for (fn = fns; fn; fn = OVL_NEXT (fn))
5038 tree t = OVL_CURRENT (fn);
5041 /* We can end up here for copy-init of same or base class. */
5042 if ((flags & LOOKUP_ONLYCONVERTING)
5043 && DECL_NONCONVERTING_P (t))
5046 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5047 this_arglist = mem_args;
5049 this_arglist = args;
5051 if (TREE_CODE (t) == TEMPLATE_DECL)
5052 /* A member template. */
5053 add_template_candidate (&candidates, t,
5056 this_arglist, optype,
5061 else if (! template_only)
5062 add_function_candidate (&candidates, t,
5070 candidates = splice_viable (candidates, pedantic, &any_viable_p);
5073 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
5074 if (flags & LOOKUP_SPECULATIVELY)
5076 if (!COMPLETE_TYPE_P (basetype))
5077 cxx_incomplete_type_error (instance_ptr, basetype);
5083 pretty_name = name_as_c_string (name, basetype, &free_p);
5084 error ("no matching function for call to `%T::%s(%A)%#V'",
5085 basetype, pretty_name, user_args,
5086 TREE_TYPE (TREE_TYPE (instance_ptr)));
5090 print_z_candidates (candidates);
5091 return error_mark_node;
5094 cand = tourney (candidates);
5100 pretty_name = name_as_c_string (name, basetype, &free_p);
5101 error ("call of overloaded `%s(%A)' is ambiguous", pretty_name,
5103 print_z_candidates (candidates);
5106 return error_mark_node;
5109 if (DECL_PURE_VIRTUAL_P (cand->fn)
5110 && instance == current_class_ref
5111 && (DECL_CONSTRUCTOR_P (current_function_decl)
5112 || DECL_DESTRUCTOR_P (current_function_decl))
5113 && ! (flags & LOOKUP_NONVIRTUAL)
5114 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
5115 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
5116 "abstract virtual `%#D' called from constructor"
5117 : "abstract virtual `%#D' called from destructor"),
5119 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5120 && is_dummy_object (instance_ptr))
5122 error ("cannot call member function `%D' without object", cand->fn);
5123 return error_mark_node;
5126 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5127 && resolves_to_fixed_type_p (instance, 0))
5128 flags |= LOOKUP_NONVIRTUAL;
5130 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
5131 call = build_over_call (cand, flags);
5134 call = build_over_call (cand, flags);
5135 /* In an expression of the form `a->f()' where `f' turns out to
5136 be a static member function, `a' is none-the-less evaluated. */
5137 if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
5138 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
5141 if (processing_template_decl && call != error_mark_node)
5142 return build_min_non_dep
5144 build_min_nt (COMPONENT_REF, orig_instance, orig_fns),
5149 /* Returns true iff standard conversion sequence ICS1 is a proper
5150 subsequence of ICS2. */
5153 is_subseq (tree ics1, tree ics2)
5155 /* We can assume that a conversion of the same code
5156 between the same types indicates a subsequence since we only get
5157 here if the types we are converting from are the same. */
5159 while (TREE_CODE (ics1) == RVALUE_CONV
5160 || TREE_CODE (ics1) == LVALUE_CONV)
5161 ics1 = TREE_OPERAND (ics1, 0);
5165 while (TREE_CODE (ics2) == RVALUE_CONV
5166 || TREE_CODE (ics2) == LVALUE_CONV)
5167 ics2 = TREE_OPERAND (ics2, 0);
5169 if (TREE_CODE (ics2) == USER_CONV
5170 || TREE_CODE (ics2) == AMBIG_CONV
5171 || TREE_CODE (ics2) == IDENTITY_CONV)
5172 /* At this point, ICS1 cannot be a proper subsequence of
5173 ICS2. We can get a USER_CONV when we are comparing the
5174 second standard conversion sequence of two user conversion
5178 ics2 = TREE_OPERAND (ics2, 0);
5180 if (TREE_CODE (ics2) == TREE_CODE (ics1)
5181 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
5182 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5183 TREE_TYPE (TREE_OPERAND (ics1, 0))))
5188 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
5189 be any _TYPE nodes. */
5192 is_properly_derived_from (tree derived, tree base)
5194 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5195 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5198 /* We only allow proper derivation here. The DERIVED_FROM_P macro
5199 considers every class derived from itself. */
5200 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
5201 && DERIVED_FROM_P (base, derived));
5204 /* We build the ICS for an implicit object parameter as a pointer
5205 conversion sequence. However, such a sequence should be compared
5206 as if it were a reference conversion sequence. If ICS is the
5207 implicit conversion sequence for an implicit object parameter,
5208 modify it accordingly. */
5211 maybe_handle_implicit_object (tree *ics)
5213 if (ICS_THIS_FLAG (*ics))
5215 /* [over.match.funcs]
5217 For non-static member functions, the type of the
5218 implicit object parameter is "reference to cv X"
5219 where X is the class of which the function is a
5220 member and cv is the cv-qualification on the member
5221 function declaration. */
5223 tree reference_type;
5225 /* The `this' parameter is a pointer to a class type. Make the
5226 implicit conversion talk about a reference to that same class
5228 reference_type = TREE_TYPE (TREE_TYPE (*ics));
5229 reference_type = build_reference_type (reference_type);
5231 if (TREE_CODE (t) == QUAL_CONV)
5232 t = TREE_OPERAND (t, 0);
5233 if (TREE_CODE (t) == PTR_CONV)
5234 t = TREE_OPERAND (t, 0);
5235 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5236 t = direct_reference_binding (reference_type, t);
5241 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5242 and return the type to which the reference refers. Otherwise,
5243 leave *ICS unchanged and return NULL_TREE. */
5246 maybe_handle_ref_bind (tree *ics)
5248 if (TREE_CODE (*ics) == REF_BIND)
5250 tree old_ics = *ics;
5251 tree type = TREE_TYPE (TREE_TYPE (old_ics));
5252 *ics = TREE_OPERAND (old_ics, 0);
5253 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5254 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5261 /* Compare two implicit conversion sequences according to the rules set out in
5262 [over.ics.rank]. Return values:
5264 1: ics1 is better than ics2
5265 -1: ics2 is better than ics1
5266 0: ics1 and ics2 are indistinguishable */
5269 compare_ics (tree ics1, tree ics2)
5275 tree deref_from_type1 = NULL_TREE;
5276 tree deref_from_type2 = NULL_TREE;
5277 tree deref_to_type1 = NULL_TREE;
5278 tree deref_to_type2 = NULL_TREE;
5281 /* REF_BINDING is nonzero if the result of the conversion sequence
5282 is a reference type. In that case TARGET_TYPE is the
5283 type referred to by the reference. */
5287 /* Handle implicit object parameters. */
5288 maybe_handle_implicit_object (&ics1);
5289 maybe_handle_implicit_object (&ics2);
5291 /* Handle reference parameters. */
5292 target_type1 = maybe_handle_ref_bind (&ics1);
5293 target_type2 = maybe_handle_ref_bind (&ics2);
5297 When comparing the basic forms of implicit conversion sequences (as
5298 defined in _over.best.ics_)
5300 --a standard conversion sequence (_over.ics.scs_) is a better
5301 conversion sequence than a user-defined conversion sequence
5302 or an ellipsis conversion sequence, and
5304 --a user-defined conversion sequence (_over.ics.user_) is a
5305 better conversion sequence than an ellipsis conversion sequence
5306 (_over.ics.ellipsis_). */
5307 rank1 = ICS_RANK (ics1);
5308 rank2 = ICS_RANK (ics2);
5312 else if (rank1 < rank2)
5315 if (rank1 == BAD_RANK)
5317 /* XXX Isn't this an extension? */
5318 /* Both ICS are bad. We try to make a decision based on what
5319 would have happened if they'd been good. */
5320 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5321 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5323 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5324 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5327 /* We couldn't make up our minds; try to figure it out below. */
5330 if (ICS_ELLIPSIS_FLAG (ics1))
5331 /* Both conversions are ellipsis conversions. */
5334 /* User-defined conversion sequence U1 is a better conversion sequence
5335 than another user-defined conversion sequence U2 if they contain the
5336 same user-defined conversion operator or constructor and if the sec-
5337 ond standard conversion sequence of U1 is better than the second
5338 standard conversion sequence of U2. */
5340 if (ICS_USER_FLAG (ics1))
5344 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5345 if (TREE_CODE (t1) == AMBIG_CONV)
5347 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5348 if (TREE_CODE (t2) == AMBIG_CONV)
5351 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5354 /* We can just fall through here, after setting up
5355 FROM_TYPE1 and FROM_TYPE2. */
5356 from_type1 = TREE_TYPE (t1);
5357 from_type2 = TREE_TYPE (t2);
5361 /* We're dealing with two standard conversion sequences.
5365 Standard conversion sequence S1 is a better conversion
5366 sequence than standard conversion sequence S2 if
5368 --S1 is a proper subsequence of S2 (comparing the conversion
5369 sequences in the canonical form defined by _over.ics.scs_,
5370 excluding any Lvalue Transformation; the identity
5371 conversion sequence is considered to be a subsequence of
5372 any non-identity conversion sequence */
5375 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5376 from_type1 = TREE_OPERAND (from_type1, 0);
5377 from_type1 = TREE_TYPE (from_type1);
5380 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5381 from_type2 = TREE_OPERAND (from_type2, 0);
5382 from_type2 = TREE_TYPE (from_type2);
5385 if (same_type_p (from_type1, from_type2))
5387 if (is_subseq (ics1, ics2))
5389 if (is_subseq (ics2, ics1))
5392 /* Otherwise, one sequence cannot be a subsequence of the other; they
5393 don't start with the same type. This can happen when comparing the
5394 second standard conversion sequence in two user-defined conversion
5401 --the rank of S1 is better than the rank of S2 (by the rules
5404 Standard conversion sequences are ordered by their ranks: an Exact
5405 Match is a better conversion than a Promotion, which is a better
5406 conversion than a Conversion.
5408 Two conversion sequences with the same rank are indistinguishable
5409 unless one of the following rules applies:
5411 --A conversion that is not a conversion of a pointer, or pointer
5412 to member, to bool is better than another conversion that is such
5415 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5416 so that we do not have to check it explicitly. */
5417 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5419 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5422 to_type1 = TREE_TYPE (ics1);
5423 to_type2 = TREE_TYPE (ics2);
5425 if (TYPE_PTR_P (from_type1)
5426 && TYPE_PTR_P (from_type2)
5427 && TYPE_PTR_P (to_type1)
5428 && TYPE_PTR_P (to_type2))
5430 deref_from_type1 = TREE_TYPE (from_type1);
5431 deref_from_type2 = TREE_TYPE (from_type2);
5432 deref_to_type1 = TREE_TYPE (to_type1);
5433 deref_to_type2 = TREE_TYPE (to_type2);
5435 /* The rules for pointers to members A::* are just like the rules
5436 for pointers A*, except opposite: if B is derived from A then
5437 A::* converts to B::*, not vice versa. For that reason, we
5438 switch the from_ and to_ variables here. */
5439 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5440 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5441 || (TYPE_PTRMEMFUNC_P (from_type1)
5442 && TYPE_PTRMEMFUNC_P (from_type2)
5443 && TYPE_PTRMEMFUNC_P (to_type1)
5444 && TYPE_PTRMEMFUNC_P (to_type2)))
5446 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5447 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5448 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5449 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
5452 if (deref_from_type1 != NULL_TREE
5453 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5454 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5456 /* This was one of the pointer or pointer-like conversions.
5460 --If class B is derived directly or indirectly from class A,
5461 conversion of B* to A* is better than conversion of B* to
5462 void*, and conversion of A* to void* is better than
5463 conversion of B* to void*. */
5464 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5465 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5467 if (is_properly_derived_from (deref_from_type1,
5470 else if (is_properly_derived_from (deref_from_type2,
5474 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5475 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5477 if (same_type_p (deref_from_type1, deref_from_type2))
5479 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5481 if (is_properly_derived_from (deref_from_type1,
5485 /* We know that DEREF_TO_TYPE1 is `void' here. */
5486 else if (is_properly_derived_from (deref_from_type1,
5491 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5492 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5496 --If class B is derived directly or indirectly from class A
5497 and class C is derived directly or indirectly from B,
5499 --conversion of C* to B* is better than conversion of C* to
5502 --conversion of B* to A* is better than conversion of C* to
5504 if (same_type_p (deref_from_type1, deref_from_type2))
5506 if (is_properly_derived_from (deref_to_type1,
5509 else if (is_properly_derived_from (deref_to_type2,
5513 else if (same_type_p (deref_to_type1, deref_to_type2))
5515 if (is_properly_derived_from (deref_from_type2,
5518 else if (is_properly_derived_from (deref_from_type1,
5524 else if (CLASS_TYPE_P (non_reference (from_type1))
5525 && same_type_p (from_type1, from_type2))
5527 tree from = non_reference (from_type1);
5531 --binding of an expression of type C to a reference of type
5532 B& is better than binding an expression of type C to a
5533 reference of type A&
5535 --conversion of C to B is better than conversion of C to A, */
5536 if (is_properly_derived_from (from, to_type1)
5537 && is_properly_derived_from (from, to_type2))
5539 if (is_properly_derived_from (to_type1, to_type2))
5541 else if (is_properly_derived_from (to_type2, to_type1))
5545 else if (CLASS_TYPE_P (non_reference (to_type1))
5546 && same_type_p (to_type1, to_type2))
5548 tree to = non_reference (to_type1);
5552 --binding of an expression of type B to a reference of type
5553 A& is better than binding an expression of type C to a
5554 reference of type A&,
5556 --onversion of B to A is better than conversion of C to A */
5557 if (is_properly_derived_from (from_type1, to)
5558 && is_properly_derived_from (from_type2, to))
5560 if (is_properly_derived_from (from_type2, from_type1))
5562 else if (is_properly_derived_from (from_type1, from_type2))
5569 --S1 and S2 differ only in their qualification conversion and yield
5570 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5571 qualification signature of type T1 is a proper subset of the cv-
5572 qualification signature of type T2 */
5573 if (TREE_CODE (ics1) == QUAL_CONV
5574 && TREE_CODE (ics2) == QUAL_CONV
5575 && same_type_p (from_type1, from_type2))
5576 return comp_cv_qual_signature (to_type1, to_type2);
5580 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5581 types to which the references refer are the same type except for
5582 top-level cv-qualifiers, and the type to which the reference
5583 initialized by S2 refers is more cv-qualified than the type to
5584 which the reference initialized by S1 refers */
5586 if (target_type1 && target_type2
5587 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5588 return comp_cv_qualification (target_type2, target_type1);
5590 /* Neither conversion sequence is better than the other. */
5594 /* The source type for this standard conversion sequence. */
5597 source_type (tree t)
5599 for (;; t = TREE_OPERAND (t, 0))
5601 if (TREE_CODE (t) == USER_CONV
5602 || TREE_CODE (t) == AMBIG_CONV
5603 || TREE_CODE (t) == IDENTITY_CONV)
5604 return TREE_TYPE (t);
5609 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5610 a pointer to LOSER and re-running joust to produce the warning if WINNER
5611 is actually used. */
5614 add_warning (struct z_candidate *winner, struct z_candidate *loser)
5616 winner->warnings = tree_cons (NULL_TREE,
5617 build_zc_wrapper (loser),
5621 /* Compare two candidates for overloading as described in
5622 [over.match.best]. Return values:
5624 1: cand1 is better than cand2
5625 -1: cand2 is better than cand1
5626 0: cand1 and cand2 are indistinguishable */
5629 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5632 int i, off1 = 0, off2 = 0, len;
5634 /* Candidates that involve bad conversions are always worse than those
5636 if (cand1->viable > cand2->viable)
5638 if (cand1->viable < cand2->viable)
5641 /* If we have two pseudo-candidates for conversions to the same type,
5642 or two candidates for the same function, arbitrarily pick one. */
5643 if (cand1->fn == cand2->fn
5644 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5647 /* a viable function F1
5648 is defined to be a better function than another viable function F2 if
5649 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5650 ICSi(F2), and then */
5652 /* for some argument j, ICSj(F1) is a better conversion sequence than
5655 /* For comparing static and non-static member functions, we ignore
5656 the implicit object parameter of the non-static function. The
5657 standard says to pretend that the static function has an object
5658 parm, but that won't work with operator overloading. */
5659 len = TREE_VEC_LENGTH (cand1->convs);
5660 if (len != TREE_VEC_LENGTH (cand2->convs))
5662 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5663 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5665 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5666 && DECL_STATIC_FUNCTION_P (cand2->fn))
5675 for (i = 0; i < len; ++i)
5677 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5678 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5679 int comp = compare_ics (t1, t2);
5684 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5685 && TREE_CODE (t1) == STD_CONV
5686 && TREE_CODE (t2) == STD_CONV
5687 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5688 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5689 && (TYPE_PRECISION (TREE_TYPE (t1))
5690 == TYPE_PRECISION (TREE_TYPE (t2)))
5691 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5692 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5695 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5697 struct z_candidate *w, *l;
5699 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5700 w = cand1, l = cand2;
5702 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5703 w = cand2, l = cand1;
5707 warning ("passing `%T' chooses `%T' over `%T'",
5708 type, type1, type2);
5709 warning (" in call to `%D'", w->fn);
5715 if (winner && comp != winner)
5724 /* warn about confusing overload resolution for user-defined conversions,
5725 either between a constructor and a conversion op, or between two
5727 if (winner && warn_conversion && cand1->second_conv
5728 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
5729 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
5731 struct z_candidate *w, *l;
5732 bool give_warning = false;
5735 w = cand1, l = cand2;
5737 w = cand2, l = cand1;
5739 /* We don't want to complain about `X::operator T1 ()'
5740 beating `X::operator T2 () const', when T2 is a no less
5741 cv-qualified version of T1. */
5742 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
5743 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
5745 tree t = TREE_TYPE (TREE_TYPE (l->fn));
5746 tree f = TREE_TYPE (TREE_TYPE (w->fn));
5748 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
5753 if (!comp_ptr_ttypes (t, f))
5754 give_warning = true;
5757 give_warning = true;
5763 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5764 if (! DECL_CONSTRUCTOR_P (w->fn))
5765 source = TREE_TYPE (source);
5766 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5767 warning (" for conversion from `%T' to `%T'",
5768 source, TREE_TYPE (w->second_conv));
5769 warning (" because conversion sequence for the argument is better");
5779 F1 is a non-template function and F2 is a template function
5782 if (! cand1->template && cand2->template)
5784 else if (cand1->template && ! cand2->template)
5788 F1 and F2 are template functions and the function template for F1 is
5789 more specialized than the template for F2 according to the partial
5792 if (cand1->template && cand2->template)
5794 winner = more_specialized
5795 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5797 /* Tell the deduction code how many real function arguments
5798 we saw, not counting the implicit 'this' argument. But,
5799 add_function_candidate() suppresses the "this" argument
5802 [temp.func.order]: The presence of unused ellipsis and default
5803 arguments has no effect on the partial ordering of function
5805 TREE_VEC_LENGTH (cand1->convs)
5806 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5807 - DECL_CONSTRUCTOR_P (cand1->fn)));
5813 the context is an initialization by user-defined conversion (see
5814 _dcl.init_ and _over.match.user_) and the standard conversion
5815 sequence from the return type of F1 to the destination type (i.e.,
5816 the type of the entity being initialized) is a better conversion
5817 sequence than the standard conversion sequence from the return type
5818 of F2 to the destination type. */
5820 if (cand1->second_conv)
5822 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5827 /* Check whether we can discard a builtin candidate, either because we
5828 have two identical ones or matching builtin and non-builtin candidates.
5830 (Pedantically in the latter case the builtin which matched the user
5831 function should not be added to the overload set, but we spot it here.
5834 ... the builtin candidates include ...
5835 - do not have the same parameter type list as any non-template
5836 non-member candidate. */
5838 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5839 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5841 for (i = 0; i < len; ++i)
5842 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5843 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5845 if (i == TREE_VEC_LENGTH (cand1->convs))
5847 if (cand1->fn == cand2->fn)
5848 /* Two built-in candidates; arbitrarily pick one. */
5850 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5851 /* cand1 is built-in; prefer cand2. */
5854 /* cand2 is built-in; prefer cand1. */
5859 /* If the two functions are the same (this can happen with declarations
5860 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5861 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5862 && equal_functions (cand1->fn, cand2->fn))
5867 /* Extension: If the worst conversion for one candidate is worse than the
5868 worst conversion for the other, take the first. */
5871 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5872 struct z_candidate *w = 0, *l = 0;
5874 for (i = 0; i < len; ++i)
5876 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5877 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5878 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5879 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5882 winner = 1, w = cand1, l = cand2;
5884 winner = -1, w = cand2, l = cand1;
5890 ISO C++ says that these are ambiguous, even \
5891 though the worst conversion for the first is better than \
5892 the worst conversion for the second:");
5893 print_z_candidate (_("candidate 1:"), w);
5894 print_z_candidate (_("candidate 2:"), l);
5902 my_friendly_assert (!winner, 20010121);
5906 /* Given a list of candidates for overloading, find the best one, if any.
5907 This algorithm has a worst case of O(2n) (winner is last), and a best
5908 case of O(n/2) (totally ambiguous); much better than a sorting
5911 static struct z_candidate *
5912 tourney (struct z_candidate *candidates)
5914 struct z_candidate *champ = candidates, *challenger;
5916 int champ_compared_to_predecessor = 0;
5918 /* Walk through the list once, comparing each current champ to the next
5919 candidate, knocking out a candidate or two with each comparison. */
5921 for (challenger = champ->next; challenger; )
5923 fate = joust (champ, challenger, 0);
5925 challenger = challenger->next;
5930 champ = challenger->next;
5933 champ_compared_to_predecessor = 0;
5938 champ_compared_to_predecessor = 1;
5941 challenger = champ->next;
5945 /* Make sure the champ is better than all the candidates it hasn't yet
5946 been compared to. */
5948 for (challenger = candidates;
5950 && !(champ_compared_to_predecessor && challenger->next == champ);
5951 challenger = challenger->next)
5953 fate = joust (champ, challenger, 0);
5961 /* Returns nonzero if things of type FROM can be converted to TO. */
5964 can_convert (tree to, tree from)
5966 return can_convert_arg (to, from, NULL_TREE);
5969 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
5972 can_convert_arg (tree to, tree from, tree arg)
5974 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5975 return (t && ! ICS_BAD_FLAG (t));
5978 /* Like can_convert_arg, but allows dubious conversions as well. */
5981 can_convert_arg_bad (tree to, tree from, tree arg)
5983 return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
5986 /* Convert EXPR to TYPE. Return the converted expression.
5988 Note that we allow bad conversions here because by the time we get to
5989 this point we are committed to doing the conversion. If we end up
5990 doing a bad conversion, convert_like will complain. */
5993 perform_implicit_conversion (tree type, tree expr)
5997 if (error_operand_p (expr))
5998 return error_mark_node;
5999 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6003 error ("could not convert `%E' to `%T'", expr, type);
6004 return error_mark_node;
6007 return convert_like (conv, expr);
6010 /* Convert EXPR to TYPE (as a direct-initialization) if that is
6011 permitted. If the conversion is valid, the converted expression is
6012 returned. Otherwise, NULL_TREE is returned, except in the case
6013 that TYPE is a class type; in that case, an error is issued. */
6016 perform_direct_initialization_if_possible (tree type, tree expr)
6020 if (type == error_mark_node || error_operand_p (expr))
6021 return error_mark_node;
6024 If the destination type is a (possibly cv-qualified) class type:
6026 -- If the initialization is direct-initialization ...,
6027 constructors are considered. ... If no constructor applies, or
6028 the overload resolution is ambiguous, the initialization is
6030 if (CLASS_TYPE_P (type))
6032 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6033 build_tree_list (NULL_TREE, expr),
6036 return build_cplus_new (type, expr);
6038 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6040 if (!conv || ICS_BAD_FLAG (conv))
6042 return convert_like_real (conv, expr, NULL_TREE, 0, 0,
6043 /*issue_conversion_warnings=*/false);
6046 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
6047 is being bound to a temporary. Create and return a new VAR_DECL
6048 with the indicated TYPE; this variable will store the value to
6049 which the reference is bound. */
6052 make_temporary_var_for_ref_to_temp (tree decl, tree type)
6056 /* Create the variable. */
6057 var = build_decl (VAR_DECL, NULL_TREE, type);
6058 DECL_ARTIFICIAL (var) = 1;
6059 TREE_USED (var) = 1;
6061 /* Register the variable. */
6062 if (TREE_STATIC (decl))
6064 /* Namespace-scope or local static; give it a mangled name. */
6067 TREE_STATIC (var) = 1;
6068 name = mangle_ref_init_variable (decl);
6069 DECL_NAME (var) = name;
6070 SET_DECL_ASSEMBLER_NAME (var, name);
6071 var = pushdecl_top_level (var);
6075 /* Create a new cleanup level if necessary. */
6076 maybe_push_cleanup_level (type);
6077 /* Don't push unnamed temps. Do set DECL_CONTEXT, though. */
6078 DECL_CONTEXT (var) = current_function_decl;
6084 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
6085 initializing a variable of that TYPE. If DECL is non-NULL, it is
6086 the VAR_DECL being initialized with the EXPR. (In that case, the
6087 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
6088 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
6089 return, if *CLEANUP is no longer NULL, it will be a CLEANUP_STMT
6090 that should be inserted after the returned expression is used to
6093 Return the converted expression. */
6096 initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
6100 if (type == error_mark_node || error_operand_p (expr))
6101 return error_mark_node;
6103 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
6104 if (!conv || ICS_BAD_FLAG (conv))
6106 if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6107 && !real_lvalue_p (expr))
6108 error ("invalid initialization of non-const reference of "
6109 "type '%T' from a temporary of type '%T'",
6110 type, TREE_TYPE (expr));
6112 error ("invalid initialization of reference of type "
6113 "'%T' from expression of type '%T'", type,
6115 return error_mark_node;
6118 /* If DECL is non-NULL, then this special rule applies:
6122 The temporary to which the reference is bound or the temporary
6123 that is the complete object to which the reference is bound
6124 persists for the lifetime of the reference.
6126 The temporaries created during the evaluation of the expression
6127 initializing the reference, except the temporary to which the
6128 reference is bound, are destroyed at the end of the
6129 full-expression in which they are created.
6131 In that case, we store the converted expression into a new
6132 VAR_DECL in a new scope.
6134 However, we want to be careful not to create temporaries when
6135 they are not required. For example, given:
6138 struct D : public B {};
6142 there is no need to copy the return value from "f"; we can just
6143 extend its lifetime. Similarly, given:
6146 struct T { operator S(); };
6150 we can extend the lifetime of the return value of the conversion
6152 my_friendly_assert (TREE_CODE (conv) == REF_BIND, 20030302);
6156 tree base_conv_type;
6158 /* Skip over the REF_BIND. */
6159 conv = TREE_OPERAND (conv, 0);
6160 /* If the next conversion is a BASE_CONV, skip that too -- but
6161 remember that the conversion was required. */
6162 if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv))
6164 void (*diagnostic_fn) (const char *, ...);
6165 if (CHECK_COPY_CONSTRUCTOR_P (conv))
6166 /* Generate a temporary copy purely to generate the required
6168 build_temp (build_dummy_object (TREE_TYPE (expr)),
6170 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6172 base_conv_type = TREE_TYPE (conv);
6173 conv = TREE_OPERAND (conv, 0);
6176 base_conv_type = NULL_TREE;
6177 /* Perform the remainder of the conversion. */
6178 expr = convert_like_real (conv, expr,
6179 /*fn=*/NULL_TREE, /*argnum=*/0,
6181 /*issue_conversion_warnings=*/true);
6182 if (!real_lvalue_p (expr))
6187 /* Create the temporary variable. */
6188 type = TREE_TYPE (expr);
6189 var = make_temporary_var_for_ref_to_temp (decl, type);
6190 layout_decl (var, 0);
6191 /* If the rvalue is the result of a function call it will be
6192 a TARGET_EXPR. If it is some other construct (such as a
6193 member access expression where the underlying object is
6194 itself the result of a function call), turn it into a
6195 TARGET_EXPR here. It is important that EXPR be a
6196 TARGET_EXPR below since otherwise the INIT_EXPR will
6197 attempt to make a bitwise copy of EXPR to intialize
6199 if (TREE_CODE (expr) != TARGET_EXPR)
6200 expr = get_target_expr (expr);
6201 /* Create the INIT_EXPR that will initialize the temporary
6203 init = build (INIT_EXPR, type, var, expr);
6204 if (at_function_scope_p ())
6206 add_decl_stmt (var);
6207 *cleanup = cxx_maybe_build_cleanup (var);
6209 /* We must be careful to destroy the temporary only
6210 after its initialization has taken place. If the
6211 initialization throws an exception, then the
6212 destructor should not be run. We cannot simply
6213 transform INIT into something like:
6215 (INIT, ({ CLEANUP_STMT; }))
6217 because emit_local_var always treats the
6218 initializer as a full-expression. Thus, the
6219 destructor would run too early; it would run at the
6220 end of initializing the reference variable, rather
6221 than at the end of the block enclosing the
6224 The solution is to pass back a CLEANUP_STMT which
6225 the caller is responsible for attaching to the
6227 *cleanup = build_stmt (CLEANUP_STMT, var, *cleanup);
6231 rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof);
6232 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6233 static_aggregates = tree_cons (NULL_TREE, var,
6236 /* Use its address to initialize the reference variable. */
6237 expr = build_address (var);
6238 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6241 /* Take the address of EXPR. */
6242 expr = build_unary_op (ADDR_EXPR, expr, 0);
6243 /* If a BASE_CONV was required, perform it now. */
6245 expr = (perform_implicit_conversion
6246 (build_pointer_type (base_conv_type), expr));
6247 return build_nop (type, expr);
6250 /* Perform the conversion. */
6251 return convert_like (conv, expr);
6254 #include "gt-cp-call.h"