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 (tree);
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),
831 /* Build a standard conversion sequence indicating the
832 binding from the reference type returned by the
833 function to the desired REFERENCE_TYPE. */
835 = (direct_reference_binding
837 build1 (IDENTITY_CONV,
838 TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))),
841 conversions = TREE_CHAIN (conversions);
844 candidates = splice_viable (candidates, pedantic, &any_viable_p);
845 /* If none of the conversion functions worked out, let our caller
850 cand = tourney (candidates);
854 /* Now that we know that this is the function we're going to use fix
855 the dummy first argument. */
856 cand->args = tree_cons (NULL_TREE,
858 TREE_CHAIN (cand->args));
860 /* Build a user-defined conversion sequence representing the
862 conv = build_conv (USER_CONV,
863 TREE_TYPE (TREE_TYPE (cand->fn)),
864 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
865 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
867 /* Merge it with the standard conversion sequence from the
868 conversion function's return type to the desired type. */
869 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
871 if (cand->viable == -1)
872 ICS_BAD_FLAG (conv) = 1;
874 return cand->second_conv;
877 /* A reference of the indicated TYPE is being bound directly to the
878 expression represented by the implicit conversion sequence CONV.
879 Return a conversion sequence for this binding. */
882 direct_reference_binding (tree type, tree conv)
886 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
887 my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
890 t = TREE_TYPE (type);
894 When a parameter of reference type binds directly
895 (_dcl.init.ref_) to an argument expression, the implicit
896 conversion sequence is the identity conversion, unless the
897 argument expression has a type that is a derived class of the
898 parameter type, in which case the implicit conversion sequence is
899 a derived-to-base Conversion.
901 If the parameter binds directly to the result of applying a
902 conversion function to the argument expression, the implicit
903 conversion sequence is a user-defined conversion sequence
904 (_over.ics.user_), with the second standard conversion sequence
905 either an identity conversion or, if the conversion function
906 returns an entity of a type that is a derived class of the
907 parameter type, a derived-to-base conversion. */
908 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
910 /* Represent the derived-to-base conversion. */
911 conv = build_conv (BASE_CONV, t, conv);
912 /* We will actually be binding to the base-class subobject in
913 the derived class, so we mark this conversion appropriately.
914 That way, convert_like knows not to generate a temporary. */
915 NEED_TEMPORARY_P (conv) = 0;
917 return build_conv (REF_BIND, type, conv);
920 /* Returns the conversion path from type FROM to reference type TO for
921 purposes of reference binding. For lvalue binding, either pass a
922 reference type to FROM or an lvalue expression to EXPR. If the
923 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
924 the conversion returned. */
927 reference_binding (tree rto, tree rfrom, tree expr, int flags)
929 tree conv = NULL_TREE;
930 tree to = TREE_TYPE (rto);
934 cp_lvalue_kind lvalue_p = clk_none;
936 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
938 expr = instantiate_type (to, expr, tf_none);
939 if (expr == error_mark_node)
941 from = TREE_TYPE (expr);
944 if (TREE_CODE (from) == REFERENCE_TYPE)
946 /* Anything with reference type is an lvalue. */
947 lvalue_p = clk_ordinary;
948 from = TREE_TYPE (from);
951 lvalue_p = real_lvalue_p (expr);
953 /* Figure out whether or not the types are reference-related and
954 reference compatible. We have do do this after stripping
955 references from FROM. */
956 related_p = reference_related_p (to, from);
957 compatible_p = reference_compatible_p (to, from);
959 if (lvalue_p && compatible_p)
963 If the initializer expression
965 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
966 is reference-compatible with "cv2 T2,"
968 the reference is bound directly to the initializer expression
970 conv = build1 (IDENTITY_CONV, from, expr);
971 conv = direct_reference_binding (rto, conv);
972 if ((lvalue_p & clk_bitfield) != 0
973 || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
974 /* For the purposes of overload resolution, we ignore the fact
975 this expression is a bitfield or packed field. (In particular,
976 [over.ics.ref] says specifically that a function with a
977 non-const reference parameter is viable even if the
978 argument is a bitfield.)
980 However, when we actually call the function we must create
981 a temporary to which to bind the reference. If the
982 reference is volatile, or isn't const, then we cannot make
983 a temporary, so we just issue an error when the conversion
985 NEED_TEMPORARY_P (conv) = 1;
989 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
993 If the initializer expression
995 -- has a class type (i.e., T2 is a class type) can be
996 implicitly converted to an lvalue of type "cv3 T3," where
997 "cv1 T1" is reference-compatible with "cv3 T3". (this
998 conversion is selected by enumerating the applicable
999 conversion functions (_over.match.ref_) and choosing the
1000 best one through overload resolution. (_over.match_).
1002 the reference is bound to the lvalue result of the conversion
1003 in the second case. */
1004 conv = convert_class_to_reference (to, from, expr);
1009 /* From this point on, we conceptually need temporaries, even if we
1010 elide them. Only the cases above are "direct bindings". */
1011 if (flags & LOOKUP_NO_TEMP_BIND)
1016 When a parameter of reference type is not bound directly to an
1017 argument expression, the conversion sequence is the one required
1018 to convert the argument expression to the underlying type of the
1019 reference according to _over.best.ics_. Conceptually, this
1020 conversion sequence corresponds to copy-initializing a temporary
1021 of the underlying type with the argument expression. Any
1022 difference in top-level cv-qualification is subsumed by the
1023 initialization itself and does not constitute a conversion. */
1027 Otherwise, the reference shall be to a non-volatile const type. */
1028 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1033 If the initializer expression is an rvalue, with T2 a class type,
1034 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1035 is bound in one of the following ways:
1037 -- The reference is bound to the object represented by the rvalue
1038 or to a sub-object within that object.
1042 We use the first alternative. The implicit conversion sequence
1043 is supposed to be same as we would obtain by generating a
1044 temporary. Fortunately, if the types are reference compatible,
1045 then this is either an identity conversion or the derived-to-base
1046 conversion, just as for direct binding. */
1047 if (CLASS_TYPE_P (from) && compatible_p)
1049 conv = build1 (IDENTITY_CONV, from, expr);
1050 conv = direct_reference_binding (rto, conv);
1051 CHECK_COPY_CONSTRUCTOR_P (TREE_OPERAND (conv, 0)) = 1;
1057 Otherwise, a temporary of type "cv1 T1" is created and
1058 initialized from the initializer expression using the rules for a
1059 non-reference copy initialization. If T1 is reference-related to
1060 T2, cv1 must be the same cv-qualification as, or greater
1061 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1062 if (related_p && !at_least_as_qualified_p (to, from))
1065 conv = implicit_conversion (to, from, expr, flags);
1069 conv = build_conv (REF_BIND, rto, conv);
1070 /* This reference binding, unlike those above, requires the
1071 creation of a temporary. */
1072 NEED_TEMPORARY_P (conv) = 1;
1077 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1078 to type TO. The optional expression EXPR may affect the conversion.
1079 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1083 implicit_conversion (tree to, tree from, tree expr, int flags)
1087 if (from == error_mark_node || to == error_mark_node
1088 || expr == error_mark_node)
1091 if (TREE_CODE (to) == REFERENCE_TYPE)
1092 conv = reference_binding (to, from, expr, flags);
1094 conv = standard_conversion (to, from, expr);
1099 if (expr != NULL_TREE
1100 && (IS_AGGR_TYPE (from)
1101 || IS_AGGR_TYPE (to))
1102 && (flags & LOOKUP_NO_CONVERSION) == 0)
1104 struct z_candidate *cand;
1106 cand = build_user_type_conversion_1
1107 (to, expr, LOOKUP_ONLYCONVERTING);
1109 conv = cand->second_conv;
1111 /* We used to try to bind a reference to a temporary here, but that
1112 is now handled by the recursive call to this function at the end
1113 of reference_binding. */
1120 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1123 static struct z_candidate *
1124 add_candidate (struct z_candidate **candidates,
1125 tree fn, tree args, tree convs, tree access_path,
1126 tree conversion_path, int viable)
1128 struct z_candidate *cand = ggc_alloc_cleared (sizeof (struct z_candidate));
1132 cand->convs = convs;
1133 cand->access_path = access_path;
1134 cand->conversion_path = conversion_path;
1135 cand->viable = viable;
1136 cand->next = *candidates;
1142 /* Create an overload candidate for the function or method FN called with
1143 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1144 to implicit_conversion.
1146 CTYPE, if non-NULL, is the type we want to pretend this function
1147 comes from for purposes of overload resolution. */
1149 static struct z_candidate *
1150 add_function_candidate (struct z_candidate **candidates,
1151 tree fn, tree ctype, tree arglist,
1152 tree access_path, tree conversion_path,
1155 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1158 tree parmnode, argnode;
1162 /* Built-in functions that haven't been declared don't really
1164 if (DECL_ANTICIPATED (fn))
1167 /* The `this', `in_chrg' and VTT arguments to constructors are not
1168 considered in overload resolution. */
1169 if (DECL_CONSTRUCTOR_P (fn))
1171 parmlist = skip_artificial_parms_for (fn, parmlist);
1172 orig_arglist = arglist;
1173 arglist = skip_artificial_parms_for (fn, arglist);
1176 orig_arglist = arglist;
1178 len = list_length (arglist);
1179 convs = make_tree_vec (len);
1181 /* 13.3.2 - Viable functions [over.match.viable]
1182 First, to be a viable function, a candidate function shall have enough
1183 parameters to agree in number with the arguments in the list.
1185 We need to check this first; otherwise, checking the ICSes might cause
1186 us to produce an ill-formed template instantiation. */
1188 parmnode = parmlist;
1189 for (i = 0; i < len; ++i)
1191 if (parmnode == NULL_TREE || parmnode == void_list_node)
1193 parmnode = TREE_CHAIN (parmnode);
1196 if (i < len && parmnode)
1199 /* Make sure there are default args for the rest of the parms. */
1200 else if (!sufficient_parms_p (parmnode))
1206 /* Second, for F to be a viable function, there shall exist for each
1207 argument an implicit conversion sequence that converts that argument
1208 to the corresponding parameter of F. */
1210 parmnode = parmlist;
1213 for (i = 0; i < len; ++i)
1215 tree arg = TREE_VALUE (argnode);
1216 tree argtype = lvalue_type (arg);
1220 if (parmnode == void_list_node)
1223 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1224 && ! DECL_CONSTRUCTOR_P (fn));
1228 tree parmtype = TREE_VALUE (parmnode);
1230 /* The type of the implicit object parameter ('this') for
1231 overload resolution is not always the same as for the
1232 function itself; conversion functions are considered to
1233 be members of the class being converted, and functions
1234 introduced by a using-declaration are considered to be
1235 members of the class that uses them.
1237 Since build_over_call ignores the ICS for the `this'
1238 parameter, we can just change the parm type. */
1239 if (ctype && is_this)
1242 = build_qualified_type (ctype,
1243 TYPE_QUALS (TREE_TYPE (parmtype)));
1244 parmtype = build_pointer_type (parmtype);
1247 t = implicit_conversion (parmtype, argtype, arg, flags);
1251 t = build1 (IDENTITY_CONV, argtype, arg);
1252 ICS_ELLIPSIS_FLAG (t) = 1;
1256 ICS_THIS_FLAG (t) = 1;
1258 TREE_VEC_ELT (convs, i) = t;
1265 if (ICS_BAD_FLAG (t))
1269 parmnode = TREE_CHAIN (parmnode);
1270 argnode = TREE_CHAIN (argnode);
1274 return add_candidate (candidates, fn, orig_arglist, convs, access_path,
1275 conversion_path, viable);
1278 /* Create an overload candidate for the conversion function FN which will
1279 be invoked for expression OBJ, producing a pointer-to-function which
1280 will in turn be called with the argument list ARGLIST, and add it to
1281 CANDIDATES. FLAGS is passed on to implicit_conversion.
1283 Actually, we don't really care about FN; we care about the type it
1284 converts to. There may be multiple conversion functions that will
1285 convert to that type, and we rely on build_user_type_conversion_1 to
1286 choose the best one; so when we create our candidate, we record the type
1287 instead of the function. */
1289 static struct z_candidate *
1290 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1291 tree arglist, tree access_path, tree conversion_path)
1293 tree totype = TREE_TYPE (TREE_TYPE (fn));
1294 int i, len, viable, flags;
1295 tree parmlist, convs, parmnode, argnode;
1297 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1298 parmlist = TREE_TYPE (parmlist);
1299 parmlist = TYPE_ARG_TYPES (parmlist);
1301 len = list_length (arglist) + 1;
1302 convs = make_tree_vec (len);
1303 parmnode = parmlist;
1306 flags = LOOKUP_NORMAL;
1308 /* Don't bother looking up the same type twice. */
1309 if (*candidates && (*candidates)->fn == totype)
1312 for (i = 0; i < len; ++i)
1314 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1315 tree argtype = lvalue_type (arg);
1319 t = implicit_conversion (totype, argtype, arg, flags);
1320 else if (parmnode == void_list_node)
1323 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1326 t = build1 (IDENTITY_CONV, argtype, arg);
1327 ICS_ELLIPSIS_FLAG (t) = 1;
1330 TREE_VEC_ELT (convs, i) = t;
1334 if (ICS_BAD_FLAG (t))
1341 parmnode = TREE_CHAIN (parmnode);
1342 argnode = TREE_CHAIN (argnode);
1348 if (!sufficient_parms_p (parmnode))
1351 return add_candidate (candidates, totype, arglist, convs, access_path,
1352 conversion_path, viable);
1356 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1357 tree type1, tree type2, tree *args, tree *argtypes,
1367 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1369 for (i = 0; i < 2; ++i)
1374 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1378 /* We need something for printing the candidate. */
1379 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1381 else if (ICS_BAD_FLAG (t))
1383 TREE_VEC_ELT (convs, i) = t;
1386 /* For COND_EXPR we rearranged the arguments; undo that now. */
1389 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1390 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1391 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1393 TREE_VEC_ELT (convs, 0) = t;
1398 add_candidate (candidates, fnname, /*args=*/NULL_TREE, convs,
1399 /*access_path=*/NULL_TREE,
1400 /*conversion_path=*/NULL_TREE,
1405 is_complete (tree t)
1407 return COMPLETE_TYPE_P (complete_type (t));
1410 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1413 promoted_arithmetic_type_p (tree type)
1417 In this section, the term promoted integral type is used to refer
1418 to those integral types which are preserved by integral promotion
1419 (including e.g. int and long but excluding e.g. char).
1420 Similarly, the term promoted arithmetic type refers to promoted
1421 integral types plus floating types. */
1422 return ((INTEGRAL_TYPE_P (type)
1423 && same_type_p (type_promotes_to (type), type))
1424 || TREE_CODE (type) == REAL_TYPE);
1427 /* Create any builtin operator overload candidates for the operator in
1428 question given the converted operand types TYPE1 and TYPE2. The other
1429 args are passed through from add_builtin_candidates to
1430 build_builtin_candidate.
1432 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1433 If CODE is requires candidates operands of the same type of the kind
1434 of which TYPE1 and TYPE2 are, we add both candidates
1435 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1438 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1439 enum tree_code code2, tree fnname, tree type1,
1440 tree type2, tree *args, tree *argtypes, int flags)
1444 case POSTINCREMENT_EXPR:
1445 case POSTDECREMENT_EXPR:
1446 args[1] = integer_zero_node;
1447 type2 = integer_type_node;
1456 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1457 and VQ is either volatile or empty, there exist candidate operator
1458 functions of the form
1459 VQ T& operator++(VQ T&);
1460 T operator++(VQ T&, int);
1461 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1462 type other than bool, and VQ is either volatile or empty, there exist
1463 candidate operator functions of the form
1464 VQ T& operator--(VQ T&);
1465 T operator--(VQ T&, int);
1466 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1467 complete object type, and VQ is either volatile or empty, there exist
1468 candidate operator functions of the form
1469 T*VQ& operator++(T*VQ&);
1470 T*VQ& operator--(T*VQ&);
1471 T* operator++(T*VQ&, int);
1472 T* operator--(T*VQ&, int); */
1474 case POSTDECREMENT_EXPR:
1475 case PREDECREMENT_EXPR:
1476 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1478 case POSTINCREMENT_EXPR:
1479 case PREINCREMENT_EXPR:
1480 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1482 type1 = build_reference_type (type1);
1487 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1488 exist candidate operator functions of the form
1492 8 For every function type T, there exist candidate operator functions of
1494 T& operator*(T*); */
1497 if (TREE_CODE (type1) == POINTER_TYPE
1498 && (TYPE_PTROB_P (type1)
1499 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1503 /* 9 For every type T, there exist candidate operator functions of the form
1506 10For every promoted arithmetic type T, there exist candidate operator
1507 functions of the form
1511 case CONVERT_EXPR: /* unary + */
1512 if (TREE_CODE (type1) == POINTER_TYPE)
1515 if (ARITHMETIC_TYPE_P (type1))
1519 /* 11For every promoted integral type T, there exist candidate operator
1520 functions of the form
1524 if (INTEGRAL_TYPE_P (type1))
1528 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1529 is the same type as C2 or is a derived class of C2, T is a complete
1530 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1531 there exist candidate operator functions of the form
1532 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1533 where CV12 is the union of CV1 and CV2. */
1536 if (TREE_CODE (type1) == POINTER_TYPE
1537 && TYPE_PTR_TO_MEMBER_P (type2))
1539 tree c1 = TREE_TYPE (type1);
1540 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
1542 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1543 && (TYPE_PTRMEMFUNC_P (type2)
1544 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1549 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1550 didate operator functions of the form
1555 bool operator<(L, R);
1556 bool operator>(L, R);
1557 bool operator<=(L, R);
1558 bool operator>=(L, R);
1559 bool operator==(L, R);
1560 bool operator!=(L, R);
1561 where LR is the result of the usual arithmetic conversions between
1564 14For every pair of types T and I, where T is a cv-qualified or cv-
1565 unqualified complete object type and I is a promoted integral type,
1566 there exist candidate operator functions of the form
1567 T* operator+(T*, I);
1568 T& operator[](T*, I);
1569 T* operator-(T*, I);
1570 T* operator+(I, T*);
1571 T& operator[](I, T*);
1573 15For every T, where T is a pointer to complete object type, there exist
1574 candidate operator functions of the form112)
1575 ptrdiff_t operator-(T, T);
1577 16For every pointer or enumeration type T, there exist candidate operator
1578 functions of the form
1579 bool operator<(T, T);
1580 bool operator>(T, T);
1581 bool operator<=(T, T);
1582 bool operator>=(T, T);
1583 bool operator==(T, T);
1584 bool operator!=(T, T);
1586 17For every pointer to member type T, there exist candidate operator
1587 functions of the form
1588 bool operator==(T, T);
1589 bool operator!=(T, T); */
1592 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1594 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1596 type2 = ptrdiff_type_node;
1600 case TRUNC_DIV_EXPR:
1601 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1607 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1608 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1610 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
1615 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
1627 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1629 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1631 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1633 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1638 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1646 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1649 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1651 type1 = ptrdiff_type_node;
1654 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1656 type2 = ptrdiff_type_node;
1661 /* 18For every pair of promoted integral types L and R, there exist candi-
1662 date operator functions of the form
1669 where LR is the result of the usual arithmetic conversions between
1672 case TRUNC_MOD_EXPR:
1678 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1682 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1683 type, VQ is either volatile or empty, and R is a promoted arithmetic
1684 type, there exist candidate operator functions of the form
1685 VQ L& operator=(VQ L&, R);
1686 VQ L& operator*=(VQ L&, R);
1687 VQ L& operator/=(VQ L&, R);
1688 VQ L& operator+=(VQ L&, R);
1689 VQ L& operator-=(VQ L&, R);
1691 20For every pair T, VQ), where T is any type and VQ is either volatile
1692 or empty, there exist candidate operator functions of the form
1693 T*VQ& operator=(T*VQ&, T*);
1695 21For every pair T, VQ), where T is a pointer to member type and VQ is
1696 either volatile or empty, there exist candidate operator functions of
1698 VQ T& operator=(VQ T&, T);
1700 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1701 unqualified complete object type, VQ is either volatile or empty, and
1702 I is a promoted integral type, there exist candidate operator func-
1704 T*VQ& operator+=(T*VQ&, I);
1705 T*VQ& operator-=(T*VQ&, I);
1707 23For every triple L, VQ, R), where L is an integral or enumeration
1708 type, VQ is either volatile or empty, and R is a promoted integral
1709 type, there exist candidate operator functions of the form
1711 VQ L& operator%=(VQ L&, R);
1712 VQ L& operator<<=(VQ L&, R);
1713 VQ L& operator>>=(VQ L&, R);
1714 VQ L& operator&=(VQ L&, R);
1715 VQ L& operator^=(VQ L&, R);
1716 VQ L& operator|=(VQ L&, R); */
1723 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1725 type2 = ptrdiff_type_node;
1729 case TRUNC_DIV_EXPR:
1730 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1734 case TRUNC_MOD_EXPR:
1740 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1745 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1747 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1748 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1749 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1750 || ((TYPE_PTRMEMFUNC_P (type1)
1751 || TREE_CODE (type1) == POINTER_TYPE)
1752 && null_ptr_cst_p (args[1])))
1762 type1 = build_reference_type (type1);
1768 For every pair of promoted arithmetic types L and R, there
1769 exist candidate operator functions of the form
1771 LR operator?(bool, L, R);
1773 where LR is the result of the usual arithmetic conversions
1774 between types L and R.
1776 For every type T, where T is a pointer or pointer-to-member
1777 type, there exist candidate operator functions of the form T
1778 operator?(bool, T, T); */
1780 if (promoted_arithmetic_type_p (type1)
1781 && promoted_arithmetic_type_p (type2))
1785 /* Otherwise, the types should be pointers. */
1786 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1787 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1790 /* We don't check that the two types are the same; the logic
1791 below will actually create two candidates; one in which both
1792 parameter types are TYPE1, and one in which both parameter
1800 /* If we're dealing with two pointer types or two enumeral types,
1801 we need candidates for both of them. */
1802 if (type2 && !same_type_p (type1, type2)
1803 && TREE_CODE (type1) == TREE_CODE (type2)
1804 && (TREE_CODE (type1) == REFERENCE_TYPE
1805 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1806 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1807 || TYPE_PTRMEMFUNC_P (type1)
1808 || IS_AGGR_TYPE (type1)
1809 || TREE_CODE (type1) == ENUMERAL_TYPE))
1811 build_builtin_candidate
1812 (candidates, fnname, type1, type1, args, argtypes, flags);
1813 build_builtin_candidate
1814 (candidates, fnname, type2, type2, args, argtypes, flags);
1818 build_builtin_candidate
1819 (candidates, fnname, type1, type2, args, argtypes, flags);
1823 type_decays_to (tree type)
1825 if (TREE_CODE (type) == ARRAY_TYPE)
1826 return build_pointer_type (TREE_TYPE (type));
1827 if (TREE_CODE (type) == FUNCTION_TYPE)
1828 return build_pointer_type (type);
1832 /* There are three conditions of builtin candidates:
1834 1) bool-taking candidates. These are the same regardless of the input.
1835 2) pointer-pair taking candidates. These are generated for each type
1836 one of the input types converts to.
1837 3) arithmetic candidates. According to the standard, we should generate
1838 all of these, but I'm trying not to...
1840 Here we generate a superset of the possible candidates for this particular
1841 case. That is a subset of the full set the standard defines, plus some
1842 other cases which the standard disallows. add_builtin_candidate will
1843 filter out the invalid set. */
1846 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
1847 enum tree_code code2, tree fnname, tree *args,
1852 tree type, argtypes[3];
1853 /* TYPES[i] is the set of possible builtin-operator parameter types
1854 we will consider for the Ith argument. These are represented as
1855 a TREE_LIST; the TREE_VALUE of each node is the potential
1859 for (i = 0; i < 3; ++i)
1862 argtypes[i] = lvalue_type (args[i]);
1864 argtypes[i] = NULL_TREE;
1869 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1870 and VQ is either volatile or empty, there exist candidate operator
1871 functions of the form
1872 VQ T& operator++(VQ T&); */
1874 case POSTINCREMENT_EXPR:
1875 case PREINCREMENT_EXPR:
1876 case POSTDECREMENT_EXPR:
1877 case PREDECREMENT_EXPR:
1882 /* 24There also exist candidate operator functions of the form
1883 bool operator!(bool);
1884 bool operator&&(bool, bool);
1885 bool operator||(bool, bool); */
1887 case TRUTH_NOT_EXPR:
1888 build_builtin_candidate
1889 (candidates, fnname, boolean_type_node,
1890 NULL_TREE, args, argtypes, flags);
1893 case TRUTH_ORIF_EXPR:
1894 case TRUTH_ANDIF_EXPR:
1895 build_builtin_candidate
1896 (candidates, fnname, boolean_type_node,
1897 boolean_type_node, args, argtypes, flags);
1919 types[0] = types[1] = NULL_TREE;
1921 for (i = 0; i < 2; ++i)
1925 else if (IS_AGGR_TYPE (argtypes[i]))
1929 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1932 convs = lookup_conversions (argtypes[i]);
1934 if (code == COND_EXPR)
1936 if (real_lvalue_p (args[i]))
1937 types[i] = tree_cons
1938 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1940 types[i] = tree_cons
1941 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1947 for (; convs; convs = TREE_CHAIN (convs))
1949 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
1952 && (TREE_CODE (type) != REFERENCE_TYPE
1953 || CP_TYPE_CONST_P (TREE_TYPE (type))))
1956 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1957 types[i] = tree_cons (NULL_TREE, type, types[i]);
1959 type = non_reference (type);
1960 if (i != 0 || ! ref1)
1962 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1963 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
1964 types[i] = tree_cons (NULL_TREE, type, types[i]);
1965 if (INTEGRAL_TYPE_P (type))
1966 type = type_promotes_to (type);
1969 if (! value_member (type, types[i]))
1970 types[i] = tree_cons (NULL_TREE, type, types[i]);
1975 if (code == COND_EXPR && real_lvalue_p (args[i]))
1976 types[i] = tree_cons
1977 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1978 type = non_reference (argtypes[i]);
1979 if (i != 0 || ! ref1)
1981 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1982 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
1983 types[i] = tree_cons (NULL_TREE, type, types[i]);
1984 if (INTEGRAL_TYPE_P (type))
1985 type = type_promotes_to (type);
1987 types[i] = tree_cons (NULL_TREE, type, types[i]);
1991 /* Run through the possible parameter types of both arguments,
1992 creating candidates with those parameter types. */
1993 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
1996 for (type = types[1]; type; type = TREE_CHAIN (type))
1997 add_builtin_candidate
1998 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
1999 TREE_VALUE (type), args, argtypes, flags);
2001 add_builtin_candidate
2002 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2003 NULL_TREE, args, argtypes, flags);
2010 /* If TMPL can be successfully instantiated as indicated by
2011 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2013 TMPL is the template. EXPLICIT_TARGS are any explicit template
2014 arguments. ARGLIST is the arguments provided at the call-site.
2015 The RETURN_TYPE is the desired type for conversion operators. If
2016 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2017 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2018 add_conv_candidate. */
2020 static struct z_candidate*
2021 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2022 tree ctype, tree explicit_targs, tree arglist,
2023 tree return_type, tree access_path,
2024 tree conversion_path, int flags, tree obj,
2025 unification_kind_t strict)
2027 int ntparms = DECL_NTPARMS (tmpl);
2028 tree targs = make_tree_vec (ntparms);
2029 tree args_without_in_chrg = arglist;
2030 struct z_candidate *cand;
2034 /* We don't do deduction on the in-charge parameter, the VTT
2035 parameter or 'this'. */
2036 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2037 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2039 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2040 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2041 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2042 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2044 i = fn_type_unification (tmpl, explicit_targs, targs,
2045 args_without_in_chrg,
2046 return_type, strict, -1);
2051 fn = instantiate_template (tmpl, targs, tf_none);
2052 if (fn == error_mark_node)
2057 A member function template is never instantiated to perform the
2058 copy of a class object to an object of its class type.
2060 It's a little unclear what this means; the standard explicitly
2061 does allow a template to be used to copy a class. For example,
2066 template <class T> A(const T&);
2069 void g () { A a (f ()); }
2071 the member template will be used to make the copy. The section
2072 quoted above appears in the paragraph that forbids constructors
2073 whose only parameter is (a possibly cv-qualified variant of) the
2074 class type, and a logical interpretation is that the intent was
2075 to forbid the instantiation of member templates which would then
2077 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2079 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2080 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2085 if (obj != NULL_TREE)
2086 /* Aha, this is a conversion function. */
2087 cand = add_conv_candidate (candidates, fn, obj, access_path,
2088 conversion_path, arglist);
2090 cand = add_function_candidate (candidates, fn, ctype,
2091 arglist, access_path,
2092 conversion_path, flags);
2093 if (DECL_TI_TEMPLATE (fn) != tmpl)
2094 /* This situation can occur if a member template of a template
2095 class is specialized. Then, instantiate_template might return
2096 an instantiation of the specialization, in which case the
2097 DECL_TI_TEMPLATE field will point at the original
2098 specialization. For example:
2100 template <class T> struct S { template <class U> void f(U);
2101 template <> void f(int) {}; };
2105 Here, TMPL will be template <class U> S<double>::f(U).
2106 And, instantiate template will give us the specialization
2107 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2108 for this will point at template <class T> template <> S<T>::f(int),
2109 so that we can find the definition. For the purposes of
2110 overload resolution, however, we want the original TMPL. */
2111 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2113 cand->template = DECL_TEMPLATE_INFO (fn);
2119 static struct z_candidate *
2120 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2121 tree explicit_targs, tree arglist, tree return_type,
2122 tree access_path, tree conversion_path, int flags,
2123 unification_kind_t strict)
2126 add_template_candidate_real (candidates, tmpl, ctype,
2127 explicit_targs, arglist, return_type,
2128 access_path, conversion_path,
2129 flags, NULL_TREE, strict);
2133 static struct z_candidate *
2134 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2135 tree obj, tree arglist, tree return_type,
2136 tree access_path, tree conversion_path)
2139 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2140 arglist, return_type, access_path,
2141 conversion_path, 0, obj, DEDUCE_CONV);
2144 /* The CANDS are the set of candidates that were considered for
2145 overload resolution. Return the set of viable candidates. If none
2146 of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P
2147 is true if a candidate should be considered viable only if it is
2150 static struct z_candidate*
2151 splice_viable (struct z_candidate *cands,
2155 struct z_candidate *viable;
2156 struct z_candidate **last_viable;
2157 struct z_candidate **cand;
2160 last_viable = &viable;
2161 *any_viable_p = false;
2166 struct z_candidate *c = *cand;
2167 if (strict_p ? c->viable == 1 : c->viable)
2172 last_viable = &c->next;
2173 *any_viable_p = true;
2179 return viable ? viable : cands;
2183 any_strictly_viable (struct z_candidate *cands)
2185 for (; cands; cands = cands->next)
2186 if (cands->viable == 1)
2192 build_this (tree obj)
2194 /* Fix this to work on non-lvalues. */
2195 return build_unary_op (ADDR_EXPR, obj, 0);
2198 /* Returns true iff functions are equivalent. Equivalent functions are
2199 not '==' only if one is a function-local extern function or if
2200 both are extern "C". */
2203 equal_functions (tree fn1, tree fn2)
2205 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2206 || DECL_EXTERN_C_FUNCTION_P (fn1))
2207 return decls_match (fn1, fn2);
2211 /* Print information about one overload candidate CANDIDATE. MSGSTR
2212 is the text to print before the candidate itself.
2214 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2215 to have been run through gettext by the caller. This wart makes
2216 life simpler in print_z_candidates and for the translators. */
2219 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2221 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2223 if (TREE_VEC_LENGTH (candidate->convs) == 3)
2224 inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2225 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2226 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)),
2227 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 2)));
2228 else if (TREE_VEC_LENGTH (candidate->convs) == 2)
2229 inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2230 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2231 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)));
2233 inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2234 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)));
2236 else if (TYPE_P (candidate->fn))
2237 inform ("%s %T <conversion>", msgstr, candidate->fn);
2238 else if (candidate->viable == -1)
2239 inform ("%J%s %+#D <near match>", candidate->fn, msgstr, candidate->fn);
2241 inform ("%J%s %+#D", candidate->fn, msgstr, candidate->fn);
2245 print_z_candidates (struct z_candidate *candidates)
2248 struct z_candidate *cand1;
2249 struct z_candidate **cand2;
2251 /* There may be duplicates in the set of candidates. We put off
2252 checking this condition as long as possible, since we have no way
2253 to eliminate duplicates from a set of functions in less than n^2
2254 time. Now we are about to emit an error message, so it is more
2255 permissible to go slowly. */
2256 for (cand1 = candidates; cand1; cand1 = cand1->next)
2258 tree fn = cand1->fn;
2259 /* Skip builtin candidates and conversion functions. */
2260 if (TREE_CODE (fn) != FUNCTION_DECL)
2262 cand2 = &cand1->next;
2265 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2266 && equal_functions (fn, (*cand2)->fn))
2267 *cand2 = (*cand2)->next;
2269 cand2 = &(*cand2)->next;
2276 str = _("candidates are:");
2277 print_z_candidate (str, candidates);
2278 if (candidates->next)
2280 /* Indent successive candidates by the width of the translation
2281 of the above string. */
2282 size_t len = gcc_gettext_width (str) + 1;
2283 char *spaces = alloca (len);
2284 memset (spaces, ' ', len-1);
2285 spaces[len - 1] = '\0';
2287 candidates = candidates->next;
2290 print_z_candidate (spaces, candidates);
2291 candidates = candidates->next;
2297 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2298 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2299 the result of the conversion function to convert it to the final
2300 desired type. Merge the the two sequences into a single sequence,
2301 and return the merged sequence. */
2304 merge_conversion_sequences (tree user_seq, tree std_seq)
2308 my_friendly_assert (TREE_CODE (user_seq) == USER_CONV,
2311 /* Find the end of the second conversion sequence. */
2313 while (TREE_CODE (*t) != IDENTITY_CONV)
2314 t = &TREE_OPERAND (*t, 0);
2316 /* Replace the identity conversion with the user conversion
2320 /* The entire sequence is a user-conversion sequence. */
2321 ICS_USER_FLAG (std_seq) = 1;
2326 /* Returns the best overload candidate to perform the requested
2327 conversion. This function is used for three the overloading situations
2328 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2329 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2330 per [dcl.init.ref], so we ignore temporary bindings. */
2332 static struct z_candidate *
2333 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2335 struct z_candidate *candidates, *cand;
2336 tree fromtype = TREE_TYPE (expr);
2337 tree ctors = NULL_TREE, convs = NULL_TREE;
2338 tree args = NULL_TREE;
2341 /* We represent conversion within a hierarchy using RVALUE_CONV and
2342 BASE_CONV, as specified by [over.best.ics]; these become plain
2343 constructor calls, as specified in [dcl.init]. */
2344 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2345 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2347 if (IS_AGGR_TYPE (totype))
2348 ctors = lookup_fnfields (TYPE_BINFO (totype),
2349 complete_ctor_identifier,
2352 if (IS_AGGR_TYPE (fromtype))
2353 convs = lookup_conversions (fromtype);
2356 flags |= LOOKUP_NO_CONVERSION;
2362 ctors = BASELINK_FUNCTIONS (ctors);
2364 t = build_int_2 (0, 0);
2365 TREE_TYPE (t) = build_pointer_type (totype);
2366 args = build_tree_list (NULL_TREE, expr);
2367 /* We should never try to call the abstract or base constructor
2369 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2370 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2372 args = tree_cons (NULL_TREE, t, args);
2374 for (; ctors; ctors = OVL_NEXT (ctors))
2376 tree ctor = OVL_CURRENT (ctors);
2377 if (DECL_NONCONVERTING_P (ctor))
2380 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2381 cand = add_template_candidate (&candidates, ctor, totype,
2382 NULL_TREE, args, NULL_TREE,
2383 TYPE_BINFO (totype),
2384 TYPE_BINFO (totype),
2388 cand = add_function_candidate (&candidates, ctor, totype,
2389 args, TYPE_BINFO (totype),
2390 TYPE_BINFO (totype),
2394 cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2398 args = build_tree_list (NULL_TREE, build_this (expr));
2400 for (; convs; convs = TREE_CHAIN (convs))
2403 tree conversion_path = TREE_PURPOSE (convs);
2404 int convflags = LOOKUP_NO_CONVERSION;
2406 /* If we are called to convert to a reference type, we are trying to
2407 find an lvalue binding, so don't even consider temporaries. If
2408 we don't find an lvalue binding, the caller will try again to
2409 look for a temporary binding. */
2410 if (TREE_CODE (totype) == REFERENCE_TYPE)
2411 convflags |= LOOKUP_NO_TEMP_BIND;
2413 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2415 tree fn = OVL_CURRENT (fns);
2417 /* [over.match.funcs] For conversion functions, the function
2418 is considered to be a member of the class of the implicit
2419 object argument for the purpose of defining the type of
2420 the implicit object parameter.
2422 So we pass fromtype as CTYPE to add_*_candidate. */
2424 if (TREE_CODE (fn) == TEMPLATE_DECL)
2425 cand = add_template_candidate (&candidates, fn, fromtype,
2428 TYPE_BINFO (fromtype),
2433 cand = add_function_candidate (&candidates, fn, fromtype,
2435 TYPE_BINFO (fromtype),
2441 tree ics = implicit_conversion (totype,
2442 TREE_TYPE (TREE_TYPE (cand->fn)),
2445 cand->second_conv = ics;
2447 if (ics == NULL_TREE)
2449 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2455 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2459 cand = tourney (candidates);
2462 if (flags & LOOKUP_COMPLAIN)
2464 error ("conversion from `%T' to `%T' is ambiguous",
2466 print_z_candidates (candidates);
2469 cand = candidates; /* any one will do */
2470 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2471 ICS_USER_FLAG (cand->second_conv) = 1;
2472 if (!any_strictly_viable (candidates))
2473 ICS_BAD_FLAG (cand->second_conv) = 1;
2474 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2475 ambiguous conversion is no worse than another user-defined
2481 /* Build the user conversion sequence. */
2484 (DECL_CONSTRUCTOR_P (cand->fn)
2485 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2486 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
2487 TREE_OPERAND (convs, 1) = build_zc_wrapper (cand);
2489 /* Combine it with the second conversion sequence. */
2490 cand->second_conv = merge_conversion_sequences (convs,
2493 if (cand->viable == -1)
2494 ICS_BAD_FLAG (cand->second_conv) = 1;
2500 build_user_type_conversion (tree totype, tree expr, int flags)
2502 struct z_candidate *cand
2503 = build_user_type_conversion_1 (totype, expr, flags);
2507 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2508 return error_mark_node;
2509 return convert_from_reference (convert_like (cand->second_conv, expr));
2514 /* Do any initial processing on the arguments to a function call. */
2517 resolve_args (tree args)
2520 for (t = args; t; t = TREE_CHAIN (t))
2522 tree arg = TREE_VALUE (t);
2524 if (arg == error_mark_node)
2525 return error_mark_node;
2526 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2528 error ("invalid use of void expression");
2529 return error_mark_node;
2531 arg = convert_from_reference (arg);
2532 TREE_VALUE (t) = arg;
2537 /* Perform overload resolution on FN, which is called with the ARGS.
2539 Return the candidate function selected by overload resolution, or
2540 NULL if the event that overload resolution failed. In the case
2541 that overload resolution fails, *CANDIDATES will be the set of
2542 candidates considered, and ANY_VIABLE_P will be set to true or
2543 false to indicate whether or not any of the candidates were
2546 The ARGS should already have gone through RESOLVE_ARGS before this
2547 function is called. */
2549 static struct z_candidate *
2550 perform_overload_resolution (tree fn,
2552 struct z_candidate **candidates,
2555 struct z_candidate *cand;
2556 tree explicit_targs = NULL_TREE;
2557 int template_only = 0;
2560 *any_viable_p = true;
2562 /* Check FN and ARGS. */
2563 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2564 || TREE_CODE (fn) == TEMPLATE_DECL
2565 || TREE_CODE (fn) == OVERLOAD
2566 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2568 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2571 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2573 explicit_targs = TREE_OPERAND (fn, 1);
2574 fn = TREE_OPERAND (fn, 0);
2578 /* Add the various candidate functions. */
2579 add_candidates (fn, args, explicit_targs, template_only,
2580 /*conversion_path=*/NULL_TREE,
2581 /*access_path=*/NULL_TREE,
2585 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2589 cand = tourney (*candidates);
2593 /* Return an expression for a call to FN (a namespace-scope function,
2594 or a static member function) with the ARGS. */
2597 build_new_function_call (tree fn, tree args)
2599 struct z_candidate *candidates, *cand;
2602 args = resolve_args (args);
2603 if (args == error_mark_node)
2604 return error_mark_node;
2606 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2610 if (!any_viable_p && candidates && ! candidates->next)
2611 return build_function_call (candidates->fn, args);
2612 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2613 fn = TREE_OPERAND (fn, 0);
2615 error ("no matching function for call to `%D(%A)'",
2616 DECL_NAME (OVL_CURRENT (fn)), args);
2618 error ("call of overloaded `%D(%A)' is ambiguous",
2619 DECL_NAME (OVL_CURRENT (fn)), args);
2621 print_z_candidates (candidates);
2622 return error_mark_node;
2625 return build_over_call (cand, LOOKUP_NORMAL);
2628 /* Build a call to a global operator new. FNNAME is the name of the
2629 operator (either "operator new" or "operator new[]") and ARGS are
2630 the arguments provided. *SIZE points to the total number of bytes
2631 required by the allocation, and is updated if that is changed here.
2632 *COOKIE_SIZE is non-NULL if a cookie should be used. If this
2633 function determines that no cookie should be used, after all,
2634 *COOKIE_SIZE is set to NULL_TREE. */
2637 build_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
2640 struct z_candidate *candidates;
2641 struct z_candidate *cand;
2644 args = tree_cons (NULL_TREE, *size, args);
2645 args = resolve_args (args);
2646 if (args == error_mark_node)
2649 fns = lookup_function_nonclass (fnname, args);
2651 /* Figure out what function is being called. */
2652 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2654 /* If no suitable function could be found, issue an error message
2659 error ("no matching function for call to `%D(%A)'",
2660 DECL_NAME (OVL_CURRENT (fns)), args);
2662 error ("call of overloaded `%D(%A)' is ambiguous",
2663 DECL_NAME (OVL_CURRENT (fns)), args);
2665 print_z_candidates (candidates);
2666 return error_mark_node;
2669 /* If a cookie is required, add some extra space. Whether
2670 or not a cookie is required cannot be determined until
2671 after we know which function was called. */
2674 bool use_cookie = true;
2675 if (!abi_version_at_least (2))
2677 tree placement = TREE_CHAIN (args);
2678 /* In G++ 3.2, the check was implemented incorrectly; it
2679 looked at the placement expression, rather than the
2680 type of the function. */
2681 if (placement && !TREE_CHAIN (placement)
2682 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2690 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2691 /* Skip the size_t parameter. */
2692 arg_types = TREE_CHAIN (arg_types);
2693 /* Check the remaining parameters (if any). */
2695 && TREE_CHAIN (arg_types) == void_list_node
2696 && same_type_p (TREE_VALUE (arg_types),
2700 /* If we need a cookie, adjust the number of bytes allocated. */
2703 /* Update the total size. */
2704 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2705 /* Update the argument list to reflect the adjusted size. */
2706 TREE_VALUE (args) = *size;
2709 *cookie_size = NULL_TREE;
2712 /* Build the CALL_EXPR. */
2713 return build_over_call (cand, LOOKUP_NORMAL);
2717 build_object_call (tree obj, tree args)
2719 struct z_candidate *candidates = 0, *cand;
2720 tree fns, convs, mem_args = NULL_TREE;
2721 tree type = TREE_TYPE (obj);
2724 if (TYPE_PTRMEMFUNC_P (type))
2726 /* It's no good looking for an overloaded operator() on a
2727 pointer-to-member-function. */
2728 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2729 return error_mark_node;
2732 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2733 if (fns == error_mark_node)
2734 return error_mark_node;
2736 args = resolve_args (args);
2738 if (args == error_mark_node)
2739 return error_mark_node;
2743 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2744 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2746 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2748 tree fn = OVL_CURRENT (fns);
2749 if (TREE_CODE (fn) == TEMPLATE_DECL)
2750 add_template_candidate (&candidates, fn, base, NULL_TREE,
2751 mem_args, NULL_TREE,
2754 LOOKUP_NORMAL, DEDUCE_CALL);
2756 add_function_candidate
2757 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
2758 TYPE_BINFO (type), LOOKUP_NORMAL);
2762 convs = lookup_conversions (type);
2764 for (; convs; convs = TREE_CHAIN (convs))
2766 tree fns = TREE_VALUE (convs);
2767 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2769 if ((TREE_CODE (totype) == POINTER_TYPE
2770 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2771 || (TREE_CODE (totype) == REFERENCE_TYPE
2772 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2773 || (TREE_CODE (totype) == REFERENCE_TYPE
2774 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2775 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2776 for (; fns; fns = OVL_NEXT (fns))
2778 tree fn = OVL_CURRENT (fns);
2779 if (TREE_CODE (fn) == TEMPLATE_DECL)
2780 add_template_conv_candidate
2781 (&candidates, fn, obj, args, totype,
2782 /*access_path=*/NULL_TREE,
2783 /*conversion_path=*/NULL_TREE);
2785 add_conv_candidate (&candidates, fn, obj, args,
2786 /*conversion_path=*/NULL_TREE,
2787 /*access_path=*/NULL_TREE);
2791 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2794 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2795 print_z_candidates (candidates);
2796 return error_mark_node;
2799 cand = tourney (candidates);
2802 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2803 print_z_candidates (candidates);
2804 return error_mark_node;
2807 /* Since cand->fn will be a type, not a function, for a conversion
2808 function, we must be careful not to unconditionally look at
2810 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2811 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2812 return build_over_call (cand, LOOKUP_NORMAL);
2814 obj = convert_like_with_context
2815 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2818 return build_function_call (obj, args);
2822 op_error (enum tree_code code, enum tree_code code2,
2823 tree arg1, tree arg2, tree arg3, const char *problem)
2827 if (code == MODIFY_EXPR)
2828 opname = assignment_operator_name_info[code2].name;
2830 opname = operator_name_info[code].name;
2835 error ("%s for ternary 'operator?:' in '%E ? %E : %E'",
2836 problem, arg1, arg2, arg3);
2839 case POSTINCREMENT_EXPR:
2840 case POSTDECREMENT_EXPR:
2841 error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname);
2845 error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2);
2850 error ("%s for '%s' in '%s %E'", problem, opname, opname, arg1);
2855 error ("%s for 'operator%s' in '%E %s %E'",
2856 problem, opname, arg1, opname, arg2);
2858 error ("%s for 'operator%s' in '%s%E'",
2859 problem, opname, opname, arg1);
2864 /* Return the implicit conversion sequence that could be used to
2865 convert E1 to E2 in [expr.cond]. */
2868 conditional_conversion (tree e1, tree e2)
2870 tree t1 = non_reference (TREE_TYPE (e1));
2871 tree t2 = non_reference (TREE_TYPE (e2));
2877 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2878 implicitly converted (clause _conv_) to the type "reference to
2879 T2", subject to the constraint that in the conversion the
2880 reference must bind directly (_dcl.init.ref_) to E1. */
2881 if (real_lvalue_p (e2))
2883 conv = implicit_conversion (build_reference_type (t2),
2886 LOOKUP_NO_TEMP_BIND);
2893 If E1 and E2 have class type, and the underlying class types are
2894 the same or one is a base class of the other: E1 can be converted
2895 to match E2 if the class of T2 is the same type as, or a base
2896 class of, the class of T1, and the cv-qualification of T2 is the
2897 same cv-qualification as, or a greater cv-qualification than, the
2898 cv-qualification of T1. If the conversion is applied, E1 is
2899 changed to an rvalue of type T2 that still refers to the original
2900 source class object (or the appropriate subobject thereof).
2902 FIXME we can't express an rvalue that refers to the original object;
2903 we have to create a new one. */
2904 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2905 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
2907 if (good_base && at_least_as_qualified_p (t2, t1))
2909 conv = build1 (IDENTITY_CONV, t1, e1);
2910 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2911 TYPE_MAIN_VARIANT (t2)))
2913 conv = build_conv (BASE_CONV, t2, conv);
2914 NEED_TEMPORARY_P (conv) = 1;
2917 conv = build_conv (RVALUE_CONV, t2, conv);
2926 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
2927 converted to the type that expression E2 would have if E2 were
2928 converted to an rvalue (or the type it has, if E2 is an rvalue). */
2929 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2932 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2933 arguments to the conditional expression. */
2936 build_conditional_expr (tree arg1, tree arg2, tree arg3)
2941 tree result_type = NULL_TREE;
2942 bool lvalue_p = true;
2943 struct z_candidate *candidates = 0;
2944 struct z_candidate *cand;
2946 /* As a G++ extension, the second argument to the conditional can be
2947 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2948 c'.) If the second operand is omitted, make sure it is
2949 calculated only once. */
2953 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2955 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
2956 if (real_lvalue_p (arg1))
2957 arg2 = arg1 = stabilize_reference (arg1);
2959 arg2 = arg1 = save_expr (arg1);
2964 The first expr ession is implicitly converted to bool (clause
2966 arg1 = perform_implicit_conversion (boolean_type_node, arg1);
2968 /* If something has already gone wrong, just pass that fact up the
2970 if (error_operand_p (arg1)
2971 || error_operand_p (arg2)
2972 || error_operand_p (arg3))
2973 return error_mark_node;
2977 If either the second or the third operand has type (possibly
2978 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2979 array-to-pointer (_conv.array_), and function-to-pointer
2980 (_conv.func_) standard conversions are performed on the second
2981 and third operands. */
2982 arg2_type = TREE_TYPE (arg2);
2983 arg3_type = TREE_TYPE (arg3);
2984 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
2986 /* Do the conversions. We don't these for `void' type arguments
2987 since it can't have any effect and since decay_conversion
2988 does not handle that case gracefully. */
2989 if (!VOID_TYPE_P (arg2_type))
2990 arg2 = decay_conversion (arg2);
2991 if (!VOID_TYPE_P (arg3_type))
2992 arg3 = decay_conversion (arg3);
2993 arg2_type = TREE_TYPE (arg2);
2994 arg3_type = TREE_TYPE (arg3);
2998 One of the following shall hold:
3000 --The second or the third operand (but not both) is a
3001 throw-expression (_except.throw_); the result is of the
3002 type of the other and is an rvalue.
3004 --Both the second and the third operands have type void; the
3005 result is of type void and is an rvalue. */
3006 if ((TREE_CODE (arg2) == THROW_EXPR)
3007 ^ (TREE_CODE (arg3) == THROW_EXPR))
3008 result_type = ((TREE_CODE (arg2) == THROW_EXPR)
3009 ? arg3_type : arg2_type);
3010 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3011 result_type = void_type_node;
3014 error ("`%E' has type `void' and is not a throw-expression",
3015 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3016 return error_mark_node;
3020 goto valid_operands;
3024 Otherwise, if the second and third operand have different types,
3025 and either has (possibly cv-qualified) class type, an attempt is
3026 made to convert each of those operands to the type of the other. */
3027 else if (!same_type_p (arg2_type, arg3_type)
3028 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3030 tree conv2 = conditional_conversion (arg2, arg3);
3031 tree conv3 = conditional_conversion (arg3, arg2);
3035 If both can be converted, or one can be converted but the
3036 conversion is ambiguous, the program is ill-formed. If
3037 neither can be converted, the operands are left unchanged and
3038 further checking is performed as described below. If exactly
3039 one conversion is possible, that conversion is applied to the
3040 chosen operand and the converted operand is used in place of
3041 the original operand for the remainder of this section. */
3042 if ((conv2 && !ICS_BAD_FLAG (conv2)
3043 && conv3 && !ICS_BAD_FLAG (conv3))
3044 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3045 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3047 error ("operands to ?: have different types");
3048 return error_mark_node;
3050 else if (conv2 && !ICS_BAD_FLAG (conv2))
3052 arg2 = convert_like (conv2, arg2);
3053 arg2 = convert_from_reference (arg2);
3054 if (!same_type_p (TREE_TYPE (arg2), arg3_type)
3055 && CLASS_TYPE_P (arg3_type))
3056 /* The types need to match if we're converting to a class type.
3057 If not, we don't care about cv-qual mismatches, since
3058 non-class rvalues are not cv-qualified. */
3060 arg2_type = TREE_TYPE (arg2);
3062 else if (conv3 && !ICS_BAD_FLAG (conv3))
3064 arg3 = convert_like (conv3, arg3);
3065 arg3 = convert_from_reference (arg3);
3066 if (!same_type_p (TREE_TYPE (arg3), arg2_type)
3067 && CLASS_TYPE_P (arg2_type))
3069 arg3_type = TREE_TYPE (arg3);
3075 If the second and third operands are lvalues and have the same
3076 type, the result is of that type and is an lvalue. */
3077 if (real_lvalue_p (arg2)
3078 && real_lvalue_p (arg3)
3079 && same_type_p (arg2_type, arg3_type))
3081 result_type = arg2_type;
3082 goto valid_operands;
3087 Otherwise, the result is an rvalue. If the second and third
3088 operand do not have the same type, and either has (possibly
3089 cv-qualified) class type, overload resolution is used to
3090 determine the conversions (if any) to be applied to the operands
3091 (_over.match.oper_, _over.built_). */
3093 if (!same_type_p (arg2_type, arg3_type)
3094 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3100 /* Rearrange the arguments so that add_builtin_candidate only has
3101 to know about two args. In build_builtin_candidates, the
3102 arguments are unscrambled. */
3106 add_builtin_candidates (&candidates,
3109 ansi_opname (COND_EXPR),
3115 If the overload resolution fails, the program is
3117 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3120 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3121 print_z_candidates (candidates);
3122 return error_mark_node;
3124 cand = tourney (candidates);
3127 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3128 print_z_candidates (candidates);
3129 return error_mark_node;
3134 Otherwise, the conversions thus determined are applied, and
3135 the converted operands are used in place of the original
3136 operands for the remainder of this section. */
3137 conv = TREE_VEC_ELT (cand->convs, 0);
3138 arg1 = convert_like (conv, arg1);
3139 conv = TREE_VEC_ELT (cand->convs, 1);
3140 arg2 = convert_like (conv, arg2);
3141 conv = TREE_VEC_ELT (cand->convs, 2);
3142 arg3 = convert_like (conv, arg3);
3147 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3148 and function-to-pointer (_conv.func_) standard conversions are
3149 performed on the second and third operands.
3151 We need to force the lvalue-to-rvalue conversion here for class types,
3152 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3153 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3156 We use ocp_convert rather than build_user_type_conversion because the
3157 latter returns NULL_TREE on failure, while the former gives an error. */
3159 arg2 = force_rvalue (arg2);
3160 arg2_type = TREE_TYPE (arg2);
3162 arg3 = force_rvalue (arg3);
3163 arg3_type = TREE_TYPE (arg3);
3165 if (arg2 == error_mark_node || arg3 == error_mark_node)
3166 return error_mark_node;
3170 After those conversions, one of the following shall hold:
3172 --The second and third operands have the same type; the result is of
3174 if (same_type_p (arg2_type, arg3_type))
3175 result_type = arg2_type;
3178 --The second and third operands have arithmetic or enumeration
3179 type; the usual arithmetic conversions are performed to bring
3180 them to a common type, and the result is of that type. */
3181 else if ((ARITHMETIC_TYPE_P (arg2_type)
3182 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3183 && (ARITHMETIC_TYPE_P (arg3_type)
3184 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3186 /* In this case, there is always a common type. */
3187 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3190 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3191 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3192 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3193 arg2_type, arg3_type);
3194 else if (extra_warnings
3195 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3196 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3197 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3198 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3199 warning ("enumeral and non-enumeral type in conditional expression");
3201 arg2 = perform_implicit_conversion (result_type, arg2);
3202 arg3 = perform_implicit_conversion (result_type, arg3);
3206 --The second and third operands have pointer type, or one has
3207 pointer type and the other is a null pointer constant; pointer
3208 conversions (_conv.ptr_) and qualification conversions
3209 (_conv.qual_) are performed to bring them to their composite
3210 pointer type (_expr.rel_). The result is of the composite
3213 --The second and third operands have pointer to member type, or
3214 one has pointer to member type and the other is a null pointer
3215 constant; pointer to member conversions (_conv.mem_) and
3216 qualification conversions (_conv.qual_) are performed to bring
3217 them to a common type, whose cv-qualification shall match the
3218 cv-qualification of either the second or the third operand.
3219 The result is of the common type. */
3220 else if ((null_ptr_cst_p (arg2)
3221 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
3222 || (null_ptr_cst_p (arg3)
3223 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
3224 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3225 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3226 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
3228 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3229 arg3, "conditional expression");
3230 if (result_type == error_mark_node)
3231 return error_mark_node;
3232 arg2 = perform_implicit_conversion (result_type, arg2);
3233 arg3 = perform_implicit_conversion (result_type, arg3);
3238 error ("operands to ?: have different types");
3239 return error_mark_node;
3243 result = fold (build (COND_EXPR, result_type, arg1, arg2, arg3));
3244 /* We can't use result_type below, as fold might have returned a
3247 /* Expand both sides into the same slot, hopefully the target of the
3248 ?: expression. We used to check for TARGET_EXPRs here, but now we
3249 sometimes wrap them in NOP_EXPRs so the test would fail. */
3250 if (!lvalue_p && IS_AGGR_TYPE (TREE_TYPE (result)))
3251 result = get_target_expr (result);
3253 /* If this expression is an rvalue, but might be mistaken for an
3254 lvalue, we must add a NON_LVALUE_EXPR. */
3255 if (!lvalue_p && real_lvalue_p (result))
3256 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
3261 /* OPERAND is an operand to an expression. Perform necessary steps
3262 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3266 prep_operand (tree operand)
3270 operand = convert_from_reference (operand);
3271 if (CLASS_TYPE_P (TREE_TYPE (operand))
3272 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3273 /* Make sure the template type is instantiated now. */
3274 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3280 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
3281 OVERLOAD) to the CANDIDATES, returning an updated list of
3282 CANDIDATES. The ARGS are the arguments provided to the call,
3283 without any implicit object parameter. The EXPLICIT_TARGS are
3284 explicit template arguments provided. TEMPLATE_ONLY is true if
3285 only template functions should be considered. CONVERSION_PATH,
3286 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3289 add_candidates (tree fns, tree args,
3290 tree explicit_targs, bool template_only,
3291 tree conversion_path, tree access_path,
3293 struct z_candidate **candidates)
3296 tree non_static_args;
3298 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3299 /* Delay creating the implicit this parameter until it is needed. */
3300 non_static_args = NULL_TREE;
3307 fn = OVL_CURRENT (fns);
3308 /* Figure out which set of arguments to use. */
3309 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3311 /* If this function is a non-static member, prepend the implicit
3312 object parameter. */
3313 if (!non_static_args)
3314 non_static_args = tree_cons (NULL_TREE,
3315 build_this (TREE_VALUE (args)),
3317 fn_args = non_static_args;
3320 /* Otherwise, just use the list of arguments provided. */
3323 if (TREE_CODE (fn) == TEMPLATE_DECL)
3324 add_template_candidate (candidates,
3334 else if (!template_only)
3335 add_function_candidate (candidates,
3342 fns = OVL_NEXT (fns);
3347 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3)
3349 struct z_candidate *candidates = 0, *cand;
3350 tree arglist, fnname;
3352 enum tree_code code2 = NOP_EXPR;
3357 if (error_operand_p (arg1)
3358 || error_operand_p (arg2)
3359 || error_operand_p (arg3))
3360 return error_mark_node;
3362 if (code == MODIFY_EXPR)
3364 code2 = TREE_CODE (arg3);
3366 fnname = ansi_assopname (code2);
3369 fnname = ansi_opname (code);
3371 arg1 = prep_operand (arg1);
3377 case VEC_DELETE_EXPR:
3379 /* Use build_op_new_call and build_op_delete_call instead. */
3383 return build_object_call (arg1, arg2);
3389 arg2 = prep_operand (arg2);
3390 arg3 = prep_operand (arg3);
3392 if (code == COND_EXPR)
3394 if (arg2 == NULL_TREE
3395 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3396 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3397 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3398 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3401 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3402 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3405 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3406 arg2 = integer_zero_node;
3408 arglist = NULL_TREE;
3410 arglist = tree_cons (NULL_TREE, arg3, arglist);
3412 arglist = tree_cons (NULL_TREE, arg2, arglist);
3413 arglist = tree_cons (NULL_TREE, arg1, arglist);
3415 /* Add namespace-scope operators to the list of functions to
3417 add_candidates (lookup_function_nonclass (fnname, arglist),
3418 arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3419 flags, &candidates);
3420 /* Add class-member operators to the candidate set. */
3421 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
3425 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3426 if (fns == error_mark_node)
3429 add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3431 BASELINK_BINFO (fns),
3432 TYPE_BINFO (TREE_TYPE (arg1)),
3433 flags, &candidates);
3436 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3437 to know about two args; a builtin candidate will always have a first
3438 parameter of type bool. We'll handle that in
3439 build_builtin_candidate. */
3440 if (code == COND_EXPR)
3450 args[2] = NULL_TREE;
3453 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
3459 /* For these, the built-in candidates set is empty
3460 [over.match.oper]/3. We don't want non-strict matches
3461 because exact matches are always possible with built-in
3462 operators. The built-in candidate set for COMPONENT_REF
3463 would be empty too, but since there are no such built-in
3464 operators, we accept non-strict matches for them. */
3469 strict_p = pedantic;
3473 candidates = splice_viable (candidates, strict_p, &any_viable_p);
3478 case POSTINCREMENT_EXPR:
3479 case POSTDECREMENT_EXPR:
3480 /* Look for an `operator++ (int)'. If they didn't have
3481 one, then we fall back to the old way of doing things. */
3482 if (flags & LOOKUP_COMPLAIN)
3483 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3485 operator_name_info[code].name);
3486 if (code == POSTINCREMENT_EXPR)
3487 code = PREINCREMENT_EXPR;
3489 code = PREDECREMENT_EXPR;
3490 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3492 /* The caller will deal with these. */
3501 if (flags & LOOKUP_COMPLAIN)
3503 op_error (code, code2, arg1, arg2, arg3, "no match");
3504 print_z_candidates (candidates);
3506 return error_mark_node;
3509 cand = tourney (candidates);
3512 if (flags & LOOKUP_COMPLAIN)
3514 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3515 print_z_candidates (candidates);
3517 return error_mark_node;
3520 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3523 && fnname == ansi_assopname (NOP_EXPR)
3524 && DECL_ARTIFICIAL (cand->fn)
3526 && ! candidates->next->next)
3528 warning ("using synthesized `%#D' for copy assignment",
3530 cp_warning_at (" where cfront would use `%#D'",
3532 ? candidates->next->fn
3536 return build_over_call (cand, LOOKUP_NORMAL);
3539 /* Check for comparison of different enum types. */
3548 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3549 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3550 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3551 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3553 warning ("comparison between `%#T' and `%#T'",
3554 TREE_TYPE (arg1), TREE_TYPE (arg2));
3561 /* We need to strip any leading REF_BIND so that bitfields don't cause
3562 errors. This should not remove any important conversions, because
3563 builtins don't apply to class objects directly. */
3564 conv = TREE_VEC_ELT (cand->convs, 0);
3565 if (TREE_CODE (conv) == REF_BIND)
3566 conv = TREE_OPERAND (conv, 0);
3567 arg1 = convert_like (conv, arg1);
3570 conv = TREE_VEC_ELT (cand->convs, 1);
3571 if (TREE_CODE (conv) == REF_BIND)
3572 conv = TREE_OPERAND (conv, 0);
3573 arg2 = convert_like (conv, arg2);
3577 conv = TREE_VEC_ELT (cand->convs, 2);
3578 if (TREE_CODE (conv) == REF_BIND)
3579 conv = TREE_OPERAND (conv, 0);
3580 arg3 = convert_like (conv, arg3);
3587 return build_modify_expr (arg1, code2, arg2);
3590 return build_indirect_ref (arg1, "unary *");
3595 case TRUNC_DIV_EXPR:
3606 case TRUNC_MOD_EXPR:
3610 case TRUTH_ANDIF_EXPR:
3611 case TRUTH_ORIF_EXPR:
3612 return cp_build_binary_op (code, arg1, arg2);
3617 case TRUTH_NOT_EXPR:
3618 case PREINCREMENT_EXPR:
3619 case POSTINCREMENT_EXPR:
3620 case PREDECREMENT_EXPR:
3621 case POSTDECREMENT_EXPR:
3624 return build_unary_op (code, arg1, candidates != 0);
3627 return build_array_ref (arg1, arg2);
3630 return build_conditional_expr (arg1, arg2, arg3);
3633 return build_m_component_ref
3634 (build_indirect_ref (arg1, NULL), arg2);
3636 /* The caller will deal with these. */
3648 /* Build a call to operator delete. This has to be handled very specially,
3649 because the restrictions on what signatures match are different from all
3650 other call instances. For a normal delete, only a delete taking (void *)
3651 or (void *, size_t) is accepted. For a placement delete, only an exact
3652 match with the placement new is accepted.
3654 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3655 ADDR is the pointer to be deleted.
3656 SIZE is the size of the memory block to be deleted.
3657 FLAGS are the usual overloading flags.
3658 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3661 build_op_delete_call (enum tree_code code, tree addr, tree size,
3662 int flags, tree placement)
3664 tree fn = NULL_TREE;
3665 tree fns, fnname, argtypes, args, type;
3668 if (addr == error_mark_node)
3669 return error_mark_node;
3671 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
3673 fnname = ansi_opname (code);
3675 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3678 If the result of the lookup is ambiguous or inaccessible, or if
3679 the lookup selects a placement deallocation function, the
3680 program is ill-formed.
3682 Therefore, we ask lookup_fnfields to complain about ambiguity. */
3684 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3685 if (fns == error_mark_node)
3686 return error_mark_node;
3691 if (fns == NULL_TREE)
3692 fns = lookup_name_nonclass (fnname);
3699 /* Find the allocation function that is being called. */
3700 call_expr = placement;
3701 /* Extract the function. */
3702 alloc_fn = get_callee_fndecl (call_expr);
3703 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3704 /* Then the second parm type. */
3705 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3706 /* Also the second argument. */
3707 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3711 /* First try it without the size argument. */
3712 argtypes = void_list_node;
3716 /* Strip const and volatile from addr. */
3717 addr = cp_convert (ptr_type_node, addr);
3719 /* We make two tries at finding a matching `operator delete'. On
3720 the first pass, we look for a one-operator (or placement)
3721 operator delete. If we're not doing placement delete, then on
3722 the second pass we look for a two-argument delete. */
3723 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3725 /* Go through the `operator delete' functions looking for one
3726 with a matching type. */
3727 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3733 /* The first argument must be "void *". */
3734 t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
3735 if (!same_type_p (TREE_VALUE (t), ptr_type_node))
3738 /* On the first pass, check the rest of the arguments. */
3741 while (argtypes && t)
3743 if (!same_type_p (TREE_VALUE (argtypes),
3746 argtypes = TREE_CHAIN (argtypes);
3749 if (!argtypes && !t)
3752 /* On the second pass, the second argument must be
3755 && same_type_p (TREE_VALUE (t), sizetype)
3756 && TREE_CHAIN (t) == void_list_node)
3760 /* If we found a match, we're done. */
3765 /* If we have a matching function, call it. */
3768 /* Make sure we have the actual function, and not an
3770 fn = OVL_CURRENT (fn);
3772 /* If the FN is a member function, make sure that it is
3774 if (DECL_CLASS_SCOPE_P (fn))
3775 perform_or_defer_access_check (TYPE_BINFO (type), fn);
3778 args = tree_cons (NULL_TREE, addr, args);
3780 args = tree_cons (NULL_TREE, addr,
3781 build_tree_list (NULL_TREE, size));
3785 /* The placement args might not be suitable for overload
3786 resolution at this point, so build the call directly. */
3788 return build_cxx_call (fn, args, args);
3791 return build_function_call (fn, args);
3794 /* If we are doing placement delete we do nothing if we don't find a
3795 matching op delete. */
3799 error ("no suitable `operator %s' for `%T'",
3800 operator_name_info[(int)code].name, type);
3801 return error_mark_node;
3804 /* If the current scope isn't allowed to access DECL along
3805 BASETYPE_PATH, give an error. The most derived class in
3806 BASETYPE_PATH is the one used to qualify DECL. */
3809 enforce_access (tree basetype_path, tree decl)
3811 my_friendly_assert (TREE_CODE (basetype_path) == TREE_VEC, 20030624);
3813 if (!accessible_p (basetype_path, decl))
3815 if (TREE_PRIVATE (decl))
3816 cp_error_at ("`%+#D' is private", decl);
3817 else if (TREE_PROTECTED (decl))
3818 cp_error_at ("`%+#D' is protected", decl);
3820 cp_error_at ("`%+#D' is inaccessible", decl);
3821 error ("within this context");
3828 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
3829 bitwise or of LOOKUP_* values. If any errors are warnings are
3830 generated, set *DIAGNOSTIC_FN to "error" or "warning",
3831 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
3835 build_temp (tree expr, tree type, int flags,
3836 void (**diagnostic_fn)(const char *, ...))
3840 savew = warningcount, savee = errorcount;
3841 expr = build_special_member_call (NULL_TREE,
3842 complete_ctor_identifier,
3843 build_tree_list (NULL_TREE, expr),
3846 if (warningcount > savew)
3847 *diagnostic_fn = warning;
3848 else if (errorcount > savee)
3849 *diagnostic_fn = error;
3851 *diagnostic_fn = NULL;
3856 /* Perform the conversions in CONVS on the expression EXPR. FN and
3857 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3858 indicates the `this' argument of a method. INNER is nonzero when
3859 being called to continue a conversion chain. It is negative when a
3860 reference binding will be applied, positive otherwise. If
3861 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
3862 conversions will be emitted if appropriate. */
3865 convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
3866 bool issue_conversion_warnings)
3868 tree totype = TREE_TYPE (convs);
3869 void (*diagnostic_fn)(const char *, ...);
3871 if (ICS_BAD_FLAG (convs)
3872 && TREE_CODE (convs) != USER_CONV
3873 && TREE_CODE (convs) != AMBIG_CONV
3874 && TREE_CODE (convs) != REF_BIND)
3877 for (; t; t = TREE_OPERAND (t, 0))
3879 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3881 expr = convert_like_real (t, expr, fn, argnum, 1,
3882 /*issue_conversion_warnings=*/false);
3885 else if (TREE_CODE (t) == AMBIG_CONV)
3886 return convert_like_real (t, expr, fn, argnum, 1,
3887 /*issue_conversion_warnings=*/false);
3888 else if (TREE_CODE (t) == IDENTITY_CONV)
3891 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3893 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3894 return cp_convert (totype, expr);
3897 if (issue_conversion_warnings)
3898 expr = dubious_conversion_warnings
3899 (totype, expr, "argument", fn, argnum);
3900 switch (TREE_CODE (convs))
3904 struct z_candidate *cand = USER_CONV_CAND (convs);
3905 tree convfn = cand->fn;
3908 if (DECL_CONSTRUCTOR_P (convfn))
3910 tree t = build_int_2 (0, 0);
3911 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3913 args = build_tree_list (NULL_TREE, expr);
3914 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3915 || DECL_HAS_VTT_PARM_P (convfn))
3916 /* We should never try to call the abstract or base constructor
3919 args = tree_cons (NULL_TREE, t, args);
3922 args = build_this (expr);
3923 expr = build_over_call (cand, LOOKUP_NORMAL);
3925 /* If this is a constructor or a function returning an aggr type,
3926 we need to build up a TARGET_EXPR. */
3927 if (DECL_CONSTRUCTOR_P (convfn))
3928 expr = build_cplus_new (totype, expr);
3930 /* The result of the call is then used to direct-initialize the object
3931 that is the destination of the copy-initialization. [dcl.init]
3933 Note that this step is not reflected in the conversion sequence;
3934 it affects the semantics when we actually perform the
3935 conversion, but is not considered during overload resolution.
3937 If the target is a class, that means call a ctor. */
3938 if (IS_AGGR_TYPE (totype)
3939 && (inner >= 0 || !lvalue_p (expr)))
3943 /* Core issue 84, now a DR, says that we don't
3944 allow UDCs for these args (which deliberately
3945 breaks copy-init of an auto_ptr<Base> from an
3946 auto_ptr<Derived>). */
3947 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
3954 (" initializing argument %P of `%D' from result of `%D'",
3955 argnum, fn, convfn);
3958 (" initializing temporary from result of `%D'", convfn);
3960 expr = build_cplus_new (totype, expr);
3965 if (type_unknown_p (expr))
3966 expr = instantiate_type (totype, expr, tf_error | tf_warning);
3967 /* Convert a non-array constant variable to its underlying value, unless we
3968 are about to bind it to a reference, in which case we need to
3969 leave it as an lvalue. */
3971 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3972 expr = decl_constant_value (expr);
3973 if (CHECK_COPY_CONSTRUCTOR_P (convs))
3974 /* Generate a temporary copy purely to generate the required
3976 build_temp (build_dummy_object (totype), totype,
3977 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
3981 /* Call build_user_type_conversion again for the error. */
3982 return build_user_type_conversion
3983 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3989 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
3990 TREE_CODE (convs) == REF_BIND ? -1 : 1,
3991 /*issue_conversion_warnings=*/false);
3992 if (expr == error_mark_node)
3993 return error_mark_node;
3995 switch (TREE_CODE (convs))
3998 if (! IS_AGGR_TYPE (totype))
4000 /* Else fall through. */
4002 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4004 /* We are going to bind a reference directly to a base-class
4005 subobject of EXPR. */
4006 if (CHECK_COPY_CONSTRUCTOR_P (convs))
4007 /* Generate a temporary copy purely to generate the required
4009 build_temp (build_dummy_object (TREE_TYPE (expr)),
4011 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4013 /* Build an expression for `*((base*) &expr)'. */
4014 expr = build_unary_op (ADDR_EXPR, expr, 0);
4015 expr = perform_implicit_conversion (build_pointer_type (totype),
4017 expr = build_indirect_ref (expr, "implicit conversion");
4021 /* Copy-initialization where the cv-unqualified version of the source
4022 type is the same class as, or a derived class of, the class of the
4023 destination [is treated as direct-initialization]. [dcl.init] */
4024 expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4026 if (diagnostic_fn && fn)
4027 diagnostic_fn (" initializing argument %P of `%D'", argnum, fn);
4028 return build_cplus_new (totype, expr);
4032 tree ref_type = totype;
4034 /* If necessary, create a temporary. */
4035 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
4037 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4039 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4041 /* If the reference is volatile or non-const, we
4042 cannot create a temporary. */
4043 cp_lvalue_kind lvalue = real_lvalue_p (expr);
4045 if (lvalue & clk_bitfield)
4046 error ("cannot bind bitfield `%E' to `%T'",
4048 else if (lvalue & clk_packed)
4049 error ("cannot bind packed field `%E' to `%T'",
4052 error ("cannot bind rvalue `%E' to `%T'", expr, ref_type);
4053 return error_mark_node;
4055 expr = build_target_expr_with_type (expr, type);
4058 /* Take the address of the thing to which we will bind the
4060 expr = build_unary_op (ADDR_EXPR, expr, 1);
4061 if (expr == error_mark_node)
4062 return error_mark_node;
4064 /* Convert it to a pointer to the type referred to by the
4065 reference. This will adjust the pointer if a derived to
4066 base conversion is being performed. */
4067 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4069 /* Convert the pointer to the desired reference type. */
4070 return build_nop (ref_type, expr);
4074 return decay_conversion (expr);
4077 /* Warn about deprecated conversion if appropriate. */
4078 string_conv_p (totype, expr, 1);
4084 return ocp_convert (totype, expr, CONV_IMPLICIT,
4085 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4088 /* Build a call to __builtin_trap which can be used as an expression of
4092 call_builtin_trap (tree type)
4094 tree fn = IDENTIFIER_GLOBAL_VALUE (get_identifier ("__builtin_trap"));
4096 my_friendly_assert (fn != NULL, 20030927);
4097 fn = build_call (fn, NULL_TREE);
4098 fn = build (COMPOUND_EXPR, type, fn, error_mark_node);
4099 fn = force_target_expr (type, fn);
4103 /* ARG is being passed to a varargs function. Perform any conversions
4104 required. Return the converted value. */
4107 convert_arg_to_ellipsis (tree arg)
4111 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4112 standard conversions are performed. */
4113 arg = decay_conversion (arg);
4116 If the argument has integral or enumeration type that is subject
4117 to the integral promotions (_conv.prom_), or a floating point
4118 type that is subject to the floating point promotion
4119 (_conv.fpprom_), the value of the argument is converted to the
4120 promoted type before the call. */
4121 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4122 && (TYPE_PRECISION (TREE_TYPE (arg))
4123 < TYPE_PRECISION (double_type_node)))
4124 arg = convert_to_real (double_type_node, arg);
4125 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4126 arg = perform_integral_promotions (arg);
4128 arg = require_complete_type (arg);
4130 if (arg != error_mark_node
4131 && !pod_type_p (TREE_TYPE (arg)))
4133 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4134 here and do a bitwise copy, but now cp_expr_size will abort if we
4136 If the call appears in the context of a sizeof expression,
4137 there is no need to emit a warning, since the expression won't be
4138 evaluated. We keep the builtin_trap just as a safety check. */
4139 if (!skip_evaluation)
4140 warning ("cannot pass objects of non-POD type `%#T' through `...'; "
4141 "call will abort at runtime", TREE_TYPE (arg));
4142 arg = call_builtin_trap (TREE_TYPE (arg));
4148 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4151 build_x_va_arg (tree expr, tree type)
4153 if (processing_template_decl)
4154 return build_min (VA_ARG_EXPR, type, expr);
4156 type = complete_type_or_else (type, NULL_TREE);
4158 if (expr == error_mark_node || !type)
4159 return error_mark_node;
4161 if (! pod_type_p (type))
4163 /* Undefined behavior [expr.call] 5.2.2/7. */
4164 warning ("cannot receive objects of non-POD type `%#T' through `...'; \
4165 call will abort at runtime",
4167 return call_builtin_trap (type);
4170 return build_va_arg (expr, type);
4173 /* TYPE has been given to va_arg. Apply the default conversions which
4174 would have happened when passed via ellipsis. Return the promoted
4175 type, or the passed type if there is no change. */
4178 cxx_type_promotes_to (tree type)
4182 /* Perform the array-to-pointer and function-to-pointer
4184 type = type_decays_to (type);
4186 promote = type_promotes_to (type);
4187 if (same_type_p (type, promote))
4193 /* ARG is a default argument expression being passed to a parameter of
4194 the indicated TYPE, which is a parameter to FN. Do any required
4195 conversions. Return the converted value. */
4198 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4200 /* If the ARG is an unparsed default argument expression, the
4201 conversion cannot be performed. */
4202 if (TREE_CODE (arg) == DEFAULT_ARG)
4204 error ("the default argument for parameter %d of `%D' has "
4205 "not yet been parsed",
4207 return error_mark_node;
4210 if (fn && DECL_TEMPLATE_INFO (fn))
4211 arg = tsubst_default_argument (fn, type, arg);
4213 arg = break_out_target_exprs (arg);
4215 if (TREE_CODE (arg) == CONSTRUCTOR)
4217 arg = digest_init (type, arg, 0);
4218 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4219 "default argument", fn, parmnum);
4223 /* This could get clobbered by the following call. */
4224 if (TREE_HAS_CONSTRUCTOR (arg))
4225 arg = copy_node (arg);
4227 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4228 "default argument", fn, parmnum);
4229 arg = convert_for_arg_passing (type, arg);
4235 /* Returns the type which will really be used for passing an argument of
4239 type_passed_as (tree type)
4241 /* Pass classes with copy ctors by invisible reference. */
4242 if (TREE_ADDRESSABLE (type))
4243 type = build_reference_type (type);
4244 else if (PROMOTE_PROTOTYPES
4245 && INTEGRAL_TYPE_P (type)
4246 && COMPLETE_TYPE_P (type)
4247 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4248 TYPE_SIZE (integer_type_node)))
4249 type = integer_type_node;
4254 /* Actually perform the appropriate conversion. */
4257 convert_for_arg_passing (tree type, tree val)
4259 if (val == error_mark_node)
4261 /* Pass classes with copy ctors by invisible reference. */
4262 else if (TREE_ADDRESSABLE (type))
4263 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4264 else if (PROMOTE_PROTOTYPES
4265 && INTEGRAL_TYPE_P (type)
4266 && COMPLETE_TYPE_P (type)
4267 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4268 TYPE_SIZE (integer_type_node)))
4269 val = perform_integral_promotions (val);
4273 /* Returns true iff FN is a function with magic varargs, i.e. ones for
4274 which no conversions at all should be done. This is true for some
4275 builtins which don't act like normal functions. */
4278 magic_varargs_p (tree fn)
4280 if (DECL_BUILT_IN (fn))
4281 switch (DECL_FUNCTION_CODE (fn))
4283 case BUILT_IN_CLASSIFY_TYPE:
4284 case BUILT_IN_CONSTANT_P:
4285 case BUILT_IN_NEXT_ARG:
4286 case BUILT_IN_STDARG_START:
4287 case BUILT_IN_VA_START:
4296 /* Subroutine of the various build_*_call functions. Overload resolution
4297 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4298 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4299 bitmask of various LOOKUP_* flags which apply to the call itself. */
4302 build_over_call (struct z_candidate *cand, int flags)
4305 tree args = cand->args;
4306 tree convs = cand->convs;
4307 tree converted_args = NULL_TREE;
4308 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4309 tree conv, arg, val;
4313 /* In a template, there is no need to perform all of the work that
4314 is normally done. We are only interested in the type of the call
4315 expression, i.e., the return type of the function. Any semantic
4316 errors will be deferred until the template is instantiated. */
4317 if (processing_template_decl)
4321 return_type = TREE_TYPE (TREE_TYPE (fn));
4322 expr = build (CALL_EXPR, return_type, fn, args);
4323 if (!VOID_TYPE_P (return_type))
4324 require_complete_type (return_type);
4325 return convert_from_reference (expr);
4328 /* Give any warnings we noticed during overload resolution. */
4330 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4331 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4333 if (DECL_FUNCTION_MEMBER_P (fn))
4335 /* If FN is a template function, two cases must be considered.
4340 template <class T> void f();
4342 template <class T> struct B {
4346 struct C : A, B<int> {
4348 using B<int>::g; // #2
4351 In case #1 where `A::f' is a member template, DECL_ACCESS is
4352 recorded in the primary template but not in its specialization.
4353 We check access of FN using its primary template.
4355 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4356 because it is a member of class template B, DECL_ACCESS is
4357 recorded in the specialization `B<int>::g'. We cannot use its
4358 primary template because `B<T>::g' and `B<int>::g' may have
4359 different access. */
4360 if (DECL_TEMPLATE_INFO (fn)
4361 && is_member_template (DECL_TI_TEMPLATE (fn)))
4362 perform_or_defer_access_check (cand->access_path,
4363 DECL_TI_TEMPLATE (fn));
4365 perform_or_defer_access_check (cand->access_path, fn);
4368 if (args && TREE_CODE (args) != TREE_LIST)
4369 args = build_tree_list (NULL_TREE, args);
4372 /* The implicit parameters to a constructor are not considered by overload
4373 resolution, and must be of the proper type. */
4374 if (DECL_CONSTRUCTOR_P (fn))
4376 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4377 arg = TREE_CHAIN (arg);
4378 parm = TREE_CHAIN (parm);
4379 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4380 /* We should never try to call the abstract constructor. */
4382 if (DECL_HAS_VTT_PARM_P (fn))
4384 converted_args = tree_cons
4385 (NULL_TREE, TREE_VALUE (arg), converted_args);
4386 arg = TREE_CHAIN (arg);
4387 parm = TREE_CHAIN (parm);
4390 /* Bypass access control for 'this' parameter. */
4391 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4393 tree parmtype = TREE_VALUE (parm);
4394 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4398 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4399 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4400 TREE_TYPE (argtype), fn);
4402 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4403 X is called for an object that is not of type X, or of a type
4404 derived from X, the behavior is undefined.
4406 So we can assume that anything passed as 'this' is non-null, and
4407 optimize accordingly. */
4408 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4409 /* Convert to the base in which the function was declared. */
4410 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4411 converted_arg = build_base_path (PLUS_EXPR,
4413 cand->conversion_path,
4415 /* Check that the base class is accessible. */
4416 if (!accessible_base_p (TREE_TYPE (argtype),
4417 BINFO_TYPE (cand->conversion_path)))
4418 error ("`%T' is not an accessible base of `%T'",
4419 BINFO_TYPE (cand->conversion_path),
4420 TREE_TYPE (argtype));
4421 /* If fn was found by a using declaration, the conversion path
4422 will be to the derived class, not the base declaring fn. We
4423 must convert from derived to base. */
4424 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4425 TREE_TYPE (parmtype), ba_ignore, NULL);
4426 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4429 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4430 parm = TREE_CHAIN (parm);
4431 arg = TREE_CHAIN (arg);
4437 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4439 tree type = TREE_VALUE (parm);
4441 conv = TREE_VEC_ELT (convs, i);
4442 val = convert_like_with_context
4443 (conv, TREE_VALUE (arg), fn, i - is_method);
4445 val = convert_for_arg_passing (type, val);
4446 converted_args = tree_cons (NULL_TREE, val, converted_args);
4449 /* Default arguments */
4450 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4452 = tree_cons (NULL_TREE,
4453 convert_default_arg (TREE_VALUE (parm),
4454 TREE_PURPOSE (parm),
4459 for (; arg; arg = TREE_CHAIN (arg))
4461 tree a = TREE_VALUE (arg);
4462 if (magic_varargs_p (fn))
4463 /* Do no conversions for magic varargs. */;
4465 a = convert_arg_to_ellipsis (a);
4466 converted_args = tree_cons (NULL_TREE, a, converted_args);
4469 converted_args = nreverse (converted_args);
4472 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4475 /* Avoid actually calling copy constructors and copy assignment operators,
4478 if (! flag_elide_constructors)
4479 /* Do things the hard way. */;
4480 else if (TREE_VEC_LENGTH (convs) == 1
4481 && DECL_COPY_CONSTRUCTOR_P (fn))
4484 arg = skip_artificial_parms_for (fn, converted_args);
4485 arg = TREE_VALUE (arg);
4487 /* Pull out the real argument, disregarding const-correctness. */
4489 while (TREE_CODE (targ) == NOP_EXPR
4490 || TREE_CODE (targ) == NON_LVALUE_EXPR
4491 || TREE_CODE (targ) == CONVERT_EXPR)
4492 targ = TREE_OPERAND (targ, 0);
4493 if (TREE_CODE (targ) == ADDR_EXPR)
4495 targ = TREE_OPERAND (targ, 0);
4496 if (!same_type_ignoring_top_level_qualifiers_p
4497 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4506 arg = build_indirect_ref (arg, 0);
4508 /* [class.copy]: the copy constructor is implicitly defined even if
4509 the implementation elided its use. */
4510 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4513 /* If we're creating a temp and we already have one, don't create a
4514 new one. If we're not creating a temp but we get one, use
4515 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4516 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4517 temp or an INIT_EXPR otherwise. */
4518 if (integer_zerop (TREE_VALUE (args)))
4520 if (TREE_CODE (arg) == TARGET_EXPR)
4522 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4523 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4525 else if (TREE_CODE (arg) == TARGET_EXPR
4526 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4529 tree to = stabilize_reference
4530 (build_indirect_ref (TREE_VALUE (args), 0));
4532 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4533 address = build_unary_op (ADDR_EXPR, val, 0);
4534 /* Avoid a warning about this expression, if the address is
4536 TREE_USED (address) = 1;
4540 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4542 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4544 tree to = stabilize_reference
4545 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4546 tree type = TREE_TYPE (to);
4547 tree as_base = CLASSTYPE_AS_BASE (type);
4549 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4550 if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
4551 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4554 /* We must only copy the non-tail padding parts. Use
4555 CLASSTYPE_AS_BASE for the bitwise copy. */
4556 tree to_ptr, arg_ptr, to_as_base, arg_as_base, base_ptr_type;
4559 to_ptr = save_expr (build_unary_op (ADDR_EXPR, to, 0));
4560 arg_ptr = build_unary_op (ADDR_EXPR, arg, 0);
4562 base_ptr_type = build_pointer_type (as_base);
4563 to_as_base = build_nop (base_ptr_type, to_ptr);
4564 to_as_base = build_indirect_ref (to_as_base, 0);
4565 arg_as_base = build_nop (base_ptr_type, arg_ptr);
4566 arg_as_base = build_indirect_ref (arg_as_base, 0);
4568 save_to = build_indirect_ref (to_ptr, 0);
4570 val = build (MODIFY_EXPR, as_base, to_as_base, arg_as_base);
4571 val = convert_to_void (val, NULL);
4572 val = build (COMPOUND_EXPR, type, val, save_to);
4573 TREE_NO_UNUSED_WARNING (val) = 1;
4581 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4583 tree t, *p = &TREE_VALUE (converted_args);
4584 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4587 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4589 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4590 if (TREE_SIDE_EFFECTS (*p))
4591 *p = save_expr (*p);
4592 t = build_pointer_type (TREE_TYPE (fn));
4593 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4594 fn = build_java_interface_fn_ref (fn, *p);
4596 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4599 else if (DECL_INLINE (fn))
4600 fn = inline_conversion (fn);
4602 fn = build_addr_func (fn);
4604 return build_cxx_call (fn, args, converted_args);
4607 /* Build and return a call to FN, using the the CONVERTED_ARGS. ARGS
4608 gives the original form of the arguments. This function performs
4609 no overload resolution, conversion, or other high-level
4613 build_cxx_call(tree fn, tree args, tree converted_args)
4617 /* Recognize certain built-in functions so we can make tree-codes
4618 other than CALL_EXPR. We do this when it enables fold-const.c
4619 to do something useful. */
4620 if (TREE_CODE (fn) == ADDR_EXPR
4621 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4622 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4625 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4630 fn = build_call (fn, converted_args);
4632 /* If this call might throw an exception, note that fact. */
4633 fndecl = get_callee_fndecl (fn);
4634 if ((!fndecl || !TREE_NOTHROW (fndecl))
4635 && at_function_scope_p ()
4637 cp_function_chain->can_throw = 1;
4639 /* Some built-in function calls will be evaluated at compile-time in
4643 if (VOID_TYPE_P (TREE_TYPE (fn)))
4646 fn = require_complete_type (fn);
4647 if (fn == error_mark_node)
4648 return error_mark_node;
4650 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4651 fn = build_cplus_new (TREE_TYPE (fn), fn);
4652 return convert_from_reference (fn);
4655 static GTY(()) tree java_iface_lookup_fn;
4657 /* Make an expression which yields the address of the Java interface
4658 method FN. This is achieved by generating a call to libjava's
4659 _Jv_LookupInterfaceMethodIdx(). */
4662 build_java_interface_fn_ref (tree fn, tree instance)
4664 tree lookup_args, lookup_fn, method, idx;
4665 tree klass_ref, iface, iface_ref;
4668 if (!java_iface_lookup_fn)
4670 tree endlink = build_void_list_node ();
4671 tree t = tree_cons (NULL_TREE, ptr_type_node,
4672 tree_cons (NULL_TREE, ptr_type_node,
4673 tree_cons (NULL_TREE, java_int_type_node,
4675 java_iface_lookup_fn
4676 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4677 build_function_type (ptr_type_node, t),
4678 0, NOT_BUILT_IN, NULL, NULL_TREE);
4681 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4682 This is the first entry in the vtable. */
4683 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4686 /* Get the java.lang.Class pointer for the interface being called. */
4687 iface = DECL_CONTEXT (fn);
4688 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
4689 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4690 || DECL_CONTEXT (iface_ref) != iface)
4692 error ("could not find class$ field in java interface type `%T'",
4694 return error_mark_node;
4696 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4698 /* Determine the itable index of FN. */
4700 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4702 if (!DECL_VIRTUAL_P (method))
4708 idx = build_int_2 (i, 0);
4710 lookup_args = tree_cons (NULL_TREE, klass_ref,
4711 tree_cons (NULL_TREE, iface_ref,
4712 build_tree_list (NULL_TREE, idx)));
4713 lookup_fn = build1 (ADDR_EXPR,
4714 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4715 java_iface_lookup_fn);
4716 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4719 /* Returns the value to use for the in-charge parameter when making a
4720 call to a function with the indicated NAME. */
4723 in_charge_arg_for_name (tree name)
4725 if (name == base_ctor_identifier
4726 || name == base_dtor_identifier)
4727 return integer_zero_node;
4728 else if (name == complete_ctor_identifier)
4729 return integer_one_node;
4730 else if (name == complete_dtor_identifier)
4731 return integer_two_node;
4732 else if (name == deleting_dtor_identifier)
4733 return integer_three_node;
4735 /* This function should only be called with one of the names listed
4741 /* Build a call to a constructor, destructor, or an assignment
4742 operator for INSTANCE, an expression with class type. NAME
4743 indicates the special member function to call; ARGS are the
4744 arguments. BINFO indicates the base of INSTANCE that is to be
4745 passed as the `this' parameter to the member function called.
4747 FLAGS are the LOOKUP_* flags to use when processing the call.
4749 If NAME indicates a complete object constructor, INSTANCE may be
4750 NULL_TREE. In this case, the caller will call build_cplus_new to
4751 store the newly constructed object into a VAR_DECL. */
4754 build_special_member_call (tree instance, tree name, tree args,
4755 tree binfo, int flags)
4758 /* The type of the subobject to be constructed or destroyed. */
4761 my_friendly_assert (name == complete_ctor_identifier
4762 || name == base_ctor_identifier
4763 || name == complete_dtor_identifier
4764 || name == base_dtor_identifier
4765 || name == deleting_dtor_identifier
4766 || name == ansi_assopname (NOP_EXPR),
4768 my_friendly_assert (binfo != NULL_TREE, 20020712);
4770 class_type = BINFO_TYPE (binfo);
4772 /* Handle the special case where INSTANCE is NULL_TREE. */
4773 if (name == complete_ctor_identifier && !instance)
4775 instance = build_int_2 (0, 0);
4776 TREE_TYPE (instance) = build_pointer_type (class_type);
4777 instance = build1 (INDIRECT_REF, class_type, instance);
4781 if (name == complete_dtor_identifier
4782 || name == base_dtor_identifier
4783 || name == deleting_dtor_identifier)
4784 my_friendly_assert (args == NULL_TREE, 20020712);
4786 /* Convert to the base class, if necessary. */
4787 if (!same_type_ignoring_top_level_qualifiers_p
4788 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
4790 if (name != ansi_assopname (NOP_EXPR))
4791 /* For constructors and destructors, either the base is
4792 non-virtual, or it is virtual but we are doing the
4793 conversion from a constructor or destructor for the
4794 complete object. In either case, we can convert
4796 instance = convert_to_base_statically (instance, binfo);
4798 /* However, for assignment operators, we must convert
4799 dynamically if the base is virtual. */
4800 instance = build_base_path (PLUS_EXPR, instance,
4801 binfo, /*nonnull=*/1);
4805 my_friendly_assert (instance != NULL_TREE, 20020712);
4807 /* Resolve the name. */
4808 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4809 return error_mark_node;
4811 fns = lookup_fnfields (binfo, name, 1);
4813 /* When making a call to a constructor or destructor for a subobject
4814 that uses virtual base classes, pass down a pointer to a VTT for
4816 if ((name == base_ctor_identifier
4817 || name == base_dtor_identifier)
4818 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4823 /* If the current function is a complete object constructor
4824 or destructor, then we fetch the VTT directly.
4825 Otherwise, we look it up using the VTT we were given. */
4826 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4827 vtt = decay_conversion (vtt);
4828 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4829 build (EQ_EXPR, boolean_type_node,
4830 current_in_charge_parm, integer_zero_node),
4833 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4834 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4835 BINFO_SUBVTT_INDEX (binfo));
4837 args = tree_cons (NULL_TREE, sub_vtt, args);
4840 return build_new_method_call (instance, fns, args,
4841 TYPE_BINFO (BINFO_TYPE (binfo)),
4845 /* Return the NAME, as a C string. The NAME indicates a function that
4846 is a member of TYPE. *FREE_P is set to true if the caller must
4847 free the memory returned.
4849 Rather than go through all of this, we should simply set the names
4850 of constructors and destructors appropriately, and dispense with
4851 ctor_identifier, dtor_identifier, etc. */
4854 name_as_c_string (tree name, tree type, bool *free_p)
4858 /* Assume that we will not allocate memory. */
4860 /* Constructors and destructors are special. */
4861 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4864 = (char *) IDENTIFIER_POINTER (constructor_name (type));
4865 /* For a destructor, add the '~'. */
4866 if (name == complete_dtor_identifier
4867 || name == base_dtor_identifier
4868 || name == deleting_dtor_identifier)
4870 pretty_name = concat ("~", pretty_name, NULL);
4871 /* Remember that we need to free the memory allocated. */
4875 else if (IDENTIFIER_TYPENAME_P (name))
4877 pretty_name = concat ("operator ",
4878 type_as_string (TREE_TYPE (name),
4879 TFF_PLAIN_IDENTIFIER),
4881 /* Remember that we need to free the memory allocated. */
4885 pretty_name = (char *) IDENTIFIER_POINTER (name);
4890 /* Build a call to "INSTANCE.FN (ARGS)". */
4893 build_new_method_call (tree instance, tree fns, tree args,
4894 tree conversion_path, int flags)
4896 struct z_candidate *candidates = 0, *cand;
4897 tree explicit_targs = NULL_TREE;
4898 tree basetype = NULL_TREE;
4901 tree mem_args = NULL_TREE, instance_ptr;
4907 int template_only = 0;
4913 my_friendly_assert (instance != NULL_TREE, 20020729);
4915 if (error_operand_p (instance)
4916 || error_operand_p (fns)
4917 || args == error_mark_node)
4918 return error_mark_node;
4920 orig_instance = instance;
4924 if (processing_template_decl)
4926 instance = build_non_dependent_expr (instance);
4927 if (!BASELINK_P (fns)
4928 && TREE_CODE (fns) != PSEUDO_DTOR_EXPR
4929 && TREE_TYPE (fns) != unknown_type_node)
4930 fns = build_non_dependent_expr (fns);
4931 args = build_non_dependent_args (orig_args);
4934 /* Process the argument list. */
4936 args = resolve_args (args);
4937 if (args == error_mark_node)
4938 return error_mark_node;
4940 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4941 instance = convert_from_reference (instance);
4942 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4943 instance_ptr = build_this (instance);
4945 if (!BASELINK_P (fns))
4947 error ("call to non-function `%D'", fns);
4948 return error_mark_node;
4951 if (!conversion_path)
4952 conversion_path = BASELINK_BINFO (fns);
4953 access_binfo = BASELINK_ACCESS_BINFO (fns);
4954 optype = BASELINK_OPTYPE (fns);
4955 fns = BASELINK_FUNCTIONS (fns);
4957 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4959 explicit_targs = TREE_OPERAND (fns, 1);
4960 fns = TREE_OPERAND (fns, 0);
4964 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
4965 || TREE_CODE (fns) == TEMPLATE_DECL
4966 || TREE_CODE (fns) == OVERLOAD,
4969 /* XXX this should be handled before we get here. */
4970 if (! IS_AGGR_TYPE (basetype))
4972 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
4973 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
4974 fns, instance, basetype);
4976 return error_mark_node;
4979 fn = get_first_fn (fns);
4980 name = DECL_NAME (fn);
4982 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4984 /* Callers should explicitly indicate whether they want to construct
4985 the complete object or just the part without virtual bases. */
4986 my_friendly_assert (name != ctor_identifier, 20000408);
4987 /* Similarly for destructors. */
4988 my_friendly_assert (name != dtor_identifier, 20000408);
4991 /* It's OK to call destructors on cv-qualified objects. Therefore,
4992 convert the INSTANCE_PTR to the unqualified type, if necessary. */
4993 if (DECL_DESTRUCTOR_P (fn))
4995 tree type = build_pointer_type (basetype);
4996 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
4997 instance_ptr = build_nop (type, instance_ptr);
5000 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5001 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
5003 for (fn = fns; fn; fn = OVL_NEXT (fn))
5005 tree t = OVL_CURRENT (fn);
5008 /* We can end up here for copy-init of same or base class. */
5009 if ((flags & LOOKUP_ONLYCONVERTING)
5010 && DECL_NONCONVERTING_P (t))
5013 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5014 this_arglist = mem_args;
5016 this_arglist = args;
5018 if (TREE_CODE (t) == TEMPLATE_DECL)
5019 /* A member template. */
5020 add_template_candidate (&candidates, t,
5023 this_arglist, optype,
5028 else if (! template_only)
5029 add_function_candidate (&candidates, t,
5037 candidates = splice_viable (candidates, pedantic, &any_viable_p);
5040 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
5041 if (flags & LOOKUP_SPECULATIVELY)
5043 if (!COMPLETE_TYPE_P (basetype))
5044 cxx_incomplete_type_error (instance_ptr, basetype);
5050 pretty_name = name_as_c_string (name, basetype, &free_p);
5051 error ("no matching function for call to `%T::%s(%A)%#V'",
5052 basetype, pretty_name, user_args,
5053 TREE_TYPE (TREE_TYPE (instance_ptr)));
5057 print_z_candidates (candidates);
5058 return error_mark_node;
5061 cand = tourney (candidates);
5067 pretty_name = name_as_c_string (name, basetype, &free_p);
5068 error ("call of overloaded `%s(%A)' is ambiguous", pretty_name,
5070 print_z_candidates (candidates);
5073 return error_mark_node;
5076 if (DECL_PURE_VIRTUAL_P (cand->fn)
5077 && instance == current_class_ref
5078 && (DECL_CONSTRUCTOR_P (current_function_decl)
5079 || DECL_DESTRUCTOR_P (current_function_decl))
5080 && ! (flags & LOOKUP_NONVIRTUAL)
5081 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
5082 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
5083 "abstract virtual `%#D' called from constructor"
5084 : "abstract virtual `%#D' called from destructor"),
5086 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5087 && is_dummy_object (instance_ptr))
5089 error ("cannot call member function `%D' without object", cand->fn);
5090 return error_mark_node;
5093 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5094 && resolves_to_fixed_type_p (instance, 0))
5095 flags |= LOOKUP_NONVIRTUAL;
5097 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
5098 call = build_over_call (cand, flags);
5101 call = build_over_call (cand, flags);
5102 /* In an expression of the form `a->f()' where `f' turns out to
5103 be a static member function, `a' is none-the-less evaluated. */
5104 if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
5105 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
5108 if (processing_template_decl && call != error_mark_node)
5109 return build_min_non_dep
5111 build_min_nt (COMPONENT_REF, orig_instance, orig_fns),
5116 /* Returns true iff standard conversion sequence ICS1 is a proper
5117 subsequence of ICS2. */
5120 is_subseq (tree ics1, tree ics2)
5122 /* We can assume that a conversion of the same code
5123 between the same types indicates a subsequence since we only get
5124 here if the types we are converting from are the same. */
5126 while (TREE_CODE (ics1) == RVALUE_CONV
5127 || TREE_CODE (ics1) == LVALUE_CONV)
5128 ics1 = TREE_OPERAND (ics1, 0);
5132 while (TREE_CODE (ics2) == RVALUE_CONV
5133 || TREE_CODE (ics2) == LVALUE_CONV)
5134 ics2 = TREE_OPERAND (ics2, 0);
5136 if (TREE_CODE (ics2) == USER_CONV
5137 || TREE_CODE (ics2) == AMBIG_CONV
5138 || TREE_CODE (ics2) == IDENTITY_CONV)
5139 /* At this point, ICS1 cannot be a proper subsequence of
5140 ICS2. We can get a USER_CONV when we are comparing the
5141 second standard conversion sequence of two user conversion
5145 ics2 = TREE_OPERAND (ics2, 0);
5147 if (TREE_CODE (ics2) == TREE_CODE (ics1)
5148 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
5149 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5150 TREE_TYPE (TREE_OPERAND (ics1, 0))))
5155 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
5156 be any _TYPE nodes. */
5159 is_properly_derived_from (tree derived, tree base)
5161 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5162 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5165 /* We only allow proper derivation here. The DERIVED_FROM_P macro
5166 considers every class derived from itself. */
5167 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
5168 && DERIVED_FROM_P (base, derived));
5171 /* We build the ICS for an implicit object parameter as a pointer
5172 conversion sequence. However, such a sequence should be compared
5173 as if it were a reference conversion sequence. If ICS is the
5174 implicit conversion sequence for an implicit object parameter,
5175 modify it accordingly. */
5178 maybe_handle_implicit_object (tree *ics)
5180 if (ICS_THIS_FLAG (*ics))
5182 /* [over.match.funcs]
5184 For non-static member functions, the type of the
5185 implicit object parameter is "reference to cv X"
5186 where X is the class of which the function is a
5187 member and cv is the cv-qualification on the member
5188 function declaration. */
5190 tree reference_type;
5192 /* The `this' parameter is a pointer to a class type. Make the
5193 implicit conversion talk about a reference to that same class
5195 reference_type = TREE_TYPE (TREE_TYPE (*ics));
5196 reference_type = build_reference_type (reference_type);
5198 if (TREE_CODE (t) == QUAL_CONV)
5199 t = TREE_OPERAND (t, 0);
5200 if (TREE_CODE (t) == PTR_CONV)
5201 t = TREE_OPERAND (t, 0);
5202 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5203 t = direct_reference_binding (reference_type, t);
5208 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5209 and return the type to which the reference refers. Otherwise,
5210 leave *ICS unchanged and return NULL_TREE. */
5213 maybe_handle_ref_bind (tree *ics)
5215 if (TREE_CODE (*ics) == REF_BIND)
5217 tree old_ics = *ics;
5218 tree type = TREE_TYPE (TREE_TYPE (old_ics));
5219 *ics = TREE_OPERAND (old_ics, 0);
5220 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5221 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5228 /* Compare two implicit conversion sequences according to the rules set out in
5229 [over.ics.rank]. Return values:
5231 1: ics1 is better than ics2
5232 -1: ics2 is better than ics1
5233 0: ics1 and ics2 are indistinguishable */
5236 compare_ics (tree ics1, tree ics2)
5242 tree deref_from_type1 = NULL_TREE;
5243 tree deref_from_type2 = NULL_TREE;
5244 tree deref_to_type1 = NULL_TREE;
5245 tree deref_to_type2 = NULL_TREE;
5248 /* REF_BINDING is nonzero if the result of the conversion sequence
5249 is a reference type. In that case TARGET_TYPE is the
5250 type referred to by the reference. */
5254 /* Handle implicit object parameters. */
5255 maybe_handle_implicit_object (&ics1);
5256 maybe_handle_implicit_object (&ics2);
5258 /* Handle reference parameters. */
5259 target_type1 = maybe_handle_ref_bind (&ics1);
5260 target_type2 = maybe_handle_ref_bind (&ics2);
5264 When comparing the basic forms of implicit conversion sequences (as
5265 defined in _over.best.ics_)
5267 --a standard conversion sequence (_over.ics.scs_) is a better
5268 conversion sequence than a user-defined conversion sequence
5269 or an ellipsis conversion sequence, and
5271 --a user-defined conversion sequence (_over.ics.user_) is a
5272 better conversion sequence than an ellipsis conversion sequence
5273 (_over.ics.ellipsis_). */
5274 rank1 = ICS_RANK (ics1);
5275 rank2 = ICS_RANK (ics2);
5279 else if (rank1 < rank2)
5282 if (rank1 == BAD_RANK)
5284 /* XXX Isn't this an extension? */
5285 /* Both ICS are bad. We try to make a decision based on what
5286 would have happened if they'd been good. */
5287 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5288 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5290 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5291 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5294 /* We couldn't make up our minds; try to figure it out below. */
5297 if (ICS_ELLIPSIS_FLAG (ics1))
5298 /* Both conversions are ellipsis conversions. */
5301 /* User-defined conversion sequence U1 is a better conversion sequence
5302 than another user-defined conversion sequence U2 if they contain the
5303 same user-defined conversion operator or constructor and if the sec-
5304 ond standard conversion sequence of U1 is better than the second
5305 standard conversion sequence of U2. */
5307 if (ICS_USER_FLAG (ics1))
5311 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5312 if (TREE_CODE (t1) == AMBIG_CONV)
5314 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5315 if (TREE_CODE (t2) == AMBIG_CONV)
5318 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5321 /* We can just fall through here, after setting up
5322 FROM_TYPE1 and FROM_TYPE2. */
5323 from_type1 = TREE_TYPE (t1);
5324 from_type2 = TREE_TYPE (t2);
5328 /* We're dealing with two standard conversion sequences.
5332 Standard conversion sequence S1 is a better conversion
5333 sequence than standard conversion sequence S2 if
5335 --S1 is a proper subsequence of S2 (comparing the conversion
5336 sequences in the canonical form defined by _over.ics.scs_,
5337 excluding any Lvalue Transformation; the identity
5338 conversion sequence is considered to be a subsequence of
5339 any non-identity conversion sequence */
5342 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5343 from_type1 = TREE_OPERAND (from_type1, 0);
5344 from_type1 = TREE_TYPE (from_type1);
5347 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5348 from_type2 = TREE_OPERAND (from_type2, 0);
5349 from_type2 = TREE_TYPE (from_type2);
5352 if (same_type_p (from_type1, from_type2))
5354 if (is_subseq (ics1, ics2))
5356 if (is_subseq (ics2, ics1))
5359 /* Otherwise, one sequence cannot be a subsequence of the other; they
5360 don't start with the same type. This can happen when comparing the
5361 second standard conversion sequence in two user-defined conversion
5368 --the rank of S1 is better than the rank of S2 (by the rules
5371 Standard conversion sequences are ordered by their ranks: an Exact
5372 Match is a better conversion than a Promotion, which is a better
5373 conversion than a Conversion.
5375 Two conversion sequences with the same rank are indistinguishable
5376 unless one of the following rules applies:
5378 --A conversion that is not a conversion of a pointer, or pointer
5379 to member, to bool is better than another conversion that is such
5382 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5383 so that we do not have to check it explicitly. */
5384 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5386 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5389 to_type1 = TREE_TYPE (ics1);
5390 to_type2 = TREE_TYPE (ics2);
5392 if (TYPE_PTR_P (from_type1)
5393 && TYPE_PTR_P (from_type2)
5394 && TYPE_PTR_P (to_type1)
5395 && TYPE_PTR_P (to_type2))
5397 deref_from_type1 = TREE_TYPE (from_type1);
5398 deref_from_type2 = TREE_TYPE (from_type2);
5399 deref_to_type1 = TREE_TYPE (to_type1);
5400 deref_to_type2 = TREE_TYPE (to_type2);
5402 /* The rules for pointers to members A::* are just like the rules
5403 for pointers A*, except opposite: if B is derived from A then
5404 A::* converts to B::*, not vice versa. For that reason, we
5405 switch the from_ and to_ variables here. */
5406 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5407 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5408 || (TYPE_PTRMEMFUNC_P (from_type1)
5409 && TYPE_PTRMEMFUNC_P (from_type2)
5410 && TYPE_PTRMEMFUNC_P (to_type1)
5411 && TYPE_PTRMEMFUNC_P (to_type2)))
5413 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5414 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5415 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5416 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
5419 if (deref_from_type1 != NULL_TREE
5420 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5421 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5423 /* This was one of the pointer or pointer-like conversions.
5427 --If class B is derived directly or indirectly from class A,
5428 conversion of B* to A* is better than conversion of B* to
5429 void*, and conversion of A* to void* is better than
5430 conversion of B* to void*. */
5431 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5432 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5434 if (is_properly_derived_from (deref_from_type1,
5437 else if (is_properly_derived_from (deref_from_type2,
5441 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5442 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5444 if (same_type_p (deref_from_type1, deref_from_type2))
5446 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5448 if (is_properly_derived_from (deref_from_type1,
5452 /* We know that DEREF_TO_TYPE1 is `void' here. */
5453 else if (is_properly_derived_from (deref_from_type1,
5458 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5459 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5463 --If class B is derived directly or indirectly from class A
5464 and class C is derived directly or indirectly from B,
5466 --conversion of C* to B* is better than conversion of C* to
5469 --conversion of B* to A* is better than conversion of C* to
5471 if (same_type_p (deref_from_type1, deref_from_type2))
5473 if (is_properly_derived_from (deref_to_type1,
5476 else if (is_properly_derived_from (deref_to_type2,
5480 else if (same_type_p (deref_to_type1, deref_to_type2))
5482 if (is_properly_derived_from (deref_from_type2,
5485 else if (is_properly_derived_from (deref_from_type1,
5491 else if (CLASS_TYPE_P (non_reference (from_type1))
5492 && same_type_p (from_type1, from_type2))
5494 tree from = non_reference (from_type1);
5498 --binding of an expression of type C to a reference of type
5499 B& is better than binding an expression of type C to a
5500 reference of type A&
5502 --conversion of C to B is better than conversion of C to A, */
5503 if (is_properly_derived_from (from, to_type1)
5504 && is_properly_derived_from (from, to_type2))
5506 if (is_properly_derived_from (to_type1, to_type2))
5508 else if (is_properly_derived_from (to_type2, to_type1))
5512 else if (CLASS_TYPE_P (non_reference (to_type1))
5513 && same_type_p (to_type1, to_type2))
5515 tree to = non_reference (to_type1);
5519 --binding of an expression of type B to a reference of type
5520 A& is better than binding an expression of type C to a
5521 reference of type A&,
5523 --onversion of B to A is better than conversion of C to A */
5524 if (is_properly_derived_from (from_type1, to)
5525 && is_properly_derived_from (from_type2, to))
5527 if (is_properly_derived_from (from_type2, from_type1))
5529 else if (is_properly_derived_from (from_type1, from_type2))
5536 --S1 and S2 differ only in their qualification conversion and yield
5537 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5538 qualification signature of type T1 is a proper subset of the cv-
5539 qualification signature of type T2 */
5540 if (TREE_CODE (ics1) == QUAL_CONV
5541 && TREE_CODE (ics2) == QUAL_CONV
5542 && same_type_p (from_type1, from_type2))
5543 return comp_cv_qual_signature (to_type1, to_type2);
5547 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5548 types to which the references refer are the same type except for
5549 top-level cv-qualifiers, and the type to which the reference
5550 initialized by S2 refers is more cv-qualified than the type to
5551 which the reference initialized by S1 refers */
5553 if (target_type1 && target_type2
5554 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5555 return comp_cv_qualification (target_type2, target_type1);
5557 /* Neither conversion sequence is better than the other. */
5561 /* The source type for this standard conversion sequence. */
5564 source_type (tree t)
5566 for (;; t = TREE_OPERAND (t, 0))
5568 if (TREE_CODE (t) == USER_CONV
5569 || TREE_CODE (t) == AMBIG_CONV
5570 || TREE_CODE (t) == IDENTITY_CONV)
5571 return TREE_TYPE (t);
5576 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5577 a pointer to LOSER and re-running joust to produce the warning if WINNER
5578 is actually used. */
5581 add_warning (struct z_candidate *winner, struct z_candidate *loser)
5583 winner->warnings = tree_cons (NULL_TREE,
5584 build_zc_wrapper (loser),
5588 /* Compare two candidates for overloading as described in
5589 [over.match.best]. Return values:
5591 1: cand1 is better than cand2
5592 -1: cand2 is better than cand1
5593 0: cand1 and cand2 are indistinguishable */
5596 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5599 int i, off1 = 0, off2 = 0, len;
5601 /* Candidates that involve bad conversions are always worse than those
5603 if (cand1->viable > cand2->viable)
5605 if (cand1->viable < cand2->viable)
5608 /* If we have two pseudo-candidates for conversions to the same type,
5609 or two candidates for the same function, arbitrarily pick one. */
5610 if (cand1->fn == cand2->fn
5611 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5614 /* a viable function F1
5615 is defined to be a better function than another viable function F2 if
5616 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5617 ICSi(F2), and then */
5619 /* for some argument j, ICSj(F1) is a better conversion sequence than
5622 /* For comparing static and non-static member functions, we ignore
5623 the implicit object parameter of the non-static function. The
5624 standard says to pretend that the static function has an object
5625 parm, but that won't work with operator overloading. */
5626 len = TREE_VEC_LENGTH (cand1->convs);
5627 if (len != TREE_VEC_LENGTH (cand2->convs))
5629 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5630 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5632 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5633 && DECL_STATIC_FUNCTION_P (cand2->fn))
5642 for (i = 0; i < len; ++i)
5644 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5645 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5646 int comp = compare_ics (t1, t2);
5651 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5652 && TREE_CODE (t1) == STD_CONV
5653 && TREE_CODE (t2) == STD_CONV
5654 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5655 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5656 && (TYPE_PRECISION (TREE_TYPE (t1))
5657 == TYPE_PRECISION (TREE_TYPE (t2)))
5658 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5659 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5662 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5664 struct z_candidate *w, *l;
5666 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5667 w = cand1, l = cand2;
5669 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5670 w = cand2, l = cand1;
5674 warning ("passing `%T' chooses `%T' over `%T'",
5675 type, type1, type2);
5676 warning (" in call to `%D'", w->fn);
5682 if (winner && comp != winner)
5691 /* warn about confusing overload resolution for user-defined conversions,
5692 either between a constructor and a conversion op, or between two
5694 if (winner && warn_conversion && cand1->second_conv
5695 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
5696 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
5698 struct z_candidate *w, *l;
5699 bool give_warning = false;
5702 w = cand1, l = cand2;
5704 w = cand2, l = cand1;
5706 /* We don't want to complain about `X::operator T1 ()'
5707 beating `X::operator T2 () const', when T2 is a no less
5708 cv-qualified version of T1. */
5709 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
5710 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
5712 tree t = TREE_TYPE (TREE_TYPE (l->fn));
5713 tree f = TREE_TYPE (TREE_TYPE (w->fn));
5715 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
5720 if (!comp_ptr_ttypes (t, f))
5721 give_warning = true;
5724 give_warning = true;
5730 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5731 if (! DECL_CONSTRUCTOR_P (w->fn))
5732 source = TREE_TYPE (source);
5733 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5734 warning (" for conversion from `%T' to `%T'",
5735 source, TREE_TYPE (w->second_conv));
5736 warning (" because conversion sequence for the argument is better");
5746 F1 is a non-template function and F2 is a template function
5749 if (! cand1->template && cand2->template)
5751 else if (cand1->template && ! cand2->template)
5755 F1 and F2 are template functions and the function template for F1 is
5756 more specialized than the template for F2 according to the partial
5759 if (cand1->template && cand2->template)
5761 winner = more_specialized
5762 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5764 /* Tell the deduction code how many real function arguments
5765 we saw, not counting the implicit 'this' argument. But,
5766 add_function_candidate() suppresses the "this" argument
5769 [temp.func.order]: The presence of unused ellipsis and default
5770 arguments has no effect on the partial ordering of function
5772 TREE_VEC_LENGTH (cand1->convs)
5773 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5774 - DECL_CONSTRUCTOR_P (cand1->fn)));
5780 the context is an initialization by user-defined conversion (see
5781 _dcl.init_ and _over.match.user_) and the standard conversion
5782 sequence from the return type of F1 to the destination type (i.e.,
5783 the type of the entity being initialized) is a better conversion
5784 sequence than the standard conversion sequence from the return type
5785 of F2 to the destination type. */
5787 if (cand1->second_conv)
5789 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5794 /* Check whether we can discard a builtin candidate, either because we
5795 have two identical ones or matching builtin and non-builtin candidates.
5797 (Pedantically in the latter case the builtin which matched the user
5798 function should not be added to the overload set, but we spot it here.
5801 ... the builtin candidates include ...
5802 - do not have the same parameter type list as any non-template
5803 non-member candidate. */
5805 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5806 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5808 for (i = 0; i < len; ++i)
5809 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5810 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5812 if (i == TREE_VEC_LENGTH (cand1->convs))
5814 if (cand1->fn == cand2->fn)
5815 /* Two built-in candidates; arbitrarily pick one. */
5817 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5818 /* cand1 is built-in; prefer cand2. */
5821 /* cand2 is built-in; prefer cand1. */
5826 /* If the two functions are the same (this can happen with declarations
5827 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5828 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5829 && equal_functions (cand1->fn, cand2->fn))
5834 /* Extension: If the worst conversion for one candidate is worse than the
5835 worst conversion for the other, take the first. */
5838 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5839 struct z_candidate *w = 0, *l = 0;
5841 for (i = 0; i < len; ++i)
5843 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5844 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5845 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5846 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5849 winner = 1, w = cand1, l = cand2;
5851 winner = -1, w = cand2, l = cand1;
5857 ISO C++ says that these are ambiguous, even \
5858 though the worst conversion for the first is better than \
5859 the worst conversion for the second:");
5860 print_z_candidate (_("candidate 1:"), w);
5861 print_z_candidate (_("candidate 2:"), l);
5869 my_friendly_assert (!winner, 20010121);
5873 /* Given a list of candidates for overloading, find the best one, if any.
5874 This algorithm has a worst case of O(2n) (winner is last), and a best
5875 case of O(n/2) (totally ambiguous); much better than a sorting
5878 static struct z_candidate *
5879 tourney (struct z_candidate *candidates)
5881 struct z_candidate *champ = candidates, *challenger;
5883 int champ_compared_to_predecessor = 0;
5885 /* Walk through the list once, comparing each current champ to the next
5886 candidate, knocking out a candidate or two with each comparison. */
5888 for (challenger = champ->next; challenger; )
5890 fate = joust (champ, challenger, 0);
5892 challenger = challenger->next;
5897 champ = challenger->next;
5900 champ_compared_to_predecessor = 0;
5905 champ_compared_to_predecessor = 1;
5908 challenger = champ->next;
5912 /* Make sure the champ is better than all the candidates it hasn't yet
5913 been compared to. */
5915 for (challenger = candidates;
5917 && !(champ_compared_to_predecessor && challenger->next == champ);
5918 challenger = challenger->next)
5920 fate = joust (champ, challenger, 0);
5928 /* Returns nonzero if things of type FROM can be converted to TO. */
5931 can_convert (tree to, tree from)
5933 return can_convert_arg (to, from, NULL_TREE);
5936 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
5939 can_convert_arg (tree to, tree from, tree arg)
5941 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5942 return (t && ! ICS_BAD_FLAG (t));
5945 /* Like can_convert_arg, but allows dubious conversions as well. */
5948 can_convert_arg_bad (tree to, tree from, tree arg)
5950 return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
5953 /* Convert EXPR to TYPE. Return the converted expression.
5955 Note that we allow bad conversions here because by the time we get to
5956 this point we are committed to doing the conversion. If we end up
5957 doing a bad conversion, convert_like will complain. */
5960 perform_implicit_conversion (tree type, tree expr)
5964 if (error_operand_p (expr))
5965 return error_mark_node;
5966 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
5970 error ("could not convert `%E' to `%T'", expr, type);
5971 return error_mark_node;
5974 return convert_like (conv, expr);
5977 /* Convert EXPR to TYPE (as a direct-initialization) if that is
5978 permitted. If the conversion is valid, the converted expression is
5979 returned. Otherwise, NULL_TREE is returned, except in the case
5980 that TYPE is a class type; in that case, an error is issued. */
5983 perform_direct_initialization_if_possible (tree type, tree expr)
5987 if (type == error_mark_node || error_operand_p (expr))
5988 return error_mark_node;
5991 If the destination type is a (possibly cv-qualified) class type:
5993 -- If the initialization is direct-initialization ...,
5994 constructors are considered. ... If no constructor applies, or
5995 the overload resolution is ambiguous, the initialization is
5997 if (CLASS_TYPE_P (type))
5999 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6000 build_tree_list (NULL_TREE, expr),
6003 return build_cplus_new (type, expr);
6005 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6007 if (!conv || ICS_BAD_FLAG (conv))
6009 return convert_like_real (conv, expr, NULL_TREE, 0, 0,
6010 /*issue_conversion_warnings=*/false);
6013 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
6014 is being bound to a temporary. Create and return a new VAR_DECL
6015 with the indicated TYPE; this variable will store the value to
6016 which the reference is bound. */
6019 make_temporary_var_for_ref_to_temp (tree decl, tree type)
6023 /* Create the variable. */
6024 var = build_decl (VAR_DECL, NULL_TREE, type);
6025 DECL_ARTIFICIAL (var) = 1;
6026 TREE_USED (var) = 1;
6028 /* Register the variable. */
6029 if (TREE_STATIC (decl))
6031 /* Namespace-scope or local static; give it a mangled name. */
6034 TREE_STATIC (var) = 1;
6035 name = mangle_ref_init_variable (decl);
6036 DECL_NAME (var) = name;
6037 SET_DECL_ASSEMBLER_NAME (var, name);
6038 var = pushdecl_top_level (var);
6042 /* Create a new cleanup level if necessary. */
6043 maybe_push_cleanup_level (type);
6044 /* Don't push unnamed temps. Do set DECL_CONTEXT, though. */
6045 DECL_CONTEXT (var) = current_function_decl;
6051 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
6052 initializing a variable of that TYPE. If DECL is non-NULL, it is
6053 the VAR_DECL being initialized with the EXPR. (In that case, the
6054 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
6055 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
6056 return, if *CLEANUP is no longer NULL, it will be a CLEANUP_STMT
6057 that should be inserted after the returned expression is used to
6060 Return the converted expression. */
6063 initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
6067 if (type == error_mark_node || error_operand_p (expr))
6068 return error_mark_node;
6070 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
6071 if (!conv || ICS_BAD_FLAG (conv))
6073 if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6074 && !real_lvalue_p (expr))
6075 error ("invalid initialization of non-const reference of "
6076 "type '%T' from a temporary of type '%T'",
6077 type, TREE_TYPE (expr));
6079 error ("invalid initialization of reference of type "
6080 "'%T' from expression of type '%T'", type,
6082 return error_mark_node;
6085 /* If DECL is non-NULL, then this special rule applies:
6089 The temporary to which the reference is bound or the temporary
6090 that is the complete object to which the reference is bound
6091 persists for the lifetime of the reference.
6093 The temporaries created during the evaluation of the expression
6094 initializing the reference, except the temporary to which the
6095 reference is bound, are destroyed at the end of the
6096 full-expression in which they are created.
6098 In that case, we store the converted expression into a new
6099 VAR_DECL in a new scope.
6101 However, we want to be careful not to create temporaries when
6102 they are not required. For example, given:
6105 struct D : public B {};
6109 there is no need to copy the return value from "f"; we can just
6110 extend its lifetime. Similarly, given:
6113 struct T { operator S(); };
6117 we can extend the lifetime of the return value of the conversion
6119 my_friendly_assert (TREE_CODE (conv) == REF_BIND, 20030302);
6123 tree base_conv_type;
6125 /* Skip over the REF_BIND. */
6126 conv = TREE_OPERAND (conv, 0);
6127 /* If the next conversion is a BASE_CONV, skip that too -- but
6128 remember that the conversion was required. */
6129 if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv))
6131 void (*diagnostic_fn) (const char *, ...);
6132 if (CHECK_COPY_CONSTRUCTOR_P (conv))
6133 /* Generate a temporary copy purely to generate the required
6135 build_temp (build_dummy_object (TREE_TYPE (expr)),
6137 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6139 base_conv_type = TREE_TYPE (conv);
6140 conv = TREE_OPERAND (conv, 0);
6143 base_conv_type = NULL_TREE;
6144 /* Perform the remainder of the conversion. */
6145 expr = convert_like_real (conv, expr,
6146 /*fn=*/NULL_TREE, /*argnum=*/0,
6148 /*issue_conversion_warnings=*/true);
6149 if (!real_lvalue_p (expr))
6154 /* Create the temporary variable. */
6155 type = TREE_TYPE (expr);
6156 var = make_temporary_var_for_ref_to_temp (decl, type);
6157 layout_decl (var, 0);
6158 /* Create the INIT_EXPR that will initialize the temporary
6160 init = build (INIT_EXPR, type, var, expr);
6161 if (at_function_scope_p ())
6163 add_decl_stmt (var);
6164 *cleanup = cxx_maybe_build_cleanup (var);
6166 /* We must be careful to destroy the temporary only
6167 after its initialization has taken place. If the
6168 initialization throws an exception, then the
6169 destructor should not be run. We cannot simply
6170 transform INIT into something like:
6172 (INIT, ({ CLEANUP_STMT; }))
6174 because emit_local_var always treats the
6175 initializer as a full-expression. Thus, the
6176 destructor would run too early; it would run at the
6177 end of initializing the reference variable, rather
6178 than at the end of the block enclosing the
6181 The solution is to pass back a CLEANUP_STMT which
6182 the caller is responsible for attaching to the
6184 *cleanup = build_stmt (CLEANUP_STMT, var, *cleanup);
6188 rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof);
6189 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6190 static_aggregates = tree_cons (NULL_TREE, var,
6193 /* Use its address to initialize the reference variable. */
6194 expr = build_address (var);
6195 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6198 /* Take the address of EXPR. */
6199 expr = build_unary_op (ADDR_EXPR, expr, 0);
6200 /* If a BASE_CONV was required, perform it now. */
6202 expr = (perform_implicit_conversion
6203 (build_pointer_type (base_conv_type), expr));
6204 return build_nop (type, expr);
6207 /* Perform the conversion. */
6208 return convert_like (conv, expr);
6211 #include "gt-cp-call.h"