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, 2005 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, int);
90 static tree reference_binding (tree, tree, tree, int);
91 static tree build_conv (enum tree_code, tree, tree);
92 static bool is_subseq (tree, tree);
93 static tree maybe_handle_ref_bind (tree *);
94 static void maybe_handle_implicit_object (tree *);
95 static struct z_candidate *add_candidate
96 (struct z_candidate **, tree, tree, tree, tree, tree, int);
97 static tree source_type (tree);
98 static void add_warning (struct z_candidate *, struct z_candidate *);
99 static bool reference_related_p (tree, tree);
100 static bool reference_compatible_p (tree, tree);
101 static tree convert_class_to_reference (tree, tree, tree);
102 static tree direct_reference_binding (tree, tree);
103 static bool promoted_arithmetic_type_p (tree);
104 static tree conditional_conversion (tree, tree);
105 static char *name_as_c_string (tree, tree, bool *);
106 static tree call_builtin_trap (void);
107 static tree prep_operand (tree);
108 static void add_candidates (tree, tree, tree, bool, tree, tree,
109 int, struct z_candidate **);
110 static tree merge_conversion_sequences (tree, tree);
111 static bool magic_varargs_p (tree);
112 static tree build_temp (tree, tree, int, void (**)(const char *, ...));
113 static void check_constructor_callable (tree, tree);
116 build_vfield_ref (tree datum, tree type)
118 if (datum == error_mark_node)
119 return error_mark_node;
121 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
122 datum = convert_from_reference (datum);
124 if (TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
125 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
126 datum = convert_to_base (datum, type, /*check_access=*/false);
128 return build (COMPONENT_REF, TREE_TYPE (TYPE_VFIELD (type)),
129 datum, TYPE_VFIELD (type));
132 /* Returns nonzero iff the destructor name specified in NAME
133 (a BIT_NOT_EXPR) matches BASETYPE. The operand of NAME can take many
137 check_dtor_name (tree basetype, tree name)
139 name = TREE_OPERAND (name, 0);
141 /* Just accept something we've already complained about. */
142 if (name == error_mark_node)
145 if (TREE_CODE (name) == TYPE_DECL)
146 name = TREE_TYPE (name);
147 else if (TYPE_P (name))
149 else if (TREE_CODE (name) == IDENTIFIER_NODE)
151 if ((IS_AGGR_TYPE (basetype) && name == constructor_name (basetype))
152 || (TREE_CODE (basetype) == ENUMERAL_TYPE
153 && name == TYPE_IDENTIFIER (basetype)))
156 name = get_type_value (name);
160 template <class T> struct S { ~S(); };
164 NAME will be a class template. */
165 else if (DECL_CLASS_TEMPLATE_P (name))
170 if (name && TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (name))
175 /* We want the address of a function or method. We avoid creating a
176 pointer-to-member function. */
179 build_addr_func (tree function)
181 tree type = TREE_TYPE (function);
183 /* We have to do these by hand to avoid real pointer to member
185 if (TREE_CODE (type) == METHOD_TYPE)
187 if (TREE_CODE (function) == OFFSET_REF)
189 tree object = build_address (TREE_OPERAND (function, 0));
190 return get_member_function_from_ptrfunc (&object,
191 TREE_OPERAND (function, 1));
193 function = build_address (function);
196 function = decay_conversion (function);
201 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
202 POINTER_TYPE to those. Note, pointer to member function types
203 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
206 build_call (tree function, tree parms)
208 int is_constructor = 0;
215 function = build_addr_func (function);
217 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
219 sorry ("unable to call pointer to member function here");
220 return error_mark_node;
223 fntype = TREE_TYPE (TREE_TYPE (function));
224 result_type = TREE_TYPE (fntype);
226 if (TREE_CODE (function) == ADDR_EXPR
227 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
228 decl = TREE_OPERAND (function, 0);
232 /* We check both the decl and the type; a function may be known not to
233 throw without being declared throw(). */
234 nothrow = ((decl && TREE_NOTHROW (decl))
235 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
237 if (decl && TREE_THIS_VOLATILE (decl) && cfun)
238 current_function_returns_abnormally = 1;
240 if (decl && TREE_DEPRECATED (decl))
241 warn_deprecated_use (decl);
242 require_complete_eh_spec_types (fntype, decl);
244 if (decl && DECL_CONSTRUCTOR_P (decl))
247 if (decl && ! TREE_USED (decl))
249 /* We invoke build_call directly for several library functions.
250 These may have been declared normally if we're building libgcc,
251 so we can't just check DECL_ARTIFICIAL. */
252 if (DECL_ARTIFICIAL (decl)
253 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "__", 2))
259 /* Don't pass empty class objects by value. This is useful
260 for tags in STL, which are used to control overload resolution.
261 We don't need to handle other cases of copying empty classes. */
262 if (! decl || ! DECL_BUILT_IN (decl))
263 for (tmp = parms; tmp; tmp = TREE_CHAIN (tmp))
264 if (is_empty_class (TREE_TYPE (TREE_VALUE (tmp)))
265 && ! TREE_ADDRESSABLE (TREE_TYPE (TREE_VALUE (tmp))))
267 tree t = build (EMPTY_CLASS_EXPR, TREE_TYPE (TREE_VALUE (tmp)));
268 TREE_VALUE (tmp) = build (COMPOUND_EXPR, TREE_TYPE (t),
269 TREE_VALUE (tmp), t);
272 function = build (CALL_EXPR, result_type, function, parms);
273 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
274 TREE_NOTHROW (function) = nothrow;
279 /* Build something of the form ptr->method (args)
280 or object.method (args). This can also build
281 calls to constructors, and find friends.
283 Member functions always take their class variable
286 INSTANCE is a class instance.
288 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
290 PARMS help to figure out what that NAME really refers to.
292 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
293 down to the real instance type to use for access checking. We need this
294 information to get protected accesses correct.
296 FLAGS is the logical disjunction of zero or more LOOKUP_
297 flags. See cp-tree.h for more info.
299 If this is all OK, calls build_function_call with the resolved
302 This function must also handle being called to perform
303 initialization, promotion/coercion of arguments, and
304 instantiation of default parameters.
306 Note that NAME may refer to an instance variable name. If
307 `operator()()' is defined for the type of that field, then we return
310 /* New overloading code. */
312 struct z_candidate GTY(()) {
313 /* The FUNCTION_DECL that will be called if this candidate is
314 selected by overload resolution. */
316 /* The arguments to use when calling this function. */
318 /* The implicit conversion sequences for each of the arguments to
321 /* If FN is a user-defined conversion, the standard conversion
322 sequence from the type returned by FN to the desired destination
326 /* If FN is a member function, the binfo indicating the path used to
327 qualify the name of FN at the call site. This path is used to
328 determine whether or not FN is accessible if it is selected by
329 overload resolution. The DECL_CONTEXT of FN will always be a
330 (possibly improper) base of this binfo. */
332 /* If FN is a non-static member function, the binfo indicating the
333 subobject to which the `this' pointer should be converted if FN
334 is selected by overload resolution. The type pointed to the by
335 the `this' pointer must correspond to the most derived class
336 indicated by the CONVERSION_PATH. */
337 tree conversion_path;
340 struct z_candidate *next;
343 #define IDENTITY_RANK 0
349 #define ELLIPSIS_RANK 6
352 #define ICS_RANK(NODE) \
353 (ICS_BAD_FLAG (NODE) ? BAD_RANK \
354 : ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
355 : ICS_USER_FLAG (NODE) ? USER_RANK \
356 : ICS_STD_RANK (NODE))
358 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
360 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
361 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
362 #define ICS_THIS_FLAG(NODE) TREE_LANG_FLAG_2 (NODE)
363 #define ICS_BAD_FLAG(NODE) TREE_LANG_FLAG_3 (NODE)
365 /* In a REF_BIND or a BASE_CONV, this indicates that a temporary
366 should be created to hold the result of the conversion. */
367 #define NEED_TEMPORARY_P(NODE) TREE_LANG_FLAG_4 (NODE)
369 /* TRUE in an IDENTITY_CONV or BASE_CONV if the copy constructor must
370 be accessible, even though it is not being used. */
371 #define CHECK_COPY_CONSTRUCTOR_P(NODE) TREE_LANG_FLAG_5 (NODE)
373 #define USER_CONV_CAND(NODE) WRAPPER_ZC (TREE_OPERAND (NODE, 1))
374 #define USER_CONV_FN(NODE) (USER_CONV_CAND (NODE)->fn)
376 /* Returns true iff T is a null pointer constant in the sense of
380 null_ptr_cst_p (tree t)
384 A null pointer constant is an integral constant expression
385 (_expr.const_) rvalue of integer type that evaluates to zero. */
386 if (DECL_INTEGRAL_CONSTANT_VAR_P (t))
387 t = decl_constant_value (t);
389 || (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t)))
395 /* Returns nonzero if PARMLIST consists of only default parms and/or
399 sufficient_parms_p (tree parmlist)
401 for (; parmlist && parmlist != void_list_node;
402 parmlist = TREE_CHAIN (parmlist))
403 if (!TREE_PURPOSE (parmlist))
409 build_conv (enum tree_code code, tree type, tree from)
412 int rank = ICS_STD_RANK (from);
414 /* We can't use buildl1 here because CODE could be USER_CONV, which
415 takes two arguments. In that case, the caller is responsible for
416 filling in the second argument. */
417 t = make_node (code);
418 TREE_TYPE (t) = type;
419 TREE_OPERAND (t, 0) = from;
432 if (rank < EXACT_RANK)
438 ICS_STD_RANK (t) = rank;
439 ICS_USER_FLAG (t) = (code == USER_CONV || ICS_USER_FLAG (from));
440 ICS_BAD_FLAG (t) = ICS_BAD_FLAG (from);
445 strip_top_quals (tree t)
447 if (TREE_CODE (t) == ARRAY_TYPE)
449 return cp_build_qualified_type (t, 0);
452 /* Returns the standard conversion path (see [conv]) from type FROM to type
453 TO, if any. For proper handling of null pointer constants, you must
454 also pass the expression EXPR to convert from. */
457 standard_conversion (tree to, tree from, tree expr, int flags)
459 enum tree_code fcode, tcode;
461 bool fromref = false;
463 to = non_reference (to);
464 if (TREE_CODE (from) == REFERENCE_TYPE)
467 from = TREE_TYPE (from);
469 to = strip_top_quals (to);
470 from = strip_top_quals (from);
472 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
473 && expr && type_unknown_p (expr))
475 expr = instantiate_type (to, expr, tf_conv);
476 if (expr == error_mark_node)
478 from = TREE_TYPE (expr);
481 fcode = TREE_CODE (from);
482 tcode = TREE_CODE (to);
484 conv = build1 (IDENTITY_CONV, from, expr);
486 if (fcode == FUNCTION_TYPE)
488 from = build_pointer_type (from);
489 fcode = TREE_CODE (from);
490 conv = build_conv (LVALUE_CONV, from, conv);
492 else if (fcode == ARRAY_TYPE)
494 from = build_pointer_type (TREE_TYPE (from));
495 fcode = TREE_CODE (from);
496 conv = build_conv (LVALUE_CONV, from, conv);
498 else if (fromref || (expr && lvalue_p (expr)))
499 conv = build_conv (RVALUE_CONV, from, conv);
501 /* Allow conversion between `__complex__' data types. */
502 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
504 /* The standard conversion sequence to convert FROM to TO is
505 the standard conversion sequence to perform componentwise
507 tree part_conv = standard_conversion
508 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, flags);
512 conv = build_conv (TREE_CODE (part_conv), to, conv);
513 ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
521 if (same_type_p (from, to))
524 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to))
525 && expr && null_ptr_cst_p (expr))
526 conv = build_conv (STD_CONV, to, conv);
527 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE
528 && TREE_CODE (TREE_TYPE (to)) == VECTOR_TYPE
529 && TREE_CODE (TREE_TYPE (from)) == VECTOR_TYPE
530 && ((*targetm.vector_opaque_p) (TREE_TYPE (to))
531 || (*targetm.vector_opaque_p) (TREE_TYPE (from))))
532 conv = build_conv (STD_CONV, to, conv);
533 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
534 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
536 /* For backwards brain damage compatibility, allow interconversion of
537 pointers and integers with a pedwarn. */
538 conv = build_conv (STD_CONV, to, conv);
539 ICS_BAD_FLAG (conv) = 1;
541 else if (tcode == ENUMERAL_TYPE && fcode == INTEGER_TYPE)
543 /* For backwards brain damage compatibility, allow interconversion of
544 enums and integers with a pedwarn. */
545 conv = build_conv (STD_CONV, to, conv);
546 ICS_BAD_FLAG (conv) = 1;
548 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
549 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
554 if (tcode == POINTER_TYPE
555 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
558 else if (VOID_TYPE_P (TREE_TYPE (to))
559 && !TYPE_PTRMEM_P (from)
560 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
562 from = build_pointer_type
563 (cp_build_qualified_type (void_type_node,
564 cp_type_quals (TREE_TYPE (from))));
565 conv = build_conv (PTR_CONV, from, conv);
567 else if (TYPE_PTRMEM_P (from))
569 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
570 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
572 if (DERIVED_FROM_P (fbase, tbase)
573 && (same_type_ignoring_top_level_qualifiers_p
574 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
575 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
577 from = build_ptrmem_type (tbase,
578 TYPE_PTRMEM_POINTED_TO_TYPE (from));
579 conv = build_conv (PMEM_CONV, from, conv);
581 else if (!same_type_p (fbase, tbase))
584 else if (IS_AGGR_TYPE (TREE_TYPE (from))
585 && IS_AGGR_TYPE (TREE_TYPE (to))
588 An rvalue of type "pointer to cv D," where D is a
589 class type, can be converted to an rvalue of type
590 "pointer to cv B," where B is a base class (clause
591 _class.derived_) of D. If B is an inaccessible
592 (clause _class.access_) or ambiguous
593 (_class.member.lookup_) base class of D, a program
594 that necessitates this conversion is ill-formed. */
595 /* Therefore, we use DERIVED_FROM_P, and not
596 ACESSIBLY_UNIQUELY_DERIVED_FROM_P, in this test. */
597 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
600 cp_build_qualified_type (TREE_TYPE (to),
601 cp_type_quals (TREE_TYPE (from)));
602 from = build_pointer_type (from);
603 conv = build_conv (PTR_CONV, from, conv);
606 if (tcode == POINTER_TYPE)
608 to_pointee = TREE_TYPE (to);
609 from_pointee = TREE_TYPE (from);
613 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
614 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
617 if (same_type_p (from, to))
619 else if (comp_ptr_ttypes (to_pointee, from_pointee))
620 conv = build_conv (QUAL_CONV, to, conv);
621 else if (expr && string_conv_p (to, expr, 0))
622 /* converting from string constant to char *. */
623 conv = build_conv (QUAL_CONV, to, conv);
624 else if (ptr_reasonably_similar (to_pointee, from_pointee))
626 conv = build_conv (PTR_CONV, to, conv);
627 ICS_BAD_FLAG (conv) = 1;
634 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
636 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
637 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
638 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
639 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
641 if (!DERIVED_FROM_P (fbase, tbase)
642 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
643 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
644 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
645 || cp_type_quals (fbase) != cp_type_quals (tbase))
648 from = cp_build_qualified_type (tbase, cp_type_quals (fbase));
649 from = build_method_type_directly (from,
651 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
652 from = build_ptrmemfunc_type (build_pointer_type (from));
653 conv = build_conv (PMEM_CONV, from, conv);
655 else if (tcode == BOOLEAN_TYPE)
659 An rvalue of arithmetic, enumeration, pointer, or pointer to
660 member type can be converted to an rvalue of type bool. */
661 if (ARITHMETIC_TYPE_P (from)
662 || fcode == ENUMERAL_TYPE
663 || fcode == POINTER_TYPE
664 || TYPE_PTR_TO_MEMBER_P (from))
666 conv = build_conv (STD_CONV, to, conv);
667 if (fcode == POINTER_TYPE
668 || TYPE_PTRMEM_P (from)
669 || (TYPE_PTRMEMFUNC_P (from)
670 && ICS_STD_RANK (conv) < PBOOL_RANK))
671 ICS_STD_RANK (conv) = PBOOL_RANK;
677 /* We don't check for ENUMERAL_TYPE here because there are no standard
678 conversions to enum type. */
679 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
680 || tcode == REAL_TYPE)
682 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
684 conv = build_conv (STD_CONV, to, conv);
686 /* Give this a better rank if it's a promotion. */
687 if (same_type_p (to, type_promotes_to (from))
688 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
689 ICS_STD_RANK (conv) = PROMO_RANK;
691 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
692 && ((*targetm.vector_opaque_p) (from)
693 || (*targetm.vector_opaque_p) (to)))
694 return build_conv (STD_CONV, to, conv);
695 else if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE)
696 && IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
697 && is_properly_derived_from (from, to))
699 if (TREE_CODE (conv) == RVALUE_CONV)
700 conv = TREE_OPERAND (conv, 0);
701 conv = build_conv (BASE_CONV, to, conv);
702 /* The derived-to-base conversion indicates the initialization
703 of a parameter with base type from an object of a derived
704 type. A temporary object is created to hold the result of
706 NEED_TEMPORARY_P (conv) = 1;
714 /* Returns nonzero if T1 is reference-related to T2. */
717 reference_related_p (tree t1, tree t2)
719 t1 = TYPE_MAIN_VARIANT (t1);
720 t2 = TYPE_MAIN_VARIANT (t2);
724 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
725 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
727 return (same_type_p (t1, t2)
728 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
729 && DERIVED_FROM_P (t1, t2)));
732 /* Returns nonzero if T1 is reference-compatible with T2. */
735 reference_compatible_p (tree t1, tree t2)
739 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
740 reference-related to T2 and cv1 is the same cv-qualification as,
741 or greater cv-qualification than, cv2. */
742 return (reference_related_p (t1, t2)
743 && at_least_as_qualified_p (t1, t2));
746 /* Determine whether or not the EXPR (of class type S) can be
747 converted to T as in [over.match.ref]. */
750 convert_class_to_reference (tree t, tree s, tree expr)
756 struct z_candidate *candidates;
757 struct z_candidate *cand;
760 conversions = lookup_conversions (s);
766 Assuming that "cv1 T" is the underlying type of the reference
767 being initialized, and "cv S" is the type of the initializer
768 expression, with S a class type, the candidate functions are
771 --The conversion functions of S and its base classes are
772 considered. Those that are not hidden within S and yield type
773 "reference to cv2 T2", where "cv1 T" is reference-compatible
774 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
776 The argument list has one argument, which is the initializer
781 /* Conceptually, we should take the address of EXPR and put it in
782 the argument list. Unfortunately, however, that can result in
783 error messages, which we should not issue now because we are just
784 trying to find a conversion operator. Therefore, we use NULL,
785 cast to the appropriate type. */
786 arglist = build_int_2 (0, 0);
787 TREE_TYPE (arglist) = build_pointer_type (s);
788 arglist = build_tree_list (NULL_TREE, arglist);
790 reference_type = build_reference_type (t);
794 tree fns = TREE_VALUE (conversions);
796 for (; fns; fns = OVL_NEXT (fns))
798 tree f = OVL_CURRENT (fns);
799 tree t2 = TREE_TYPE (TREE_TYPE (f));
803 /* If this is a template function, try to get an exact
805 if (TREE_CODE (f) == TEMPLATE_DECL)
807 cand = add_template_candidate (&candidates,
813 TREE_PURPOSE (conversions),
819 /* Now, see if the conversion function really returns
820 an lvalue of the appropriate type. From the
821 point of view of unification, simply returning an
822 rvalue of the right type is good enough. */
824 t2 = TREE_TYPE (TREE_TYPE (f));
825 if (TREE_CODE (t2) != REFERENCE_TYPE
826 || !reference_compatible_p (t, TREE_TYPE (t2)))
828 candidates = candidates->next;
833 else if (TREE_CODE (t2) == REFERENCE_TYPE
834 && reference_compatible_p (t, TREE_TYPE (t2)))
835 cand = add_function_candidate (&candidates, f, s, arglist,
837 TREE_PURPOSE (conversions),
842 /* Build a standard conversion sequence indicating the
843 binding from the reference type returned by the
844 function to the desired REFERENCE_TYPE. */
846 = (direct_reference_binding
848 build1 (IDENTITY_CONV,
849 TREE_TYPE (TREE_TYPE (TREE_TYPE (cand->fn))),
851 ICS_BAD_FLAG (cand->second_conv)
852 |= ICS_BAD_FLAG (TREE_VEC_ELT (cand->convs, 0));
855 conversions = TREE_CHAIN (conversions);
858 candidates = splice_viable (candidates, pedantic, &any_viable_p);
859 /* If none of the conversion functions worked out, let our caller
864 cand = tourney (candidates);
868 /* Now that we know that this is the function we're going to use fix
869 the dummy first argument. */
870 cand->args = tree_cons (NULL_TREE,
872 TREE_CHAIN (cand->args));
874 /* Build a user-defined conversion sequence representing the
876 conv = build_conv (USER_CONV,
877 TREE_TYPE (TREE_TYPE (cand->fn)),
878 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
879 TREE_OPERAND (conv, 1) = build_zc_wrapper (cand);
881 /* Merge it with the standard conversion sequence from the
882 conversion function's return type to the desired type. */
883 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
885 if (cand->viable == -1)
886 ICS_BAD_FLAG (conv) = 1;
888 return cand->second_conv;
891 /* A reference of the indicated TYPE is being bound directly to the
892 expression represented by the implicit conversion sequence CONV.
893 Return a conversion sequence for this binding. */
896 direct_reference_binding (tree type, tree conv)
900 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 20030306);
901 my_friendly_assert (TREE_CODE (TREE_TYPE (conv)) != REFERENCE_TYPE,
904 t = TREE_TYPE (type);
908 When a parameter of reference type binds directly
909 (_dcl.init.ref_) to an argument expression, the implicit
910 conversion sequence is the identity conversion, unless the
911 argument expression has a type that is a derived class of the
912 parameter type, in which case the implicit conversion sequence is
913 a derived-to-base Conversion.
915 If the parameter binds directly to the result of applying a
916 conversion function to the argument expression, the implicit
917 conversion sequence is a user-defined conversion sequence
918 (_over.ics.user_), with the second standard conversion sequence
919 either an identity conversion or, if the conversion function
920 returns an entity of a type that is a derived class of the
921 parameter type, a derived-to-base conversion. */
922 if (!same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (conv)))
924 /* Represent the derived-to-base conversion. */
925 conv = build_conv (BASE_CONV, t, conv);
926 /* We will actually be binding to the base-class subobject in
927 the derived class, so we mark this conversion appropriately.
928 That way, convert_like knows not to generate a temporary. */
929 NEED_TEMPORARY_P (conv) = 0;
931 return build_conv (REF_BIND, type, conv);
934 /* Returns the conversion path from type FROM to reference type TO for
935 purposes of reference binding. For lvalue binding, either pass a
936 reference type to FROM or an lvalue expression to EXPR. If the
937 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
938 the conversion returned. */
941 reference_binding (tree rto, tree rfrom, tree expr, int flags)
943 tree conv = NULL_TREE;
944 tree to = TREE_TYPE (rto);
948 cp_lvalue_kind lvalue_p = clk_none;
950 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
952 expr = instantiate_type (to, expr, tf_none);
953 if (expr == error_mark_node)
955 from = TREE_TYPE (expr);
958 if (TREE_CODE (from) == REFERENCE_TYPE)
960 /* Anything with reference type is an lvalue. */
961 lvalue_p = clk_ordinary;
962 from = TREE_TYPE (from);
965 lvalue_p = real_lvalue_p (expr);
967 /* Figure out whether or not the types are reference-related and
968 reference compatible. We have do do this after stripping
969 references from FROM. */
970 related_p = reference_related_p (to, from);
971 compatible_p = reference_compatible_p (to, from);
973 if (lvalue_p && compatible_p)
977 If the initializer expression
979 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
980 is reference-compatible with "cv2 T2,"
982 the reference is bound directly to the initializer expression
984 conv = build1 (IDENTITY_CONV, from, expr);
985 conv = direct_reference_binding (rto, conv);
986 if ((lvalue_p & clk_bitfield) != 0
987 || ((lvalue_p & clk_packed) != 0 && !TYPE_PACKED (to)))
988 /* For the purposes of overload resolution, we ignore the fact
989 this expression is a bitfield or packed field. (In particular,
990 [over.ics.ref] says specifically that a function with a
991 non-const reference parameter is viable even if the
992 argument is a bitfield.)
994 However, when we actually call the function we must create
995 a temporary to which to bind the reference. If the
996 reference is volatile, or isn't const, then we cannot make
997 a temporary, so we just issue an error when the conversion
999 NEED_TEMPORARY_P (conv) = 1;
1003 else if (CLASS_TYPE_P (from) && !(flags & LOOKUP_NO_CONVERSION))
1007 If the initializer expression
1009 -- has a class type (i.e., T2 is a class type) can be
1010 implicitly converted to an lvalue of type "cv3 T3," where
1011 "cv1 T1" is reference-compatible with "cv3 T3". (this
1012 conversion is selected by enumerating the applicable
1013 conversion functions (_over.match.ref_) and choosing the
1014 best one through overload resolution. (_over.match_).
1016 the reference is bound to the lvalue result of the conversion
1017 in the second case. */
1018 conv = convert_class_to_reference (to, from, expr);
1023 /* From this point on, we conceptually need temporaries, even if we
1024 elide them. Only the cases above are "direct bindings". */
1025 if (flags & LOOKUP_NO_TEMP_BIND)
1030 When a parameter of reference type is not bound directly to an
1031 argument expression, the conversion sequence is the one required
1032 to convert the argument expression to the underlying type of the
1033 reference according to _over.best.ics_. Conceptually, this
1034 conversion sequence corresponds to copy-initializing a temporary
1035 of the underlying type with the argument expression. Any
1036 difference in top-level cv-qualification is subsumed by the
1037 initialization itself and does not constitute a conversion. */
1041 Otherwise, the reference shall be to a non-volatile const type. */
1042 if (!CP_TYPE_CONST_NON_VOLATILE_P (to))
1047 If the initializer expression is an rvalue, with T2 a class type,
1048 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1049 is bound in one of the following ways:
1051 -- The reference is bound to the object represented by the rvalue
1052 or to a sub-object within that object.
1056 We use the first alternative. The implicit conversion sequence
1057 is supposed to be same as we would obtain by generating a
1058 temporary. Fortunately, if the types are reference compatible,
1059 then this is either an identity conversion or the derived-to-base
1060 conversion, just as for direct binding. */
1061 if (CLASS_TYPE_P (from) && compatible_p)
1063 conv = build1 (IDENTITY_CONV, from, expr);
1064 conv = direct_reference_binding (rto, conv);
1065 if (!(flags & LOOKUP_CONSTRUCTOR_CALLABLE))
1066 CHECK_COPY_CONSTRUCTOR_P (TREE_OPERAND (conv, 0)) = 1;
1072 Otherwise, a temporary of type "cv1 T1" is created and
1073 initialized from the initializer expression using the rules for a
1074 non-reference copy initialization. If T1 is reference-related to
1075 T2, cv1 must be the same cv-qualification as, or greater
1076 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1077 if (related_p && !at_least_as_qualified_p (to, from))
1080 conv = implicit_conversion (to, from, expr, flags);
1084 conv = build_conv (REF_BIND, rto, conv);
1085 /* This reference binding, unlike those above, requires the
1086 creation of a temporary. */
1087 NEED_TEMPORARY_P (conv) = 1;
1092 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
1093 to type TO. The optional expression EXPR may affect the conversion.
1094 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
1098 implicit_conversion (tree to, tree from, tree expr, int flags)
1102 if (from == error_mark_node || to == error_mark_node
1103 || expr == error_mark_node)
1106 if (TREE_CODE (to) == REFERENCE_TYPE)
1107 conv = reference_binding (to, from, expr, flags);
1109 conv = standard_conversion (to, from, expr, flags);
1114 if (expr != NULL_TREE
1115 && (IS_AGGR_TYPE (from)
1116 || IS_AGGR_TYPE (to))
1117 && (flags & LOOKUP_NO_CONVERSION) == 0)
1119 struct z_candidate *cand;
1121 cand = build_user_type_conversion_1
1122 (to, expr, LOOKUP_ONLYCONVERTING);
1124 conv = cand->second_conv;
1126 /* We used to try to bind a reference to a temporary here, but that
1127 is now handled by the recursive call to this function at the end
1128 of reference_binding. */
1135 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1138 static struct z_candidate *
1139 add_candidate (struct z_candidate **candidates,
1140 tree fn, tree args, tree convs, tree access_path,
1141 tree conversion_path, int viable)
1143 struct z_candidate *cand = ggc_alloc_cleared (sizeof (struct z_candidate));
1147 cand->convs = convs;
1148 cand->access_path = access_path;
1149 cand->conversion_path = conversion_path;
1150 cand->viable = viable;
1151 cand->next = *candidates;
1157 /* Create an overload candidate for the function or method FN called with
1158 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
1159 to implicit_conversion.
1161 CTYPE, if non-NULL, is the type we want to pretend this function
1162 comes from for purposes of overload resolution. */
1164 static struct z_candidate *
1165 add_function_candidate (struct z_candidate **candidates,
1166 tree fn, tree ctype, tree arglist,
1167 tree access_path, tree conversion_path,
1170 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1173 tree parmnode, argnode;
1177 /* Built-in functions that haven't been declared don't really
1179 if (DECL_ANTICIPATED (fn))
1182 /* The `this', `in_chrg' and VTT arguments to constructors are not
1183 considered in overload resolution. */
1184 if (DECL_CONSTRUCTOR_P (fn))
1186 parmlist = skip_artificial_parms_for (fn, parmlist);
1187 orig_arglist = arglist;
1188 arglist = skip_artificial_parms_for (fn, arglist);
1191 orig_arglist = arglist;
1193 len = list_length (arglist);
1194 convs = make_tree_vec (len);
1196 /* 13.3.2 - Viable functions [over.match.viable]
1197 First, to be a viable function, a candidate function shall have enough
1198 parameters to agree in number with the arguments in the list.
1200 We need to check this first; otherwise, checking the ICSes might cause
1201 us to produce an ill-formed template instantiation. */
1203 parmnode = parmlist;
1204 for (i = 0; i < len; ++i)
1206 if (parmnode == NULL_TREE || parmnode == void_list_node)
1208 parmnode = TREE_CHAIN (parmnode);
1211 if (i < len && parmnode)
1214 /* Make sure there are default args for the rest of the parms. */
1215 else if (!sufficient_parms_p (parmnode))
1221 /* Second, for F to be a viable function, there shall exist for each
1222 argument an implicit conversion sequence that converts that argument
1223 to the corresponding parameter of F. */
1225 parmnode = parmlist;
1228 for (i = 0; i < len; ++i)
1230 tree arg = TREE_VALUE (argnode);
1231 tree argtype = lvalue_type (arg);
1235 if (parmnode == void_list_node)
1238 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1239 && ! DECL_CONSTRUCTOR_P (fn));
1243 tree parmtype = TREE_VALUE (parmnode);
1245 /* The type of the implicit object parameter ('this') for
1246 overload resolution is not always the same as for the
1247 function itself; conversion functions are considered to
1248 be members of the class being converted, and functions
1249 introduced by a using-declaration are considered to be
1250 members of the class that uses them.
1252 Since build_over_call ignores the ICS for the `this'
1253 parameter, we can just change the parm type. */
1254 if (ctype && is_this)
1257 = build_qualified_type (ctype,
1258 TYPE_QUALS (TREE_TYPE (parmtype)));
1259 parmtype = build_pointer_type (parmtype);
1262 t = implicit_conversion (parmtype, argtype, arg, flags);
1266 t = build1 (IDENTITY_CONV, argtype, arg);
1267 ICS_ELLIPSIS_FLAG (t) = 1;
1271 ICS_THIS_FLAG (t) = 1;
1273 TREE_VEC_ELT (convs, i) = t;
1280 if (ICS_BAD_FLAG (t))
1284 parmnode = TREE_CHAIN (parmnode);
1285 argnode = TREE_CHAIN (argnode);
1289 return add_candidate (candidates, fn, orig_arglist, convs, access_path,
1290 conversion_path, viable);
1293 /* Create an overload candidate for the conversion function FN which will
1294 be invoked for expression OBJ, producing a pointer-to-function which
1295 will in turn be called with the argument list ARGLIST, and add it to
1296 CANDIDATES. FLAGS is passed on to implicit_conversion.
1298 Actually, we don't really care about FN; we care about the type it
1299 converts to. There may be multiple conversion functions that will
1300 convert to that type, and we rely on build_user_type_conversion_1 to
1301 choose the best one; so when we create our candidate, we record the type
1302 instead of the function. */
1304 static struct z_candidate *
1305 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1306 tree arglist, tree access_path, tree conversion_path)
1308 tree totype = TREE_TYPE (TREE_TYPE (fn));
1309 int i, len, viable, flags;
1310 tree parmlist, convs, parmnode, argnode;
1312 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1313 parmlist = TREE_TYPE (parmlist);
1314 parmlist = TYPE_ARG_TYPES (parmlist);
1316 len = list_length (arglist) + 1;
1317 convs = make_tree_vec (len);
1318 parmnode = parmlist;
1321 flags = LOOKUP_NORMAL;
1323 /* Don't bother looking up the same type twice. */
1324 if (*candidates && (*candidates)->fn == totype)
1327 for (i = 0; i < len; ++i)
1329 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
1330 tree argtype = lvalue_type (arg);
1334 t = implicit_conversion (totype, argtype, arg, flags);
1335 else if (parmnode == void_list_node)
1338 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
1341 t = build1 (IDENTITY_CONV, argtype, arg);
1342 ICS_ELLIPSIS_FLAG (t) = 1;
1345 TREE_VEC_ELT (convs, i) = t;
1349 if (ICS_BAD_FLAG (t))
1356 parmnode = TREE_CHAIN (parmnode);
1357 argnode = TREE_CHAIN (argnode);
1363 if (!sufficient_parms_p (parmnode))
1366 return add_candidate (candidates, totype, arglist, convs, access_path,
1367 conversion_path, viable);
1371 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1372 tree type1, tree type2, tree *args, tree *argtypes,
1382 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
1384 for (i = 0; i < 2; ++i)
1389 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
1393 /* We need something for printing the candidate. */
1394 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
1396 else if (ICS_BAD_FLAG (t))
1398 TREE_VEC_ELT (convs, i) = t;
1401 /* For COND_EXPR we rearranged the arguments; undo that now. */
1404 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
1405 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
1406 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
1408 TREE_VEC_ELT (convs, 0) = t;
1413 add_candidate (candidates, fnname, /*args=*/NULL_TREE, convs,
1414 /*access_path=*/NULL_TREE,
1415 /*conversion_path=*/NULL_TREE,
1420 is_complete (tree t)
1422 return COMPLETE_TYPE_P (complete_type (t));
1425 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1428 promoted_arithmetic_type_p (tree type)
1432 In this section, the term promoted integral type is used to refer
1433 to those integral types which are preserved by integral promotion
1434 (including e.g. int and long but excluding e.g. char).
1435 Similarly, the term promoted arithmetic type refers to promoted
1436 integral types plus floating types. */
1437 return ((INTEGRAL_TYPE_P (type)
1438 && same_type_p (type_promotes_to (type), type))
1439 || TREE_CODE (type) == REAL_TYPE);
1442 /* Create any builtin operator overload candidates for the operator in
1443 question given the converted operand types TYPE1 and TYPE2. The other
1444 args are passed through from add_builtin_candidates to
1445 build_builtin_candidate.
1447 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1448 If CODE is requires candidates operands of the same type of the kind
1449 of which TYPE1 and TYPE2 are, we add both candidates
1450 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1453 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1454 enum tree_code code2, tree fnname, tree type1,
1455 tree type2, tree *args, tree *argtypes, int flags)
1459 case POSTINCREMENT_EXPR:
1460 case POSTDECREMENT_EXPR:
1461 args[1] = integer_zero_node;
1462 type2 = integer_type_node;
1471 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1472 and VQ is either volatile or empty, there exist candidate operator
1473 functions of the form
1474 VQ T& operator++(VQ T&);
1475 T operator++(VQ T&, int);
1476 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1477 type other than bool, and VQ is either volatile or empty, there exist
1478 candidate operator functions of the form
1479 VQ T& operator--(VQ T&);
1480 T operator--(VQ T&, int);
1481 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1482 complete object type, and VQ is either volatile or empty, there exist
1483 candidate operator functions of the form
1484 T*VQ& operator++(T*VQ&);
1485 T*VQ& operator--(T*VQ&);
1486 T* operator++(T*VQ&, int);
1487 T* operator--(T*VQ&, int); */
1489 case POSTDECREMENT_EXPR:
1490 case PREDECREMENT_EXPR:
1491 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1493 case POSTINCREMENT_EXPR:
1494 case PREINCREMENT_EXPR:
1495 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1497 type1 = build_reference_type (type1);
1502 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1503 exist candidate operator functions of the form
1507 8 For every function type T, there exist candidate operator functions of
1509 T& operator*(T*); */
1512 if (TREE_CODE (type1) == POINTER_TYPE
1513 && (TYPE_PTROB_P (type1)
1514 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
1518 /* 9 For every type T, there exist candidate operator functions of the form
1521 10For every promoted arithmetic type T, there exist candidate operator
1522 functions of the form
1526 case CONVERT_EXPR: /* unary + */
1527 if (TREE_CODE (type1) == POINTER_TYPE)
1530 if (ARITHMETIC_TYPE_P (type1))
1534 /* 11For every promoted integral type T, there exist candidate operator
1535 functions of the form
1539 if (INTEGRAL_TYPE_P (type1))
1543 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
1544 is the same type as C2 or is a derived class of C2, T is a complete
1545 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
1546 there exist candidate operator functions of the form
1547 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
1548 where CV12 is the union of CV1 and CV2. */
1551 if (TREE_CODE (type1) == POINTER_TYPE
1552 && TYPE_PTR_TO_MEMBER_P (type2))
1554 tree c1 = TREE_TYPE (type1);
1555 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
1557 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
1558 && (TYPE_PTRMEMFUNC_P (type2)
1559 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
1564 /* 13For every pair of promoted arithmetic types L and R, there exist can-
1565 didate operator functions of the form
1570 bool operator<(L, R);
1571 bool operator>(L, R);
1572 bool operator<=(L, R);
1573 bool operator>=(L, R);
1574 bool operator==(L, R);
1575 bool operator!=(L, R);
1576 where LR is the result of the usual arithmetic conversions between
1579 14For every pair of types T and I, where T is a cv-qualified or cv-
1580 unqualified complete object type and I is a promoted integral type,
1581 there exist candidate operator functions of the form
1582 T* operator+(T*, I);
1583 T& operator[](T*, I);
1584 T* operator-(T*, I);
1585 T* operator+(I, T*);
1586 T& operator[](I, T*);
1588 15For every T, where T is a pointer to complete object type, there exist
1589 candidate operator functions of the form112)
1590 ptrdiff_t operator-(T, T);
1592 16For every pointer or enumeration type T, there exist candidate operator
1593 functions of the form
1594 bool operator<(T, T);
1595 bool operator>(T, T);
1596 bool operator<=(T, T);
1597 bool operator>=(T, T);
1598 bool operator==(T, T);
1599 bool operator!=(T, T);
1601 17For every pointer to member type T, there exist candidate operator
1602 functions of the form
1603 bool operator==(T, T);
1604 bool operator!=(T, T); */
1607 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
1609 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1611 type2 = ptrdiff_type_node;
1615 case TRUNC_DIV_EXPR:
1616 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1622 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1623 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
1625 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
1630 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
1642 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1644 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1646 if (TREE_CODE (type1) == ENUMERAL_TYPE && TREE_CODE (type2) == ENUMERAL_TYPE)
1648 if (TYPE_PTR_P (type1) && null_ptr_cst_p (args[1]))
1653 if (null_ptr_cst_p (args[0]) && TYPE_PTR_P (type2))
1661 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1664 if (INTEGRAL_TYPE_P (type1) && TYPE_PTROB_P (type2))
1666 type1 = ptrdiff_type_node;
1669 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1671 type2 = ptrdiff_type_node;
1676 /* 18For every pair of promoted integral types L and R, there exist candi-
1677 date operator functions of the form
1684 where LR is the result of the usual arithmetic conversions between
1687 case TRUNC_MOD_EXPR:
1693 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1697 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
1698 type, VQ is either volatile or empty, and R is a promoted arithmetic
1699 type, there exist candidate operator functions of the form
1700 VQ L& operator=(VQ L&, R);
1701 VQ L& operator*=(VQ L&, R);
1702 VQ L& operator/=(VQ L&, R);
1703 VQ L& operator+=(VQ L&, R);
1704 VQ L& operator-=(VQ L&, R);
1706 20For every pair T, VQ), where T is any type and VQ is either volatile
1707 or empty, there exist candidate operator functions of the form
1708 T*VQ& operator=(T*VQ&, T*);
1710 21For every pair T, VQ), where T is a pointer to member type and VQ is
1711 either volatile or empty, there exist candidate operator functions of
1713 VQ T& operator=(VQ T&, T);
1715 22For every triple T, VQ, I), where T is a cv-qualified or cv-
1716 unqualified complete object type, VQ is either volatile or empty, and
1717 I is a promoted integral type, there exist candidate operator func-
1719 T*VQ& operator+=(T*VQ&, I);
1720 T*VQ& operator-=(T*VQ&, I);
1722 23For every triple L, VQ, R), where L is an integral or enumeration
1723 type, VQ is either volatile or empty, and R is a promoted integral
1724 type, there exist candidate operator functions of the form
1726 VQ L& operator%=(VQ L&, R);
1727 VQ L& operator<<=(VQ L&, R);
1728 VQ L& operator>>=(VQ L&, R);
1729 VQ L& operator&=(VQ L&, R);
1730 VQ L& operator^=(VQ L&, R);
1731 VQ L& operator|=(VQ L&, R); */
1738 if (TYPE_PTROB_P (type1) && INTEGRAL_TYPE_P (type2))
1740 type2 = ptrdiff_type_node;
1744 case TRUNC_DIV_EXPR:
1745 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1749 case TRUNC_MOD_EXPR:
1755 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
1760 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
1762 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
1763 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1764 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1765 || ((TYPE_PTRMEMFUNC_P (type1)
1766 || TREE_CODE (type1) == POINTER_TYPE)
1767 && null_ptr_cst_p (args[1])))
1777 type1 = build_reference_type (type1);
1783 For every pair of promoted arithmetic types L and R, there
1784 exist candidate operator functions of the form
1786 LR operator?(bool, L, R);
1788 where LR is the result of the usual arithmetic conversions
1789 between types L and R.
1791 For every type T, where T is a pointer or pointer-to-member
1792 type, there exist candidate operator functions of the form T
1793 operator?(bool, T, T); */
1795 if (promoted_arithmetic_type_p (type1)
1796 && promoted_arithmetic_type_p (type2))
1800 /* Otherwise, the types should be pointers. */
1801 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
1802 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
1805 /* We don't check that the two types are the same; the logic
1806 below will actually create two candidates; one in which both
1807 parameter types are TYPE1, and one in which both parameter
1815 /* If we're dealing with two pointer types or two enumeral types,
1816 we need candidates for both of them. */
1817 if (type2 && !same_type_p (type1, type2)
1818 && TREE_CODE (type1) == TREE_CODE (type2)
1819 && (TREE_CODE (type1) == REFERENCE_TYPE
1820 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1821 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
1822 || TYPE_PTRMEMFUNC_P (type1)
1823 || IS_AGGR_TYPE (type1)
1824 || TREE_CODE (type1) == ENUMERAL_TYPE))
1826 build_builtin_candidate
1827 (candidates, fnname, type1, type1, args, argtypes, flags);
1828 build_builtin_candidate
1829 (candidates, fnname, type2, type2, args, argtypes, flags);
1833 build_builtin_candidate
1834 (candidates, fnname, type1, type2, args, argtypes, flags);
1838 type_decays_to (tree type)
1840 if (TREE_CODE (type) == ARRAY_TYPE)
1841 return build_pointer_type (TREE_TYPE (type));
1842 if (TREE_CODE (type) == FUNCTION_TYPE)
1843 return build_pointer_type (type);
1847 /* There are three conditions of builtin candidates:
1849 1) bool-taking candidates. These are the same regardless of the input.
1850 2) pointer-pair taking candidates. These are generated for each type
1851 one of the input types converts to.
1852 3) arithmetic candidates. According to the standard, we should generate
1853 all of these, but I'm trying not to...
1855 Here we generate a superset of the possible candidates for this particular
1856 case. That is a subset of the full set the standard defines, plus some
1857 other cases which the standard disallows. add_builtin_candidate will
1858 filter out the invalid set. */
1861 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
1862 enum tree_code code2, tree fnname, tree *args,
1867 tree type, argtypes[3];
1868 /* TYPES[i] is the set of possible builtin-operator parameter types
1869 we will consider for the Ith argument. These are represented as
1870 a TREE_LIST; the TREE_VALUE of each node is the potential
1874 for (i = 0; i < 3; ++i)
1877 argtypes[i] = lvalue_type (args[i]);
1879 argtypes[i] = NULL_TREE;
1884 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1885 and VQ is either volatile or empty, there exist candidate operator
1886 functions of the form
1887 VQ T& operator++(VQ T&); */
1889 case POSTINCREMENT_EXPR:
1890 case PREINCREMENT_EXPR:
1891 case POSTDECREMENT_EXPR:
1892 case PREDECREMENT_EXPR:
1897 /* 24There also exist candidate operator functions of the form
1898 bool operator!(bool);
1899 bool operator&&(bool, bool);
1900 bool operator||(bool, bool); */
1902 case TRUTH_NOT_EXPR:
1903 build_builtin_candidate
1904 (candidates, fnname, boolean_type_node,
1905 NULL_TREE, args, argtypes, flags);
1908 case TRUTH_ORIF_EXPR:
1909 case TRUTH_ANDIF_EXPR:
1910 build_builtin_candidate
1911 (candidates, fnname, boolean_type_node,
1912 boolean_type_node, args, argtypes, flags);
1934 types[0] = types[1] = NULL_TREE;
1936 for (i = 0; i < 2; ++i)
1940 else if (IS_AGGR_TYPE (argtypes[i]))
1944 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
1947 convs = lookup_conversions (argtypes[i]);
1949 if (code == COND_EXPR)
1951 if (real_lvalue_p (args[i]))
1952 types[i] = tree_cons
1953 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1955 types[i] = tree_cons
1956 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
1962 for (; convs; convs = TREE_CHAIN (convs))
1964 type = TREE_TYPE (TREE_TYPE (OVL_CURRENT (TREE_VALUE (convs))));
1967 && (TREE_CODE (type) != REFERENCE_TYPE
1968 || CP_TYPE_CONST_P (TREE_TYPE (type))))
1971 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
1972 types[i] = tree_cons (NULL_TREE, type, types[i]);
1974 type = non_reference (type);
1975 if (i != 0 || ! ref1)
1977 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1978 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
1979 types[i] = tree_cons (NULL_TREE, type, types[i]);
1980 if (INTEGRAL_TYPE_P (type))
1981 type = type_promotes_to (type);
1984 if (! value_member (type, types[i]))
1985 types[i] = tree_cons (NULL_TREE, type, types[i]);
1990 if (code == COND_EXPR && real_lvalue_p (args[i]))
1991 types[i] = tree_cons
1992 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
1993 type = non_reference (argtypes[i]);
1994 if (i != 0 || ! ref1)
1996 type = TYPE_MAIN_VARIANT (type_decays_to (type));
1997 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
1998 types[i] = tree_cons (NULL_TREE, type, types[i]);
1999 if (INTEGRAL_TYPE_P (type))
2000 type = type_promotes_to (type);
2002 types[i] = tree_cons (NULL_TREE, type, types[i]);
2006 /* Run through the possible parameter types of both arguments,
2007 creating candidates with those parameter types. */
2008 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2011 for (type = types[1]; type; type = TREE_CHAIN (type))
2012 add_builtin_candidate
2013 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2014 TREE_VALUE (type), args, argtypes, flags);
2016 add_builtin_candidate
2017 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2018 NULL_TREE, args, argtypes, flags);
2025 /* If TMPL can be successfully instantiated as indicated by
2026 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2028 TMPL is the template. EXPLICIT_TARGS are any explicit template
2029 arguments. ARGLIST is the arguments provided at the call-site.
2030 The RETURN_TYPE is the desired type for conversion operators. If
2031 OBJ is NULL_TREE, FLAGS and CTYPE are as for add_function_candidate.
2032 If an OBJ is supplied, FLAGS and CTYPE are ignored, and OBJ is as for
2033 add_conv_candidate. */
2035 static struct z_candidate*
2036 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2037 tree ctype, tree explicit_targs, tree arglist,
2038 tree return_type, tree access_path,
2039 tree conversion_path, int flags, tree obj,
2040 unification_kind_t strict)
2042 int ntparms = DECL_NTPARMS (tmpl);
2043 tree targs = make_tree_vec (ntparms);
2044 tree args_without_in_chrg = arglist;
2045 struct z_candidate *cand;
2049 /* We don't do deduction on the in-charge parameter, the VTT
2050 parameter or 'this'. */
2051 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2052 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2054 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2055 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2056 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (tmpl)))
2057 args_without_in_chrg = TREE_CHAIN (args_without_in_chrg);
2059 i = fn_type_unification (tmpl, explicit_targs, targs,
2060 args_without_in_chrg,
2061 return_type, strict, -1);
2066 fn = instantiate_template (tmpl, targs, tf_none);
2067 if (fn == error_mark_node)
2072 A member function template is never instantiated to perform the
2073 copy of a class object to an object of its class type.
2075 It's a little unclear what this means; the standard explicitly
2076 does allow a template to be used to copy a class. For example,
2081 template <class T> A(const T&);
2084 void g () { A a (f ()); }
2086 the member template will be used to make the copy. The section
2087 quoted above appears in the paragraph that forbids constructors
2088 whose only parameter is (a possibly cv-qualified variant of) the
2089 class type, and a logical interpretation is that the intent was
2090 to forbid the instantiation of member templates which would then
2092 if (DECL_CONSTRUCTOR_P (fn) && list_length (arglist) == 2)
2094 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2095 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2100 if (obj != NULL_TREE)
2101 /* Aha, this is a conversion function. */
2102 cand = add_conv_candidate (candidates, fn, obj, access_path,
2103 conversion_path, arglist);
2105 cand = add_function_candidate (candidates, fn, ctype,
2106 arglist, access_path,
2107 conversion_path, flags);
2108 if (DECL_TI_TEMPLATE (fn) != tmpl)
2109 /* This situation can occur if a member template of a template
2110 class is specialized. Then, instantiate_template might return
2111 an instantiation of the specialization, in which case the
2112 DECL_TI_TEMPLATE field will point at the original
2113 specialization. For example:
2115 template <class T> struct S { template <class U> void f(U);
2116 template <> void f(int) {}; };
2120 Here, TMPL will be template <class U> S<double>::f(U).
2121 And, instantiate template will give us the specialization
2122 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2123 for this will point at template <class T> template <> S<T>::f(int),
2124 so that we can find the definition. For the purposes of
2125 overload resolution, however, we want the original TMPL. */
2126 cand->template = tree_cons (tmpl, targs, NULL_TREE);
2128 cand->template = DECL_TEMPLATE_INFO (fn);
2134 static struct z_candidate *
2135 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2136 tree explicit_targs, tree arglist, tree return_type,
2137 tree access_path, tree conversion_path, int flags,
2138 unification_kind_t strict)
2141 add_template_candidate_real (candidates, tmpl, ctype,
2142 explicit_targs, arglist, return_type,
2143 access_path, conversion_path,
2144 flags, NULL_TREE, strict);
2148 static struct z_candidate *
2149 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2150 tree obj, tree arglist, tree return_type,
2151 tree access_path, tree conversion_path)
2154 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2155 arglist, return_type, access_path,
2156 conversion_path, 0, obj, DEDUCE_CONV);
2159 /* The CANDS are the set of candidates that were considered for
2160 overload resolution. Return the set of viable candidates. If none
2161 of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P
2162 is true if a candidate should be considered viable only if it is
2165 static struct z_candidate*
2166 splice_viable (struct z_candidate *cands,
2170 struct z_candidate *viable;
2171 struct z_candidate **last_viable;
2172 struct z_candidate **cand;
2175 last_viable = &viable;
2176 *any_viable_p = false;
2181 struct z_candidate *c = *cand;
2182 if (strict_p ? c->viable == 1 : c->viable)
2187 last_viable = &c->next;
2188 *any_viable_p = true;
2194 return viable ? viable : cands;
2198 any_strictly_viable (struct z_candidate *cands)
2200 for (; cands; cands = cands->next)
2201 if (cands->viable == 1)
2206 /* OBJ is being used in an expression like "OBJ.f (...)". In other
2207 words, it is about to become the "this" pointer for a member
2208 function call. Take the address of the object. */
2211 build_this (tree obj)
2213 /* In a template, we are only concerned about the type of the
2214 expression, so we can take a shortcut. */
2215 if (processing_template_decl)
2216 return build_address (obj);
2218 return build_unary_op (ADDR_EXPR, obj, 0);
2221 /* Returns true iff functions are equivalent. Equivalent functions are
2222 not '==' only if one is a function-local extern function or if
2223 both are extern "C". */
2226 equal_functions (tree fn1, tree fn2)
2228 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2229 || DECL_EXTERN_C_FUNCTION_P (fn1))
2230 return decls_match (fn1, fn2);
2234 /* Print information about one overload candidate CANDIDATE. MSGSTR
2235 is the text to print before the candidate itself.
2237 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2238 to have been run through gettext by the caller. This wart makes
2239 life simpler in print_z_candidates and for the translators. */
2242 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2244 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2246 if (TREE_VEC_LENGTH (candidate->convs) == 3)
2247 inform ("%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2248 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2249 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)),
2250 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 2)));
2251 else if (TREE_VEC_LENGTH (candidate->convs) == 2)
2252 inform ("%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2253 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)),
2254 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 1)));
2256 inform ("%s %D(%T) <built-in>", msgstr, candidate->fn,
2257 TREE_TYPE (TREE_VEC_ELT (candidate->convs, 0)));
2259 else if (TYPE_P (candidate->fn))
2260 inform ("%s %T <conversion>", msgstr, candidate->fn);
2261 else if (candidate->viable == -1)
2262 inform ("%J%s %+#D <near match>", candidate->fn, msgstr, candidate->fn);
2264 inform ("%J%s %+#D", candidate->fn, msgstr, candidate->fn);
2268 print_z_candidates (struct z_candidate *candidates)
2271 struct z_candidate *cand1;
2272 struct z_candidate **cand2;
2274 /* There may be duplicates in the set of candidates. We put off
2275 checking this condition as long as possible, since we have no way
2276 to eliminate duplicates from a set of functions in less than n^2
2277 time. Now we are about to emit an error message, so it is more
2278 permissible to go slowly. */
2279 for (cand1 = candidates; cand1; cand1 = cand1->next)
2281 tree fn = cand1->fn;
2282 /* Skip builtin candidates and conversion functions. */
2283 if (TREE_CODE (fn) != FUNCTION_DECL)
2285 cand2 = &cand1->next;
2288 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2289 && equal_functions (fn, (*cand2)->fn))
2290 *cand2 = (*cand2)->next;
2292 cand2 = &(*cand2)->next;
2299 str = _("candidates are:");
2300 print_z_candidate (str, candidates);
2301 if (candidates->next)
2303 /* Indent successive candidates by the width of the translation
2304 of the above string. */
2305 size_t len = gcc_gettext_width (str) + 1;
2306 char *spaces = alloca (len);
2307 memset (spaces, ' ', len-1);
2308 spaces[len - 1] = '\0';
2310 candidates = candidates->next;
2313 print_z_candidate (spaces, candidates);
2314 candidates = candidates->next;
2320 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2321 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2322 the result of the conversion function to convert it to the final
2323 desired type. Merge the the two sequences into a single sequence,
2324 and return the merged sequence. */
2327 merge_conversion_sequences (tree user_seq, tree std_seq)
2331 my_friendly_assert (TREE_CODE (user_seq) == USER_CONV,
2334 /* Find the end of the second conversion sequence. */
2336 while (TREE_CODE (*t) != IDENTITY_CONV)
2337 t = &TREE_OPERAND (*t, 0);
2339 /* Replace the identity conversion with the user conversion
2343 /* The entire sequence is a user-conversion sequence. */
2344 ICS_USER_FLAG (std_seq) = 1;
2349 /* Returns the best overload candidate to perform the requested
2350 conversion. This function is used for three the overloading situations
2351 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2352 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2353 per [dcl.init.ref], so we ignore temporary bindings. */
2355 static struct z_candidate *
2356 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2358 struct z_candidate *candidates, *cand;
2359 tree fromtype = TREE_TYPE (expr);
2360 tree ctors = NULL_TREE, convs = NULL_TREE;
2361 tree args = NULL_TREE;
2364 /* We represent conversion within a hierarchy using RVALUE_CONV and
2365 BASE_CONV, as specified by [over.best.ics]; these become plain
2366 constructor calls, as specified in [dcl.init]. */
2367 my_friendly_assert (!IS_AGGR_TYPE (fromtype) || !IS_AGGR_TYPE (totype)
2368 || !DERIVED_FROM_P (totype, fromtype), 20011226);
2370 if (IS_AGGR_TYPE (totype))
2371 ctors = lookup_fnfields (TYPE_BINFO (totype),
2372 complete_ctor_identifier,
2375 if (IS_AGGR_TYPE (fromtype))
2376 convs = lookup_conversions (fromtype);
2379 flags |= LOOKUP_NO_CONVERSION;
2385 ctors = BASELINK_FUNCTIONS (ctors);
2387 t = build_int_2 (0, 0);
2388 TREE_TYPE (t) = build_pointer_type (totype);
2389 args = build_tree_list (NULL_TREE, expr);
2390 /* We should never try to call the abstract or base constructor
2392 my_friendly_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
2393 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)),
2395 args = tree_cons (NULL_TREE, t, args);
2397 for (; ctors; ctors = OVL_NEXT (ctors))
2399 tree ctor = OVL_CURRENT (ctors);
2400 if (DECL_NONCONVERTING_P (ctor))
2403 if (TREE_CODE (ctor) == TEMPLATE_DECL)
2404 cand = add_template_candidate (&candidates, ctor, totype,
2405 NULL_TREE, args, NULL_TREE,
2406 TYPE_BINFO (totype),
2407 TYPE_BINFO (totype),
2411 cand = add_function_candidate (&candidates, ctor, totype,
2412 args, TYPE_BINFO (totype),
2413 TYPE_BINFO (totype),
2417 cand->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
2421 args = build_tree_list (NULL_TREE, build_this (expr));
2423 for (; convs; convs = TREE_CHAIN (convs))
2426 tree conversion_path = TREE_PURPOSE (convs);
2427 int convflags = LOOKUP_NO_CONVERSION;
2429 /* If we are called to convert to a reference type, we are trying to
2430 find an lvalue binding, so don't even consider temporaries. If
2431 we don't find an lvalue binding, the caller will try again to
2432 look for a temporary binding. */
2433 if (TREE_CODE (totype) == REFERENCE_TYPE)
2434 convflags |= LOOKUP_NO_TEMP_BIND;
2436 for (fns = TREE_VALUE (convs); fns; fns = OVL_NEXT (fns))
2438 tree fn = OVL_CURRENT (fns);
2440 /* [over.match.funcs] For conversion functions, the function
2441 is considered to be a member of the class of the implicit
2442 object argument for the purpose of defining the type of
2443 the implicit object parameter.
2445 So we pass fromtype as CTYPE to add_*_candidate. */
2447 if (TREE_CODE (fn) == TEMPLATE_DECL)
2448 cand = add_template_candidate (&candidates, fn, fromtype,
2451 TYPE_BINFO (fromtype),
2456 cand = add_function_candidate (&candidates, fn, fromtype,
2458 TYPE_BINFO (fromtype),
2464 tree ics = implicit_conversion (totype,
2465 TREE_TYPE (TREE_TYPE (cand->fn)),
2468 cand->second_conv = ics;
2470 if (ics == NULL_TREE)
2472 else if (candidates->viable == 1 && ICS_BAD_FLAG (ics))
2478 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2482 cand = tourney (candidates);
2485 if (flags & LOOKUP_COMPLAIN)
2487 error ("conversion from `%T' to `%T' is ambiguous",
2489 print_z_candidates (candidates);
2492 cand = candidates; /* any one will do */
2493 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
2494 ICS_USER_FLAG (cand->second_conv) = 1;
2495 if (!any_strictly_viable (candidates))
2496 ICS_BAD_FLAG (cand->second_conv) = 1;
2497 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
2498 ambiguous conversion is no worse than another user-defined
2504 /* Build the user conversion sequence. */
2507 (DECL_CONSTRUCTOR_P (cand->fn)
2508 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
2509 build1 (IDENTITY_CONV, TREE_TYPE (expr), expr));
2510 TREE_OPERAND (convs, 1) = build_zc_wrapper (cand);
2512 /* Combine it with the second conversion sequence. */
2513 cand->second_conv = merge_conversion_sequences (convs,
2516 if (cand->viable == -1)
2517 ICS_BAD_FLAG (cand->second_conv) = 1;
2523 build_user_type_conversion (tree totype, tree expr, int flags)
2525 struct z_candidate *cand
2526 = build_user_type_conversion_1 (totype, expr, flags);
2530 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
2531 return error_mark_node;
2532 return convert_from_reference (convert_like (cand->second_conv, expr));
2537 /* Do any initial processing on the arguments to a function call. */
2540 resolve_args (tree args)
2543 for (t = args; t; t = TREE_CHAIN (t))
2545 tree arg = TREE_VALUE (t);
2547 if (arg == error_mark_node)
2548 return error_mark_node;
2549 else if (VOID_TYPE_P (TREE_TYPE (arg)))
2551 error ("invalid use of void expression");
2552 return error_mark_node;
2554 arg = convert_from_reference (arg);
2555 TREE_VALUE (t) = arg;
2560 /* Perform overload resolution on FN, which is called with the ARGS.
2562 Return the candidate function selected by overload resolution, or
2563 NULL if the event that overload resolution failed. In the case
2564 that overload resolution fails, *CANDIDATES will be the set of
2565 candidates considered, and ANY_VIABLE_P will be set to true or
2566 false to indicate whether or not any of the candidates were
2569 The ARGS should already have gone through RESOLVE_ARGS before this
2570 function is called. */
2572 static struct z_candidate *
2573 perform_overload_resolution (tree fn,
2575 struct z_candidate **candidates,
2578 struct z_candidate *cand;
2579 tree explicit_targs = NULL_TREE;
2580 int template_only = 0;
2583 *any_viable_p = true;
2585 /* Check FN and ARGS. */
2586 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL
2587 || TREE_CODE (fn) == TEMPLATE_DECL
2588 || TREE_CODE (fn) == OVERLOAD
2589 || TREE_CODE (fn) == TEMPLATE_ID_EXPR,
2591 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
2594 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2596 explicit_targs = TREE_OPERAND (fn, 1);
2597 fn = TREE_OPERAND (fn, 0);
2601 /* Add the various candidate functions. */
2602 add_candidates (fn, args, explicit_targs, template_only,
2603 /*conversion_path=*/NULL_TREE,
2604 /*access_path=*/NULL_TREE,
2608 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
2612 cand = tourney (*candidates);
2616 /* Return an expression for a call to FN (a namespace-scope function,
2617 or a static member function) with the ARGS. */
2620 build_new_function_call (tree fn, tree args)
2622 struct z_candidate *candidates, *cand;
2625 args = resolve_args (args);
2626 if (args == error_mark_node)
2627 return error_mark_node;
2629 cand = perform_overload_resolution (fn, args, &candidates, &any_viable_p);
2633 if (!any_viable_p && candidates && ! candidates->next)
2634 return build_function_call (candidates->fn, args);
2635 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2636 fn = TREE_OPERAND (fn, 0);
2638 error ("no matching function for call to `%D(%A)'",
2639 DECL_NAME (OVL_CURRENT (fn)), args);
2641 error ("call of overloaded `%D(%A)' is ambiguous",
2642 DECL_NAME (OVL_CURRENT (fn)), args);
2644 print_z_candidates (candidates);
2645 return error_mark_node;
2648 return build_over_call (cand, LOOKUP_NORMAL);
2651 /* Build a call to a global operator new. FNNAME is the name of the
2652 operator (either "operator new" or "operator new[]") and ARGS are
2653 the arguments provided. *SIZE points to the total number of bytes
2654 required by the allocation, and is updated if that is changed here.
2655 *COOKIE_SIZE is non-NULL if a cookie should be used. If this
2656 function determines that no cookie should be used, after all,
2657 *COOKIE_SIZE is set to NULL_TREE. */
2660 build_operator_new_call (tree fnname, tree args, tree *size, tree *cookie_size)
2663 struct z_candidate *candidates;
2664 struct z_candidate *cand;
2667 args = tree_cons (NULL_TREE, *size, args);
2668 args = resolve_args (args);
2669 if (args == error_mark_node)
2672 fns = lookup_function_nonclass (fnname, args);
2674 /* Figure out what function is being called. */
2675 cand = perform_overload_resolution (fns, args, &candidates, &any_viable_p);
2677 /* If no suitable function could be found, issue an error message
2682 error ("no matching function for call to `%D(%A)'",
2683 DECL_NAME (OVL_CURRENT (fns)), args);
2685 error ("call of overloaded `%D(%A)' is ambiguous",
2686 DECL_NAME (OVL_CURRENT (fns)), args);
2688 print_z_candidates (candidates);
2689 return error_mark_node;
2692 /* If a cookie is required, add some extra space. Whether
2693 or not a cookie is required cannot be determined until
2694 after we know which function was called. */
2697 bool use_cookie = true;
2698 if (!abi_version_at_least (2))
2700 tree placement = TREE_CHAIN (args);
2701 /* In G++ 3.2, the check was implemented incorrectly; it
2702 looked at the placement expression, rather than the
2703 type of the function. */
2704 if (placement && !TREE_CHAIN (placement)
2705 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2713 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
2714 /* Skip the size_t parameter. */
2715 arg_types = TREE_CHAIN (arg_types);
2716 /* Check the remaining parameters (if any). */
2718 && TREE_CHAIN (arg_types) == void_list_node
2719 && same_type_p (TREE_VALUE (arg_types),
2723 /* If we need a cookie, adjust the number of bytes allocated. */
2726 /* Update the total size. */
2727 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
2728 /* Update the argument list to reflect the adjusted size. */
2729 TREE_VALUE (args) = *size;
2732 *cookie_size = NULL_TREE;
2735 /* Build the CALL_EXPR. */
2736 return build_over_call (cand, LOOKUP_NORMAL);
2740 build_object_call (tree obj, tree args)
2742 struct z_candidate *candidates = 0, *cand;
2743 tree fns, convs, mem_args = NULL_TREE;
2744 tree type = TREE_TYPE (obj);
2747 if (TYPE_PTRMEMFUNC_P (type))
2749 /* It's no good looking for an overloaded operator() on a
2750 pointer-to-member-function. */
2751 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
2752 return error_mark_node;
2755 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
2756 if (fns == error_mark_node)
2757 return error_mark_node;
2759 args = resolve_args (args);
2761 if (args == error_mark_node)
2762 return error_mark_node;
2766 tree base = BINFO_TYPE (BASELINK_BINFO (fns));
2767 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
2769 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
2771 tree fn = OVL_CURRENT (fns);
2772 if (TREE_CODE (fn) == TEMPLATE_DECL)
2773 add_template_candidate (&candidates, fn, base, NULL_TREE,
2774 mem_args, NULL_TREE,
2777 LOOKUP_NORMAL, DEDUCE_CALL);
2779 add_function_candidate
2780 (&candidates, fn, base, mem_args, TYPE_BINFO (type),
2781 TYPE_BINFO (type), LOOKUP_NORMAL);
2785 convs = lookup_conversions (type);
2787 for (; convs; convs = TREE_CHAIN (convs))
2789 tree fns = TREE_VALUE (convs);
2790 tree totype = TREE_TYPE (TREE_TYPE (OVL_CURRENT (fns)));
2792 if ((TREE_CODE (totype) == POINTER_TYPE
2793 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2794 || (TREE_CODE (totype) == REFERENCE_TYPE
2795 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
2796 || (TREE_CODE (totype) == REFERENCE_TYPE
2797 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
2798 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
2799 for (; fns; fns = OVL_NEXT (fns))
2801 tree fn = OVL_CURRENT (fns);
2802 if (TREE_CODE (fn) == TEMPLATE_DECL)
2803 add_template_conv_candidate
2804 (&candidates, fn, obj, args, totype,
2805 /*access_path=*/NULL_TREE,
2806 /*conversion_path=*/NULL_TREE);
2808 add_conv_candidate (&candidates, fn, obj, args,
2809 /*conversion_path=*/NULL_TREE,
2810 /*access_path=*/NULL_TREE);
2814 candidates = splice_viable (candidates, pedantic, &any_viable_p);
2817 error ("no match for call to `(%T) (%A)'", TREE_TYPE (obj), args);
2818 print_z_candidates (candidates);
2819 return error_mark_node;
2822 cand = tourney (candidates);
2825 error ("call of `(%T) (%A)' is ambiguous", TREE_TYPE (obj), args);
2826 print_z_candidates (candidates);
2827 return error_mark_node;
2830 /* Since cand->fn will be a type, not a function, for a conversion
2831 function, we must be careful not to unconditionally look at
2833 if (TREE_CODE (cand->fn) == FUNCTION_DECL
2834 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
2835 return build_over_call (cand, LOOKUP_NORMAL);
2837 obj = convert_like_with_context
2838 (TREE_VEC_ELT (cand->convs, 0), obj, cand->fn, -1);
2841 return build_function_call (obj, args);
2845 op_error (enum tree_code code, enum tree_code code2,
2846 tree arg1, tree arg2, tree arg3, const char *problem)
2850 if (code == MODIFY_EXPR)
2851 opname = assignment_operator_name_info[code2].name;
2853 opname = operator_name_info[code].name;
2858 error ("%s for ternary 'operator?:' in '%E ? %E : %E'",
2859 problem, arg1, arg2, arg3);
2862 case POSTINCREMENT_EXPR:
2863 case POSTDECREMENT_EXPR:
2864 error ("%s for 'operator%s' in '%E%s'", problem, opname, arg1, opname);
2868 error ("%s for 'operator[]' in '%E[%E]'", problem, arg1, arg2);
2873 error ("%s for '%s' in '%s %E'", problem, opname, opname, arg1);
2878 error ("%s for 'operator%s' in '%E %s %E'",
2879 problem, opname, arg1, opname, arg2);
2881 error ("%s for 'operator%s' in '%s%E'",
2882 problem, opname, opname, arg1);
2887 /* Return the implicit conversion sequence that could be used to
2888 convert E1 to E2 in [expr.cond]. */
2891 conditional_conversion (tree e1, tree e2)
2893 tree t1 = non_reference (TREE_TYPE (e1));
2894 tree t2 = non_reference (TREE_TYPE (e2));
2900 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
2901 implicitly converted (clause _conv_) to the type "reference to
2902 T2", subject to the constraint that in the conversion the
2903 reference must bind directly (_dcl.init.ref_) to E1. */
2904 if (real_lvalue_p (e2))
2906 conv = implicit_conversion (build_reference_type (t2),
2909 LOOKUP_NO_TEMP_BIND);
2916 If E1 and E2 have class type, and the underlying class types are
2917 the same or one is a base class of the other: E1 can be converted
2918 to match E2 if the class of T2 is the same type as, or a base
2919 class of, the class of T1, and the cv-qualification of T2 is the
2920 same cv-qualification as, or a greater cv-qualification than, the
2921 cv-qualification of T1. If the conversion is applied, E1 is
2922 changed to an rvalue of type T2 that still refers to the original
2923 source class object (or the appropriate subobject thereof). */
2924 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
2925 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
2927 if (good_base && at_least_as_qualified_p (t2, t1))
2929 conv = build1 (IDENTITY_CONV, t1, e1);
2930 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
2931 TYPE_MAIN_VARIANT (t2)))
2932 conv = build_conv (BASE_CONV, t2, conv);
2934 conv = build_conv (RVALUE_CONV, t2, conv);
2943 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
2944 converted to the type that expression E2 would have if E2 were
2945 converted to an rvalue (or the type it has, if E2 is an rvalue). */
2946 return implicit_conversion (t2, t1, e1, LOOKUP_NORMAL);
2949 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
2950 arguments to the conditional expression. */
2953 build_conditional_expr (tree arg1, tree arg2, tree arg3)
2958 tree result_type = NULL_TREE;
2959 bool lvalue_p = true;
2960 struct z_candidate *candidates = 0;
2961 struct z_candidate *cand;
2963 /* As a G++ extension, the second argument to the conditional can be
2964 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
2965 c'.) If the second operand is omitted, make sure it is
2966 calculated only once. */
2970 pedwarn ("ISO C++ forbids omitting the middle term of a ?: expression");
2972 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
2973 if (real_lvalue_p (arg1))
2974 arg2 = arg1 = stabilize_reference (arg1);
2976 arg2 = arg1 = save_expr (arg1);
2981 The first expr ession is implicitly converted to bool (clause
2983 arg1 = perform_implicit_conversion (boolean_type_node, arg1);
2985 /* If something has already gone wrong, just pass that fact up the
2987 if (error_operand_p (arg1)
2988 || error_operand_p (arg2)
2989 || error_operand_p (arg3))
2990 return error_mark_node;
2994 If either the second or the third operand has type (possibly
2995 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
2996 array-to-pointer (_conv.array_), and function-to-pointer
2997 (_conv.func_) standard conversions are performed on the second
2998 and third operands. */
2999 arg2_type = TREE_TYPE (arg2);
3000 arg3_type = TREE_TYPE (arg3);
3001 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3003 /* Do the conversions. We don't these for `void' type arguments
3004 since it can't have any effect and since decay_conversion
3005 does not handle that case gracefully. */
3006 if (!VOID_TYPE_P (arg2_type))
3007 arg2 = decay_conversion (arg2);
3008 if (!VOID_TYPE_P (arg3_type))
3009 arg3 = decay_conversion (arg3);
3010 arg2_type = TREE_TYPE (arg2);
3011 arg3_type = TREE_TYPE (arg3);
3015 One of the following shall hold:
3017 --The second or the third operand (but not both) is a
3018 throw-expression (_except.throw_); the result is of the
3019 type of the other and is an rvalue.
3021 --Both the second and the third operands have type void; the
3022 result is of type void and is an rvalue.
3024 We must avoid calling force_rvalue for expressions of type
3025 "void" because it will complain that their value is being
3027 if (TREE_CODE (arg2) == THROW_EXPR
3028 && TREE_CODE (arg3) != THROW_EXPR)
3030 if (!VOID_TYPE_P (arg3_type))
3031 arg3 = force_rvalue (arg3);
3032 arg3_type = TREE_TYPE (arg3);
3033 result_type = arg3_type;
3035 else if (TREE_CODE (arg2) != THROW_EXPR
3036 && TREE_CODE (arg3) == THROW_EXPR)
3038 if (!VOID_TYPE_P (arg2_type))
3039 arg2 = force_rvalue (arg2);
3040 arg2_type = TREE_TYPE (arg2);
3041 result_type = arg2_type;
3043 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3044 result_type = void_type_node;
3047 error ("`%E' has type `void' and is not a throw-expression",
3048 VOID_TYPE_P (arg2_type) ? arg2 : arg3);
3049 return error_mark_node;
3053 goto valid_operands;
3057 Otherwise, if the second and third operand have different types,
3058 and either has (possibly cv-qualified) class type, an attempt is
3059 made to convert each of those operands to the type of the other. */
3060 else if (!same_type_p (arg2_type, arg3_type)
3061 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3063 tree conv2 = conditional_conversion (arg2, arg3);
3064 tree conv3 = conditional_conversion (arg3, arg2);
3068 If both can be converted, or one can be converted but the
3069 conversion is ambiguous, the program is ill-formed. If
3070 neither can be converted, the operands are left unchanged and
3071 further checking is performed as described below. If exactly
3072 one conversion is possible, that conversion is applied to the
3073 chosen operand and the converted operand is used in place of
3074 the original operand for the remainder of this section. */
3075 if ((conv2 && !ICS_BAD_FLAG (conv2)
3076 && conv3 && !ICS_BAD_FLAG (conv3))
3077 || (conv2 && TREE_CODE (conv2) == AMBIG_CONV)
3078 || (conv3 && TREE_CODE (conv3) == AMBIG_CONV))
3080 error ("operands to ?: have different types");
3081 return error_mark_node;
3083 else if (conv2 && !ICS_BAD_FLAG (conv2))
3085 arg2 = convert_like (conv2, arg2);
3086 arg2 = convert_from_reference (arg2);
3087 arg2_type = TREE_TYPE (arg2);
3089 else if (conv3 && !ICS_BAD_FLAG (conv3))
3091 arg3 = convert_like (conv3, arg3);
3092 arg3 = convert_from_reference (arg3);
3093 arg3_type = TREE_TYPE (arg3);
3096 /* If, after the conversion, both operands have class type,
3097 treat the cv-qualification of both operands as if it were the
3098 union of the cv-qualification of the operands.
3100 The standard is not clear about what to do in this
3101 circumstance. For example, if the first operand has type
3102 "const X" and the second operand has a user-defined
3103 conversion to "volatile X", what is the type of the second
3104 operand after this step? Making it be "const X" (matching
3105 the first operand) seems wrong, as that discards the
3106 qualification without actuall performing a copy. Leaving it
3107 as "volatile X" seems wrong as that will result in the
3108 conditional expression failing altogether, even though,
3109 according to this step, the one operand could be converted to
3110 the type of the other. */
3111 if ((conv2 || conv3)
3112 && CLASS_TYPE_P (arg2_type)
3113 && TYPE_QUALS (arg2_type) != TYPE_QUALS (arg3_type))
3114 arg2_type = arg3_type =
3115 cp_build_qualified_type (arg2_type,
3116 TYPE_QUALS (arg2_type)
3117 | TYPE_QUALS (arg3_type));
3122 If the second and third operands are lvalues and have the same
3123 type, the result is of that type and is an lvalue. */
3124 if (real_lvalue_p (arg2)
3125 && real_lvalue_p (arg3)
3126 && same_type_p (arg2_type, arg3_type))
3128 result_type = arg2_type;
3129 goto valid_operands;
3134 Otherwise, the result is an rvalue. If the second and third
3135 operand do not have the same type, and either has (possibly
3136 cv-qualified) class type, overload resolution is used to
3137 determine the conversions (if any) to be applied to the operands
3138 (_over.match.oper_, _over.built_). */
3140 if (!same_type_p (arg2_type, arg3_type)
3141 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3147 /* Rearrange the arguments so that add_builtin_candidate only has
3148 to know about two args. In build_builtin_candidates, the
3149 arguments are unscrambled. */
3153 add_builtin_candidates (&candidates,
3156 ansi_opname (COND_EXPR),
3162 If the overload resolution fails, the program is
3164 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3167 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3168 print_z_candidates (candidates);
3169 return error_mark_node;
3171 cand = tourney (candidates);
3174 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, "no match");
3175 print_z_candidates (candidates);
3176 return error_mark_node;
3181 Otherwise, the conversions thus determined are applied, and
3182 the converted operands are used in place of the original
3183 operands for the remainder of this section. */
3184 conv = TREE_VEC_ELT (cand->convs, 0);
3185 arg1 = convert_like (conv, arg1);
3186 conv = TREE_VEC_ELT (cand->convs, 1);
3187 arg2 = convert_like (conv, arg2);
3188 conv = TREE_VEC_ELT (cand->convs, 2);
3189 arg3 = convert_like (conv, arg3);
3194 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
3195 and function-to-pointer (_conv.func_) standard conversions are
3196 performed on the second and third operands.
3198 We need to force the lvalue-to-rvalue conversion here for class types,
3199 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
3200 that isn't wrapped with a TARGET_EXPR plays havoc with exception
3203 arg2 = force_rvalue (arg2);
3204 if (!CLASS_TYPE_P (arg2_type))
3205 arg2_type = TREE_TYPE (arg2);
3207 arg3 = force_rvalue (arg3);
3208 if (!CLASS_TYPE_P (arg2_type))
3209 arg3_type = TREE_TYPE (arg3);
3211 if (arg2 == error_mark_node || arg3 == error_mark_node)
3212 return error_mark_node;
3216 After those conversions, one of the following shall hold:
3218 --The second and third operands have the same type; the result is of
3220 if (same_type_p (arg2_type, arg3_type))
3221 result_type = arg2_type;
3224 --The second and third operands have arithmetic or enumeration
3225 type; the usual arithmetic conversions are performed to bring
3226 them to a common type, and the result is of that type. */
3227 else if ((ARITHMETIC_TYPE_P (arg2_type)
3228 || TREE_CODE (arg2_type) == ENUMERAL_TYPE)
3229 && (ARITHMETIC_TYPE_P (arg3_type)
3230 || TREE_CODE (arg3_type) == ENUMERAL_TYPE))
3232 /* In this case, there is always a common type. */
3233 result_type = type_after_usual_arithmetic_conversions (arg2_type,
3236 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
3237 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
3238 warning ("enumeral mismatch in conditional expression: `%T' vs `%T'",
3239 arg2_type, arg3_type);
3240 else if (extra_warnings
3241 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
3242 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
3243 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
3244 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
3245 warning ("enumeral and non-enumeral type in conditional expression");
3247 arg2 = perform_implicit_conversion (result_type, arg2);
3248 arg3 = perform_implicit_conversion (result_type, arg3);
3252 --The second and third operands have pointer type, or one has
3253 pointer type and the other is a null pointer constant; pointer
3254 conversions (_conv.ptr_) and qualification conversions
3255 (_conv.qual_) are performed to bring them to their composite
3256 pointer type (_expr.rel_). The result is of the composite
3259 --The second and third operands have pointer to member type, or
3260 one has pointer to member type and the other is a null pointer
3261 constant; pointer to member conversions (_conv.mem_) and
3262 qualification conversions (_conv.qual_) are performed to bring
3263 them to a common type, whose cv-qualification shall match the
3264 cv-qualification of either the second or the third operand.
3265 The result is of the common type. */
3266 else if ((null_ptr_cst_p (arg2)
3267 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
3268 || (null_ptr_cst_p (arg3)
3269 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
3270 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
3271 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
3272 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
3274 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
3275 arg3, "conditional expression");
3276 if (result_type == error_mark_node)
3277 return error_mark_node;
3278 arg2 = perform_implicit_conversion (result_type, arg2);
3279 arg3 = perform_implicit_conversion (result_type, arg3);
3284 error ("operands to ?: have different types");
3285 return error_mark_node;
3289 result = fold_if_not_in_template (build (COND_EXPR, result_type,
3291 /* We can't use result_type below, as fold might have returned a
3294 /* Expand both sides into the same slot, hopefully the target of the
3295 ?: expression. We used to check for TARGET_EXPRs here, but now we
3296 sometimes wrap them in NOP_EXPRs so the test would fail. */
3297 if (!lvalue_p && CLASS_TYPE_P (TREE_TYPE (result)))
3298 result = get_target_expr (result);
3300 /* If this expression is an rvalue, but might be mistaken for an
3301 lvalue, we must add a NON_LVALUE_EXPR. */
3302 if (!lvalue_p && real_lvalue_p (result))
3303 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
3308 /* OPERAND is an operand to an expression. Perform necessary steps
3309 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
3313 prep_operand (tree operand)
3317 operand = convert_from_reference (operand);
3318 if (CLASS_TYPE_P (TREE_TYPE (operand))
3319 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
3320 /* Make sure the template type is instantiated now. */
3321 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
3327 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
3328 OVERLOAD) to the CANDIDATES, returning an updated list of
3329 CANDIDATES. The ARGS are the arguments provided to the call,
3330 without any implicit object parameter. The EXPLICIT_TARGS are
3331 explicit template arguments provided. TEMPLATE_ONLY is true if
3332 only template functions should be considered. CONVERSION_PATH,
3333 ACCESS_PATH, and FLAGS are as for add_function_candidate. */
3336 add_candidates (tree fns, tree args,
3337 tree explicit_targs, bool template_only,
3338 tree conversion_path, tree access_path,
3340 struct z_candidate **candidates)
3343 tree non_static_args;
3345 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
3346 /* Delay creating the implicit this parameter until it is needed. */
3347 non_static_args = NULL_TREE;
3354 fn = OVL_CURRENT (fns);
3355 /* Figure out which set of arguments to use. */
3356 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3358 /* If this function is a non-static member, prepend the implicit
3359 object parameter. */
3360 if (!non_static_args)
3361 non_static_args = tree_cons (NULL_TREE,
3362 build_this (TREE_VALUE (args)),
3364 fn_args = non_static_args;
3367 /* Otherwise, just use the list of arguments provided. */
3370 if (TREE_CODE (fn) == TEMPLATE_DECL)
3371 add_template_candidate (candidates,
3381 else if (!template_only)
3382 add_function_candidate (candidates,
3389 fns = OVL_NEXT (fns);
3394 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
3397 struct z_candidate *candidates = 0, *cand;
3398 tree arglist, fnname;
3400 enum tree_code code2 = NOP_EXPR;
3405 if (error_operand_p (arg1)
3406 || error_operand_p (arg2)
3407 || error_operand_p (arg3))
3408 return error_mark_node;
3410 if (code == MODIFY_EXPR)
3412 code2 = TREE_CODE (arg3);
3414 fnname = ansi_assopname (code2);
3417 fnname = ansi_opname (code);
3419 arg1 = prep_operand (arg1);
3425 case VEC_DELETE_EXPR:
3427 /* Use build_op_new_call and build_op_delete_call instead. */
3431 return build_object_call (arg1, arg2);
3437 arg2 = prep_operand (arg2);
3438 arg3 = prep_operand (arg3);
3440 if (code == COND_EXPR)
3442 if (arg2 == NULL_TREE
3443 || TREE_CODE (TREE_TYPE (arg2)) == VOID_TYPE
3444 || TREE_CODE (TREE_TYPE (arg3)) == VOID_TYPE
3445 || (! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))
3446 && ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3449 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3450 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
3453 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3454 arg2 = integer_zero_node;
3456 arglist = NULL_TREE;
3458 arglist = tree_cons (NULL_TREE, arg3, arglist);
3460 arglist = tree_cons (NULL_TREE, arg2, arglist);
3461 arglist = tree_cons (NULL_TREE, arg1, arglist);
3463 /* Add namespace-scope operators to the list of functions to
3465 add_candidates (lookup_function_nonclass (fnname, arglist),
3466 arglist, NULL_TREE, false, NULL_TREE, NULL_TREE,
3467 flags, &candidates);
3468 /* Add class-member operators to the candidate set. */
3469 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
3473 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 1);
3474 if (fns == error_mark_node)
3477 add_candidates (BASELINK_FUNCTIONS (fns), arglist,
3479 BASELINK_BINFO (fns),
3480 TYPE_BINFO (TREE_TYPE (arg1)),
3481 flags, &candidates);
3484 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3485 to know about two args; a builtin candidate will always have a first
3486 parameter of type bool. We'll handle that in
3487 build_builtin_candidate. */
3488 if (code == COND_EXPR)
3498 args[2] = NULL_TREE;
3501 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
3507 /* For these, the built-in candidates set is empty
3508 [over.match.oper]/3. We don't want non-strict matches
3509 because exact matches are always possible with built-in
3510 operators. The built-in candidate set for COMPONENT_REF
3511 would be empty too, but since there are no such built-in
3512 operators, we accept non-strict matches for them. */
3517 strict_p = pedantic;
3521 candidates = splice_viable (candidates, strict_p, &any_viable_p);
3526 case POSTINCREMENT_EXPR:
3527 case POSTDECREMENT_EXPR:
3528 /* Look for an `operator++ (int)'. If they didn't have
3529 one, then we fall back to the old way of doing things. */
3530 if (flags & LOOKUP_COMPLAIN)
3531 pedwarn ("no `%D(int)' declared for postfix `%s', trying prefix operator instead",
3533 operator_name_info[code].name);
3534 if (code == POSTINCREMENT_EXPR)
3535 code = PREINCREMENT_EXPR;
3537 code = PREDECREMENT_EXPR;
3538 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
3541 /* The caller will deal with these. */
3550 if (flags & LOOKUP_COMPLAIN)
3552 op_error (code, code2, arg1, arg2, arg3, "no match");
3553 print_z_candidates (candidates);
3555 return error_mark_node;
3558 cand = tourney (candidates);
3561 if (flags & LOOKUP_COMPLAIN)
3563 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3564 print_z_candidates (candidates);
3566 return error_mark_node;
3569 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3572 *overloaded_p = true;
3574 return build_over_call (cand, LOOKUP_NORMAL);
3577 /* Check for comparison of different enum types. */
3586 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
3587 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
3588 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
3589 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2))))
3591 warning ("comparison between `%#T' and `%#T'",
3592 TREE_TYPE (arg1), TREE_TYPE (arg2));
3599 /* We need to strip any leading REF_BIND so that bitfields don't cause
3600 errors. This should not remove any important conversions, because
3601 builtins don't apply to class objects directly. */
3602 conv = TREE_VEC_ELT (cand->convs, 0);
3603 if (TREE_CODE (conv) == REF_BIND)
3604 conv = TREE_OPERAND (conv, 0);
3605 arg1 = convert_like (conv, arg1);
3608 conv = TREE_VEC_ELT (cand->convs, 1);
3609 if (TREE_CODE (conv) == REF_BIND)
3610 conv = TREE_OPERAND (conv, 0);
3611 arg2 = convert_like (conv, arg2);
3615 conv = TREE_VEC_ELT (cand->convs, 2);
3616 if (TREE_CODE (conv) == REF_BIND)
3617 conv = TREE_OPERAND (conv, 0);
3618 arg3 = convert_like (conv, arg3);
3625 return build_modify_expr (arg1, code2, arg2);
3628 return build_indirect_ref (arg1, "unary *");
3633 case TRUNC_DIV_EXPR:
3644 case TRUNC_MOD_EXPR:
3648 case TRUTH_ANDIF_EXPR:
3649 case TRUTH_ORIF_EXPR:
3650 return cp_build_binary_op (code, arg1, arg2);
3655 case TRUTH_NOT_EXPR:
3656 case PREINCREMENT_EXPR:
3657 case POSTINCREMENT_EXPR:
3658 case PREDECREMENT_EXPR:
3659 case POSTDECREMENT_EXPR:
3662 return build_unary_op (code, arg1, candidates != 0);
3665 return build_array_ref (arg1, arg2);
3668 return build_conditional_expr (arg1, arg2, arg3);
3671 return build_m_component_ref
3672 (build_indirect_ref (arg1, NULL), arg2);
3674 /* The caller will deal with these. */
3686 /* Build a call to operator delete. This has to be handled very specially,
3687 because the restrictions on what signatures match are different from all
3688 other call instances. For a normal delete, only a delete taking (void *)
3689 or (void *, size_t) is accepted. For a placement delete, only an exact
3690 match with the placement new is accepted.
3692 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
3693 ADDR is the pointer to be deleted.
3694 SIZE is the size of the memory block to be deleted.
3695 FLAGS are the usual overloading flags.
3696 PLACEMENT is the corresponding placement new call, or NULL_TREE. */
3699 build_op_delete_call (enum tree_code code, tree addr, tree size,
3700 int flags, tree placement)
3702 tree fn = NULL_TREE;
3703 tree fns, fnname, argtypes, args, type;
3706 if (addr == error_mark_node)
3707 return error_mark_node;
3709 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
3711 fnname = ansi_opname (code);
3713 if (IS_AGGR_TYPE (type) && ! (flags & LOOKUP_GLOBAL))
3716 If the result of the lookup is ambiguous or inaccessible, or if
3717 the lookup selects a placement deallocation function, the
3718 program is ill-formed.
3720 Therefore, we ask lookup_fnfields to complain about ambiguity. */
3722 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
3723 if (fns == error_mark_node)
3724 return error_mark_node;
3729 if (fns == NULL_TREE)
3730 fns = lookup_name_nonclass (fnname);
3737 /* Find the allocation function that is being called. */
3738 call_expr = placement;
3739 /* Extract the function. */
3740 alloc_fn = get_callee_fndecl (call_expr);
3741 my_friendly_assert (alloc_fn != NULL_TREE, 20020327);
3742 /* Then the second parm type. */
3743 argtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (alloc_fn)));
3744 /* Also the second argument. */
3745 args = TREE_CHAIN (TREE_OPERAND (call_expr, 1));
3749 /* First try it without the size argument. */
3750 argtypes = void_list_node;
3754 /* Strip const and volatile from addr. */
3755 addr = cp_convert (ptr_type_node, addr);
3757 /* We make two tries at finding a matching `operator delete'. On
3758 the first pass, we look for a one-operator (or placement)
3759 operator delete. If we're not doing placement delete, then on
3760 the second pass we look for a two-argument delete. */
3761 for (pass = 0; pass < (placement ? 1 : 2); ++pass)
3763 /* Go through the `operator delete' functions looking for one
3764 with a matching type. */
3765 for (fn = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
3771 /* The first argument must be "void *". */
3772 t = TYPE_ARG_TYPES (TREE_TYPE (OVL_CURRENT (fn)));
3773 if (!same_type_p (TREE_VALUE (t), ptr_type_node))
3776 /* On the first pass, check the rest of the arguments. */
3782 if (!same_type_p (TREE_VALUE (a), TREE_VALUE (t)))
3790 /* On the second pass, the second argument must be
3793 && same_type_p (TREE_VALUE (t), sizetype)
3794 && TREE_CHAIN (t) == void_list_node)
3798 /* If we found a match, we're done. */
3803 /* If we have a matching function, call it. */
3806 /* Make sure we have the actual function, and not an
3808 fn = OVL_CURRENT (fn);
3810 /* If the FN is a member function, make sure that it is
3812 if (DECL_CLASS_SCOPE_P (fn))
3813 perform_or_defer_access_check (TYPE_BINFO (type), fn);
3816 args = tree_cons (NULL_TREE, addr, args);
3818 args = tree_cons (NULL_TREE, addr,
3819 build_tree_list (NULL_TREE, size));
3823 /* The placement args might not be suitable for overload
3824 resolution at this point, so build the call directly. */
3826 return build_cxx_call (fn, args, args);
3829 return build_function_call (fn, args);
3832 /* If we are doing placement delete we do nothing if we don't find a
3833 matching op delete. */
3837 error ("no suitable `operator %s' for `%T'",
3838 operator_name_info[(int)code].name, type);
3839 return error_mark_node;
3842 /* If the current scope isn't allowed to access DECL along
3843 BASETYPE_PATH, give an error. The most derived class in
3844 BASETYPE_PATH is the one used to qualify DECL. */
3847 enforce_access (tree basetype_path, tree decl)
3849 my_friendly_assert (TREE_CODE (basetype_path) == TREE_VEC, 20030624);
3851 if (!accessible_p (basetype_path, decl))
3853 if (TREE_PRIVATE (decl))
3854 cp_error_at ("`%+#D' is private", decl);
3855 else if (TREE_PROTECTED (decl))
3856 cp_error_at ("`%+#D' is protected", decl);
3858 cp_error_at ("`%+#D' is inaccessible", decl);
3859 error ("within this context");
3866 /* Check that a callable constructor to initialize a temporary of
3867 TYPE from an EXPR exists. */
3870 check_constructor_callable (tree type, tree expr)
3872 build_special_member_call (NULL_TREE,
3873 complete_ctor_identifier,
3874 build_tree_list (NULL_TREE, expr),
3876 LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING
3877 | LOOKUP_NO_CONVERSION
3878 | LOOKUP_CONSTRUCTOR_CALLABLE);
3881 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
3882 bitwise or of LOOKUP_* values. If any errors are warnings are
3883 generated, set *DIAGNOSTIC_FN to "error" or "warning",
3884 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
3888 build_temp (tree expr, tree type, int flags,
3889 void (**diagnostic_fn)(const char *, ...))
3893 savew = warningcount, savee = errorcount;
3894 expr = build_special_member_call (NULL_TREE,
3895 complete_ctor_identifier,
3896 build_tree_list (NULL_TREE, expr),
3899 if (warningcount > savew)
3900 *diagnostic_fn = warning;
3901 else if (errorcount > savee)
3902 *diagnostic_fn = error;
3904 *diagnostic_fn = NULL;
3909 /* Perform the conversions in CONVS on the expression EXPR. FN and
3910 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
3911 indicates the `this' argument of a method. INNER is nonzero when
3912 being called to continue a conversion chain. It is negative when a
3913 reference binding will be applied, positive otherwise. If
3914 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
3915 conversions will be emitted if appropriate. */
3918 convert_like_real (tree convs, tree expr, tree fn, int argnum, int inner,
3919 bool issue_conversion_warnings)
3921 tree totype = TREE_TYPE (convs);
3922 void (*diagnostic_fn)(const char *, ...);
3924 if (ICS_BAD_FLAG (convs)
3925 && TREE_CODE (convs) != USER_CONV
3926 && TREE_CODE (convs) != AMBIG_CONV
3927 && TREE_CODE (convs) != REF_BIND)
3930 for (; t; t = TREE_OPERAND (t, 0))
3932 if (TREE_CODE (t) == USER_CONV || !ICS_BAD_FLAG (t))
3934 expr = convert_like_real (t, expr, fn, argnum, 1,
3935 /*issue_conversion_warnings=*/false);
3938 else if (TREE_CODE (t) == AMBIG_CONV)
3939 return convert_like_real (t, expr, fn, argnum, 1,
3940 /*issue_conversion_warnings=*/false);
3941 else if (TREE_CODE (t) == IDENTITY_CONV)
3944 pedwarn ("invalid conversion from `%T' to `%T'", TREE_TYPE (expr), totype);
3946 pedwarn (" initializing argument %P of `%D'", argnum, fn);
3947 return cp_convert (totype, expr);
3950 if (issue_conversion_warnings)
3951 expr = dubious_conversion_warnings
3952 (totype, expr, "converting", fn, argnum);
3953 switch (TREE_CODE (convs))
3957 struct z_candidate *cand = USER_CONV_CAND (convs);
3958 tree convfn = cand->fn;
3961 if (DECL_CONSTRUCTOR_P (convfn))
3963 tree t = build_int_2 (0, 0);
3964 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (convfn));
3966 args = build_tree_list (NULL_TREE, expr);
3967 if (DECL_HAS_IN_CHARGE_PARM_P (convfn)
3968 || DECL_HAS_VTT_PARM_P (convfn))
3969 /* We should never try to call the abstract or base constructor
3972 args = tree_cons (NULL_TREE, t, args);
3975 args = build_this (expr);
3976 expr = build_over_call (cand, LOOKUP_NORMAL);
3978 /* If this is a constructor or a function returning an aggr type,
3979 we need to build up a TARGET_EXPR. */
3980 if (DECL_CONSTRUCTOR_P (convfn))
3981 expr = build_cplus_new (totype, expr);
3983 /* The result of the call is then used to direct-initialize the object
3984 that is the destination of the copy-initialization. [dcl.init]
3986 Note that this step is not reflected in the conversion sequence;
3987 it affects the semantics when we actually perform the
3988 conversion, but is not considered during overload resolution.
3990 If the target is a class, that means call a ctor. */
3991 if (IS_AGGR_TYPE (totype)
3992 && (inner >= 0 || !lvalue_p (expr)))
3996 /* Core issue 84, now a DR, says that we don't
3997 allow UDCs for these args (which deliberately
3998 breaks copy-init of an auto_ptr<Base> from an
3999 auto_ptr<Derived>). */
4000 LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION,
4007 (" initializing argument %P of `%D' from result of `%D'",
4008 argnum, fn, convfn);
4011 (" initializing temporary from result of `%D'", convfn);
4013 expr = build_cplus_new (totype, expr);
4018 if (type_unknown_p (expr))
4019 expr = instantiate_type (totype, expr, tf_error | tf_warning);
4020 /* Convert a non-array constant variable to its underlying
4021 value, unless we are about to bind it to a reference, in
4022 which case we need to leave it as an lvalue. */
4024 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
4025 expr = decl_constant_value (expr);
4026 if (CHECK_COPY_CONSTRUCTOR_P (convs))
4027 check_constructor_callable (totype, expr);
4031 /* Call build_user_type_conversion again for the error. */
4032 return build_user_type_conversion
4033 (totype, TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
4039 expr = convert_like_real (TREE_OPERAND (convs, 0), expr, fn, argnum,
4040 TREE_CODE (convs) == REF_BIND ? -1 : 1,
4041 /*issue_conversion_warnings=*/false);
4042 if (expr == error_mark_node)
4043 return error_mark_node;
4045 switch (TREE_CODE (convs))
4048 if (! IS_AGGR_TYPE (totype))
4050 /* Else fall through. */
4052 if (TREE_CODE (convs) == BASE_CONV && !NEED_TEMPORARY_P (convs))
4054 /* We are going to bind a reference directly to a base-class
4055 subobject of EXPR. */
4056 if (CHECK_COPY_CONSTRUCTOR_P (convs))
4057 check_constructor_callable (TREE_TYPE (expr), expr);
4058 /* Build an expression for `*((base*) &expr)'. */
4059 expr = build_unary_op (ADDR_EXPR, expr, 0);
4060 expr = perform_implicit_conversion (build_pointer_type (totype),
4062 expr = build_indirect_ref (expr, "implicit conversion");
4066 /* Copy-initialization where the cv-unqualified version of the source
4067 type is the same class as, or a derived class of, the class of the
4068 destination [is treated as direct-initialization]. [dcl.init] */
4069 expr = build_temp (expr, totype, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
4071 if (diagnostic_fn && fn)
4072 diagnostic_fn (" initializing argument %P of `%D'", argnum, fn);
4073 return build_cplus_new (totype, expr);
4077 tree ref_type = totype;
4079 /* If necessary, create a temporary. */
4080 if (NEED_TEMPORARY_P (convs) || !lvalue_p (expr))
4082 tree type = TREE_TYPE (TREE_OPERAND (convs, 0));
4084 if (!CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
4086 /* If the reference is volatile or non-const, we
4087 cannot create a temporary. */
4088 cp_lvalue_kind lvalue = real_lvalue_p (expr);
4090 if (lvalue & clk_bitfield)
4091 error ("cannot bind bitfield `%E' to `%T'",
4093 else if (lvalue & clk_packed)
4094 error ("cannot bind packed field `%E' to `%T'",
4097 error ("cannot bind rvalue `%E' to `%T'", expr, ref_type);
4098 return error_mark_node;
4100 expr = build_target_expr_with_type (expr, type);
4103 /* Take the address of the thing to which we will bind the
4105 expr = build_unary_op (ADDR_EXPR, expr, 1);
4106 if (expr == error_mark_node)
4107 return error_mark_node;
4109 /* Convert it to a pointer to the type referred to by the
4110 reference. This will adjust the pointer if a derived to
4111 base conversion is being performed. */
4112 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
4114 /* Convert the pointer to the desired reference type. */
4115 return build_nop (ref_type, expr);
4119 return decay_conversion (expr);
4122 /* Warn about deprecated conversion if appropriate. */
4123 string_conv_p (totype, expr, 1);
4129 return ocp_convert (totype, expr, CONV_IMPLICIT,
4130 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
4133 /* Build a call to __builtin_trap. */
4136 call_builtin_trap (void)
4138 tree fn = IDENTIFIER_GLOBAL_VALUE (get_identifier ("__builtin_trap"));
4140 my_friendly_assert (fn != NULL, 20030927);
4141 fn = build_call (fn, NULL_TREE);
4145 /* ARG is being passed to a varargs function. Perform any conversions
4146 required. Return the converted value. */
4149 convert_arg_to_ellipsis (tree arg)
4153 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
4154 standard conversions are performed. */
4155 arg = decay_conversion (arg);
4158 If the argument has integral or enumeration type that is subject
4159 to the integral promotions (_conv.prom_), or a floating point
4160 type that is subject to the floating point promotion
4161 (_conv.fpprom_), the value of the argument is converted to the
4162 promoted type before the call. */
4163 if (TREE_CODE (TREE_TYPE (arg)) == REAL_TYPE
4164 && (TYPE_PRECISION (TREE_TYPE (arg))
4165 < TYPE_PRECISION (double_type_node)))
4166 arg = convert_to_real (double_type_node, arg);
4167 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
4168 arg = perform_integral_promotions (arg);
4170 arg = require_complete_type (arg);
4172 if (arg != error_mark_node
4173 && !pod_type_p (TREE_TYPE (arg)))
4175 /* Undefined behavior [expr.call] 5.2.2/7. We used to just warn
4176 here and do a bitwise copy, but now cp_expr_size will abort if we
4178 If the call appears in the context of a sizeof expression,
4179 there is no need to emit a warning, since the expression won't be
4180 evaluated. We keep the builtin_trap just as a safety check. */
4181 if (!skip_evaluation)
4182 warning ("cannot pass objects of non-POD type `%#T' through `...'; "
4183 "call will abort at runtime", TREE_TYPE (arg));
4184 arg = call_builtin_trap ();
4185 arg = build (COMPOUND_EXPR, integer_type_node, arg,
4192 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
4195 build_x_va_arg (tree expr, tree type)
4197 if (processing_template_decl)
4198 return build_min (VA_ARG_EXPR, type, expr);
4200 type = complete_type_or_else (type, NULL_TREE);
4202 if (expr == error_mark_node || !type)
4203 return error_mark_node;
4205 if (! pod_type_p (type))
4207 /* Undefined behavior [expr.call] 5.2.2/7. */
4208 warning ("cannot receive objects of non-POD type `%#T' through `...'; \
4209 call will abort at runtime",
4211 expr = convert (build_pointer_type (type), null_node);
4212 expr = build (COMPOUND_EXPR, TREE_TYPE (expr),
4213 call_builtin_trap (), expr);
4214 expr = build_indirect_ref (expr, NULL);
4218 return build_va_arg (expr, type);
4221 /* TYPE has been given to va_arg. Apply the default conversions which
4222 would have happened when passed via ellipsis. Return the promoted
4223 type, or the passed type if there is no change. */
4226 cxx_type_promotes_to (tree type)
4230 /* Perform the array-to-pointer and function-to-pointer
4232 type = type_decays_to (type);
4234 promote = type_promotes_to (type);
4235 if (same_type_p (type, promote))
4241 /* ARG is a default argument expression being passed to a parameter of
4242 the indicated TYPE, which is a parameter to FN. Do any required
4243 conversions. Return the converted value. */
4246 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
4248 /* If the ARG is an unparsed default argument expression, the
4249 conversion cannot be performed. */
4250 if (TREE_CODE (arg) == DEFAULT_ARG)
4252 error ("the default argument for parameter %d of `%D' has "
4253 "not yet been parsed",
4255 return error_mark_node;
4258 if (fn && DECL_TEMPLATE_INFO (fn))
4259 arg = tsubst_default_argument (fn, type, arg);
4261 arg = break_out_target_exprs (arg);
4263 if (TREE_CODE (arg) == CONSTRUCTOR)
4265 arg = digest_init (type, arg, 0);
4266 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4267 "default argument", fn, parmnum);
4271 /* This could get clobbered by the following call. */
4272 if (TREE_HAS_CONSTRUCTOR (arg))
4273 arg = copy_node (arg);
4275 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
4276 "default argument", fn, parmnum);
4277 arg = convert_for_arg_passing (type, arg);
4283 /* Returns the type which will really be used for passing an argument of
4287 type_passed_as (tree type)
4289 /* Pass classes with copy ctors by invisible reference. */
4290 if (TREE_ADDRESSABLE (type))
4291 type = build_reference_type (type);
4292 else if (PROMOTE_PROTOTYPES
4293 && INTEGRAL_TYPE_P (type)
4294 && COMPLETE_TYPE_P (type)
4295 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4296 TYPE_SIZE (integer_type_node)))
4297 type = integer_type_node;
4302 /* Actually perform the appropriate conversion. */
4305 convert_for_arg_passing (tree type, tree val)
4307 if (val == error_mark_node)
4309 /* Pass classes with copy ctors by invisible reference. */
4310 else if (TREE_ADDRESSABLE (type))
4311 val = build1 (ADDR_EXPR, build_reference_type (type), val);
4312 else if (PROMOTE_PROTOTYPES
4313 && INTEGRAL_TYPE_P (type)
4314 && COMPLETE_TYPE_P (type)
4315 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
4316 TYPE_SIZE (integer_type_node)))
4317 val = perform_integral_promotions (val);
4321 /* Returns true iff FN is a function with magic varargs, i.e. ones for
4322 which no conversions at all should be done. This is true for some
4323 builtins which don't act like normal functions. */
4326 magic_varargs_p (tree fn)
4328 if (DECL_BUILT_IN (fn))
4329 switch (DECL_FUNCTION_CODE (fn))
4331 case BUILT_IN_CLASSIFY_TYPE:
4332 case BUILT_IN_CONSTANT_P:
4333 case BUILT_IN_NEXT_ARG:
4334 case BUILT_IN_STDARG_START:
4335 case BUILT_IN_VA_START:
4344 /* Subroutine of the various build_*_call functions. Overload resolution
4345 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
4346 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
4347 bitmask of various LOOKUP_* flags which apply to the call itself. */
4350 build_over_call (struct z_candidate *cand, int flags)
4353 tree args = cand->args;
4354 tree convs = cand->convs;
4355 tree converted_args = NULL_TREE;
4356 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
4357 tree conv, arg, val;
4361 /* In a template, there is no need to perform all of the work that
4362 is normally done. We are only interested in the type of the call
4363 expression, i.e., the return type of the function. Any semantic
4364 errors will be deferred until the template is instantiated. */
4365 if (processing_template_decl)
4369 return_type = TREE_TYPE (TREE_TYPE (fn));
4370 expr = build (CALL_EXPR, return_type, fn, args);
4371 if (TREE_THIS_VOLATILE (fn) && cfun)
4372 current_function_returns_abnormally = 1;
4373 if (!VOID_TYPE_P (return_type))
4374 require_complete_type (return_type);
4375 return convert_from_reference (expr);
4378 /* Give any warnings we noticed during overload resolution. */
4380 for (val = cand->warnings; val; val = TREE_CHAIN (val))
4381 joust (cand, WRAPPER_ZC (TREE_VALUE (val)), 1);
4383 if (DECL_FUNCTION_MEMBER_P (fn))
4385 /* If FN is a template function, two cases must be considered.
4390 template <class T> void f();
4392 template <class T> struct B {
4396 struct C : A, B<int> {
4398 using B<int>::g; // #2
4401 In case #1 where `A::f' is a member template, DECL_ACCESS is
4402 recorded in the primary template but not in its specialization.
4403 We check access of FN using its primary template.
4405 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
4406 because it is a member of class template B, DECL_ACCESS is
4407 recorded in the specialization `B<int>::g'. We cannot use its
4408 primary template because `B<T>::g' and `B<int>::g' may have
4409 different access. */
4410 if (DECL_TEMPLATE_INFO (fn)
4411 && is_member_template (DECL_TI_TEMPLATE (fn)))
4412 perform_or_defer_access_check (cand->access_path,
4413 DECL_TI_TEMPLATE (fn));
4415 perform_or_defer_access_check (cand->access_path, fn);
4418 if (args && TREE_CODE (args) != TREE_LIST)
4419 args = build_tree_list (NULL_TREE, args);
4422 /* The implicit parameters to a constructor are not considered by overload
4423 resolution, and must be of the proper type. */
4424 if (DECL_CONSTRUCTOR_P (fn))
4426 converted_args = tree_cons (NULL_TREE, TREE_VALUE (arg), converted_args);
4427 arg = TREE_CHAIN (arg);
4428 parm = TREE_CHAIN (parm);
4429 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4430 /* We should never try to call the abstract constructor. */
4432 if (DECL_HAS_VTT_PARM_P (fn))
4434 converted_args = tree_cons
4435 (NULL_TREE, TREE_VALUE (arg), converted_args);
4436 arg = TREE_CHAIN (arg);
4437 parm = TREE_CHAIN (parm);
4440 /* Bypass access control for 'this' parameter. */
4441 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
4443 tree parmtype = TREE_VALUE (parm);
4444 tree argtype = TREE_TYPE (TREE_VALUE (arg));
4448 if (ICS_BAD_FLAG (TREE_VEC_ELT (convs, i)))
4449 pedwarn ("passing `%T' as `this' argument of `%#D' discards qualifiers",
4450 TREE_TYPE (argtype), fn);
4452 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
4453 X is called for an object that is not of type X, or of a type
4454 derived from X, the behavior is undefined.
4456 So we can assume that anything passed as 'this' is non-null, and
4457 optimize accordingly. */
4458 my_friendly_assert (TREE_CODE (parmtype) == POINTER_TYPE, 19990811);
4459 /* Convert to the base in which the function was declared. */
4460 my_friendly_assert (cand->conversion_path != NULL_TREE, 20020730);
4461 converted_arg = build_base_path (PLUS_EXPR,
4463 cand->conversion_path,
4465 /* Check that the base class is accessible. */
4466 if (!accessible_base_p (TREE_TYPE (argtype),
4467 BINFO_TYPE (cand->conversion_path)))
4468 error ("`%T' is not an accessible base of `%T'",
4469 BINFO_TYPE (cand->conversion_path),
4470 TREE_TYPE (argtype));
4471 /* If fn was found by a using declaration, the conversion path
4472 will be to the derived class, not the base declaring fn. We
4473 must convert from derived to base. */
4474 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
4475 TREE_TYPE (parmtype), ba_ignore, NULL);
4476 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
4479 converted_args = tree_cons (NULL_TREE, converted_arg, converted_args);
4480 parm = TREE_CHAIN (parm);
4481 arg = TREE_CHAIN (arg);
4487 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
4489 tree type = TREE_VALUE (parm);
4491 conv = TREE_VEC_ELT (convs, i);
4492 val = convert_like_with_context
4493 (conv, TREE_VALUE (arg), fn, i - is_method);
4495 val = convert_for_arg_passing (type, val);
4496 converted_args = tree_cons (NULL_TREE, val, converted_args);
4499 /* Default arguments */
4500 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
4502 = tree_cons (NULL_TREE,
4503 convert_default_arg (TREE_VALUE (parm),
4504 TREE_PURPOSE (parm),
4509 for (; arg; arg = TREE_CHAIN (arg))
4511 tree a = TREE_VALUE (arg);
4512 if (magic_varargs_p (fn))
4513 /* Do no conversions for magic varargs. */;
4515 a = convert_arg_to_ellipsis (a);
4516 converted_args = tree_cons (NULL_TREE, a, converted_args);
4519 converted_args = nreverse (converted_args);
4522 check_function_format (NULL, TYPE_ATTRIBUTES (TREE_TYPE (fn)),
4525 /* Avoid actually calling copy constructors and copy assignment operators,
4528 if (! flag_elide_constructors)
4529 /* Do things the hard way. */;
4530 else if (TREE_VEC_LENGTH (convs) == 1
4531 && DECL_COPY_CONSTRUCTOR_P (fn))
4534 arg = skip_artificial_parms_for (fn, converted_args);
4535 arg = TREE_VALUE (arg);
4537 /* Pull out the real argument, disregarding const-correctness. */
4539 while (TREE_CODE (targ) == NOP_EXPR
4540 || TREE_CODE (targ) == NON_LVALUE_EXPR
4541 || TREE_CODE (targ) == CONVERT_EXPR)
4542 targ = TREE_OPERAND (targ, 0);
4543 if (TREE_CODE (targ) == ADDR_EXPR)
4545 targ = TREE_OPERAND (targ, 0);
4546 if (!same_type_ignoring_top_level_qualifiers_p
4547 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
4556 arg = build_indirect_ref (arg, 0);
4558 /* [class.copy]: the copy constructor is implicitly defined even if
4559 the implementation elided its use. */
4560 if (TYPE_HAS_COMPLEX_INIT_REF (DECL_CONTEXT (fn)))
4563 /* If we're creating a temp and we already have one, don't create a
4564 new one. If we're not creating a temp but we get one, use
4565 INIT_EXPR to collapse the temp into our target. Otherwise, if the
4566 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
4567 temp or an INIT_EXPR otherwise. */
4568 if (integer_zerop (TREE_VALUE (args)))
4570 if (TREE_CODE (arg) == TARGET_EXPR)
4572 else if (TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4573 return build_target_expr_with_type (arg, DECL_CONTEXT (fn));
4575 else if (TREE_CODE (arg) == TARGET_EXPR
4576 || TYPE_HAS_TRIVIAL_INIT_REF (DECL_CONTEXT (fn)))
4578 tree to = stabilize_reference
4579 (build_indirect_ref (TREE_VALUE (args), 0));
4581 val = build (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
4585 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
4587 && TYPE_HAS_TRIVIAL_ASSIGN_REF (DECL_CONTEXT (fn)))
4589 tree to = stabilize_reference
4590 (build_indirect_ref (TREE_VALUE (converted_args), 0));
4591 tree type = TREE_TYPE (to);
4592 tree as_base = CLASSTYPE_AS_BASE (type);
4594 arg = build_indirect_ref (TREE_VALUE (TREE_CHAIN (converted_args)), 0);
4595 if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
4596 val = build (MODIFY_EXPR, TREE_TYPE (to), to, arg);
4599 /* We must only copy the non-tail padding parts. Use
4600 CLASSTYPE_AS_BASE for the bitwise copy. */
4601 tree to_ptr, arg_ptr, to_as_base, arg_as_base, base_ptr_type;
4604 to_ptr = save_expr (build_unary_op (ADDR_EXPR, to, 0));
4605 arg_ptr = build_unary_op (ADDR_EXPR, arg, 0);
4607 base_ptr_type = build_pointer_type (as_base);
4608 to_as_base = build_nop (base_ptr_type, to_ptr);
4609 to_as_base = build_indirect_ref (to_as_base, 0);
4610 arg_as_base = build_nop (base_ptr_type, arg_ptr);
4611 arg_as_base = build_indirect_ref (arg_as_base, 0);
4613 save_to = build_indirect_ref (to_ptr, 0);
4615 val = build (MODIFY_EXPR, as_base, to_as_base, arg_as_base);
4616 val = convert_to_void (val, NULL);
4617 val = build (COMPOUND_EXPR, type, val, save_to);
4618 TREE_NO_UNUSED_WARNING (val) = 1;
4626 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
4628 tree t, *p = &TREE_VALUE (converted_args);
4629 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (*p)),
4632 my_friendly_assert (binfo && binfo != error_mark_node, 20010730);
4634 *p = build_base_path (PLUS_EXPR, *p, binfo, 1);
4635 if (TREE_SIDE_EFFECTS (*p))
4636 *p = save_expr (*p);
4637 t = build_pointer_type (TREE_TYPE (fn));
4638 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
4639 fn = build_java_interface_fn_ref (fn, *p);
4641 fn = build_vfn_ref (build_indirect_ref (*p, 0), DECL_VINDEX (fn));
4644 else if (DECL_INLINE (fn))
4645 fn = inline_conversion (fn);
4647 fn = build_addr_func (fn);
4649 return build_cxx_call (fn, args, converted_args);
4652 /* Build and return a call to FN, using the the CONVERTED_ARGS. ARGS
4653 gives the original form of the arguments. This function performs
4654 no overload resolution, conversion, or other high-level
4658 build_cxx_call(tree fn, tree args, tree converted_args)
4662 /* Recognize certain built-in functions so we can make tree-codes
4663 other than CALL_EXPR. We do this when it enables fold-const.c
4664 to do something useful. */
4665 if (TREE_CODE (fn) == ADDR_EXPR
4666 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4667 && DECL_BUILT_IN (TREE_OPERAND (fn, 0)))
4670 exp = expand_tree_builtin (TREE_OPERAND (fn, 0), args, converted_args);
4675 fn = build_call (fn, converted_args);
4677 /* If this call might throw an exception, note that fact. */
4678 fndecl = get_callee_fndecl (fn);
4679 if ((!fndecl || !TREE_NOTHROW (fndecl))
4680 && at_function_scope_p ()
4682 cp_function_chain->can_throw = 1;
4684 /* Some built-in function calls will be evaluated at compile-time in
4688 if (VOID_TYPE_P (TREE_TYPE (fn)))
4691 fn = require_complete_type (fn);
4692 if (fn == error_mark_node)
4693 return error_mark_node;
4695 if (IS_AGGR_TYPE (TREE_TYPE (fn)))
4696 fn = build_cplus_new (TREE_TYPE (fn), fn);
4697 return convert_from_reference (fn);
4700 static GTY(()) tree java_iface_lookup_fn;
4702 /* Make an expression which yields the address of the Java interface
4703 method FN. This is achieved by generating a call to libjava's
4704 _Jv_LookupInterfaceMethodIdx(). */
4707 build_java_interface_fn_ref (tree fn, tree instance)
4709 tree lookup_args, lookup_fn, method, idx;
4710 tree klass_ref, iface, iface_ref;
4713 if (!java_iface_lookup_fn)
4715 tree endlink = build_void_list_node ();
4716 tree t = tree_cons (NULL_TREE, ptr_type_node,
4717 tree_cons (NULL_TREE, ptr_type_node,
4718 tree_cons (NULL_TREE, java_int_type_node,
4720 java_iface_lookup_fn
4721 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
4722 build_function_type (ptr_type_node, t),
4723 0, NOT_BUILT_IN, NULL, NULL_TREE);
4726 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
4727 This is the first entry in the vtable. */
4728 klass_ref = build_vtbl_ref (build_indirect_ref (instance, 0),
4731 /* Get the java.lang.Class pointer for the interface being called. */
4732 iface = DECL_CONTEXT (fn);
4733 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
4734 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
4735 || DECL_CONTEXT (iface_ref) != iface)
4737 error ("could not find class$ field in java interface type `%T'",
4739 return error_mark_node;
4741 iface_ref = build1 (ADDR_EXPR, build_pointer_type (iface), iface_ref);
4743 /* Determine the itable index of FN. */
4745 for (method = TYPE_METHODS (iface); method; method = TREE_CHAIN (method))
4747 if (!DECL_VIRTUAL_P (method))
4753 idx = build_int_2 (i, 0);
4755 lookup_args = tree_cons (NULL_TREE, klass_ref,
4756 tree_cons (NULL_TREE, iface_ref,
4757 build_tree_list (NULL_TREE, idx)));
4758 lookup_fn = build1 (ADDR_EXPR,
4759 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
4760 java_iface_lookup_fn);
4761 return build (CALL_EXPR, ptr_type_node, lookup_fn, lookup_args, NULL_TREE);
4764 /* Returns the value to use for the in-charge parameter when making a
4765 call to a function with the indicated NAME. */
4768 in_charge_arg_for_name (tree name)
4770 if (name == base_ctor_identifier
4771 || name == base_dtor_identifier)
4772 return integer_zero_node;
4773 else if (name == complete_ctor_identifier)
4774 return integer_one_node;
4775 else if (name == complete_dtor_identifier)
4776 return integer_two_node;
4777 else if (name == deleting_dtor_identifier)
4778 return integer_three_node;
4780 /* This function should only be called with one of the names listed
4786 /* Build a call to a constructor, destructor, or an assignment
4787 operator for INSTANCE, an expression with class type. NAME
4788 indicates the special member function to call; ARGS are the
4789 arguments. BINFO indicates the base of INSTANCE that is to be
4790 passed as the `this' parameter to the member function called.
4792 FLAGS are the LOOKUP_* flags to use when processing the call.
4794 If NAME indicates a complete object constructor, INSTANCE may be
4795 NULL_TREE. In this case, the caller will call build_cplus_new to
4796 store the newly constructed object into a VAR_DECL. */
4799 build_special_member_call (tree instance, tree name, tree args,
4800 tree binfo, int flags)
4803 /* The type of the subobject to be constructed or destroyed. */
4806 my_friendly_assert (name == complete_ctor_identifier
4807 || name == base_ctor_identifier
4808 || name == complete_dtor_identifier
4809 || name == base_dtor_identifier
4810 || name == deleting_dtor_identifier
4811 || name == ansi_assopname (NOP_EXPR),
4813 my_friendly_assert (binfo != NULL_TREE, 20020712);
4815 class_type = BINFO_TYPE (binfo);
4817 /* Handle the special case where INSTANCE is NULL_TREE. */
4818 if (name == complete_ctor_identifier && !instance)
4820 instance = build_int_2 (0, 0);
4821 TREE_TYPE (instance) = build_pointer_type (class_type);
4822 instance = build1 (INDIRECT_REF, class_type, instance);
4826 if (name == complete_dtor_identifier
4827 || name == base_dtor_identifier
4828 || name == deleting_dtor_identifier)
4829 my_friendly_assert (args == NULL_TREE, 20020712);
4831 /* Convert to the base class, if necessary. */
4832 if (!same_type_ignoring_top_level_qualifiers_p
4833 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
4835 if (name != ansi_assopname (NOP_EXPR))
4836 /* For constructors and destructors, either the base is
4837 non-virtual, or it is virtual but we are doing the
4838 conversion from a constructor or destructor for the
4839 complete object. In either case, we can convert
4841 instance = convert_to_base_statically (instance, binfo);
4843 /* However, for assignment operators, we must convert
4844 dynamically if the base is virtual. */
4845 instance = build_base_path (PLUS_EXPR, instance,
4846 binfo, /*nonnull=*/1);
4850 my_friendly_assert (instance != NULL_TREE, 20020712);
4852 /* Resolve the name. */
4853 if (!complete_type_or_else (BINFO_TYPE (binfo), NULL_TREE))
4854 return error_mark_node;
4856 fns = lookup_fnfields (binfo, name, 1);
4858 /* When making a call to a constructor or destructor for a subobject
4859 that uses virtual base classes, pass down a pointer to a VTT for
4861 if ((name == base_ctor_identifier
4862 || name == base_dtor_identifier)
4863 && TYPE_USES_VIRTUAL_BASECLASSES (class_type))
4868 /* If the current function is a complete object constructor
4869 or destructor, then we fetch the VTT directly.
4870 Otherwise, we look it up using the VTT we were given. */
4871 vtt = TREE_CHAIN (CLASSTYPE_VTABLES (current_class_type));
4872 vtt = decay_conversion (vtt);
4873 vtt = build (COND_EXPR, TREE_TYPE (vtt),
4874 build (EQ_EXPR, boolean_type_node,
4875 current_in_charge_parm, integer_zero_node),
4878 my_friendly_assert (BINFO_SUBVTT_INDEX (binfo), 20010110);
4879 sub_vtt = build (PLUS_EXPR, TREE_TYPE (vtt), vtt,
4880 BINFO_SUBVTT_INDEX (binfo));
4882 args = tree_cons (NULL_TREE, sub_vtt, args);
4885 return build_new_method_call (instance, fns, args,
4886 TYPE_BINFO (BINFO_TYPE (binfo)),
4890 /* Return the NAME, as a C string. The NAME indicates a function that
4891 is a member of TYPE. *FREE_P is set to true if the caller must
4892 free the memory returned.
4894 Rather than go through all of this, we should simply set the names
4895 of constructors and destructors appropriately, and dispense with
4896 ctor_identifier, dtor_identifier, etc. */
4899 name_as_c_string (tree name, tree type, bool *free_p)
4903 /* Assume that we will not allocate memory. */
4905 /* Constructors and destructors are special. */
4906 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
4909 = (char *) IDENTIFIER_POINTER (constructor_name (type));
4910 /* For a destructor, add the '~'. */
4911 if (name == complete_dtor_identifier
4912 || name == base_dtor_identifier
4913 || name == deleting_dtor_identifier)
4915 pretty_name = concat ("~", pretty_name, NULL);
4916 /* Remember that we need to free the memory allocated. */
4920 else if (IDENTIFIER_TYPENAME_P (name))
4922 pretty_name = concat ("operator ",
4923 type_as_string (TREE_TYPE (name),
4924 TFF_PLAIN_IDENTIFIER),
4926 /* Remember that we need to free the memory allocated. */
4930 pretty_name = (char *) IDENTIFIER_POINTER (name);
4935 /* Build a call to "INSTANCE.FN (ARGS)". */
4938 build_new_method_call (tree instance, tree fns, tree args,
4939 tree conversion_path, int flags)
4941 struct z_candidate *candidates = 0, *cand;
4942 tree explicit_targs = NULL_TREE;
4943 tree basetype = NULL_TREE;
4946 tree mem_args = NULL_TREE, instance_ptr;
4952 int template_only = 0;
4958 my_friendly_assert (instance != NULL_TREE, 20020729);
4960 if (error_operand_p (instance)
4961 || error_operand_p (fns)
4962 || args == error_mark_node)
4963 return error_mark_node;
4965 orig_instance = instance;
4969 if (processing_template_decl)
4971 instance = build_non_dependent_expr (instance);
4972 if (!BASELINK_P (fns)
4973 && TREE_CODE (fns) != PSEUDO_DTOR_EXPR
4974 && TREE_TYPE (fns) != unknown_type_node)
4975 fns = build_non_dependent_expr (fns);
4976 args = build_non_dependent_args (orig_args);
4979 /* Process the argument list. */
4981 args = resolve_args (args);
4982 if (args == error_mark_node)
4983 return error_mark_node;
4985 if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4986 instance = convert_from_reference (instance);
4987 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
4988 instance_ptr = build_this (instance);
4990 if (!BASELINK_P (fns))
4992 error ("call to non-function `%D'", fns);
4993 return error_mark_node;
4996 if (!conversion_path)
4997 conversion_path = BASELINK_BINFO (fns);
4998 access_binfo = BASELINK_ACCESS_BINFO (fns);
4999 optype = BASELINK_OPTYPE (fns);
5000 fns = BASELINK_FUNCTIONS (fns);
5002 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
5004 explicit_targs = TREE_OPERAND (fns, 1);
5005 fns = TREE_OPERAND (fns, 0);
5009 my_friendly_assert (TREE_CODE (fns) == FUNCTION_DECL
5010 || TREE_CODE (fns) == TEMPLATE_DECL
5011 || TREE_CODE (fns) == OVERLOAD,
5014 /* XXX this should be handled before we get here. */
5015 if (! IS_AGGR_TYPE (basetype))
5017 if ((flags & LOOKUP_COMPLAIN) && basetype != error_mark_node)
5018 error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
5019 fns, instance, basetype);
5021 return error_mark_node;
5024 fn = get_first_fn (fns);
5025 name = DECL_NAME (fn);
5027 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
5029 /* Callers should explicitly indicate whether they want to construct
5030 the complete object or just the part without virtual bases. */
5031 my_friendly_assert (name != ctor_identifier, 20000408);
5032 /* Similarly for destructors. */
5033 my_friendly_assert (name != dtor_identifier, 20000408);
5036 /* It's OK to call destructors on cv-qualified objects. Therefore,
5037 convert the INSTANCE_PTR to the unqualified type, if necessary. */
5038 if (DECL_DESTRUCTOR_P (fn))
5040 tree type = build_pointer_type (basetype);
5041 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
5042 instance_ptr = build_nop (type, instance_ptr);
5045 class_type = (conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE);
5046 mem_args = tree_cons (NULL_TREE, instance_ptr, args);
5048 for (fn = fns; fn; fn = OVL_NEXT (fn))
5050 tree t = OVL_CURRENT (fn);
5053 /* We can end up here for copy-init of same or base class. */
5054 if ((flags & LOOKUP_ONLYCONVERTING)
5055 && DECL_NONCONVERTING_P (t))
5058 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
5059 this_arglist = mem_args;
5061 this_arglist = args;
5063 if (TREE_CODE (t) == TEMPLATE_DECL)
5064 /* A member template. */
5065 add_template_candidate (&candidates, t,
5068 this_arglist, optype,
5073 else if (! template_only)
5074 add_function_candidate (&candidates, t,
5082 candidates = splice_viable (candidates, pedantic, &any_viable_p);
5085 /* XXX will LOOKUP_SPECULATIVELY be needed when this is done? */
5086 if (flags & LOOKUP_SPECULATIVELY)
5088 if (!COMPLETE_TYPE_P (basetype))
5089 cxx_incomplete_type_error (instance_ptr, basetype);
5095 pretty_name = name_as_c_string (name, basetype, &free_p);
5096 error ("no matching function for call to `%T::%s(%A)%#V'",
5097 basetype, pretty_name, user_args,
5098 TREE_TYPE (TREE_TYPE (instance_ptr)));
5102 print_z_candidates (candidates);
5103 return error_mark_node;
5106 cand = tourney (candidates);
5112 pretty_name = name_as_c_string (name, basetype, &free_p);
5113 error ("call of overloaded `%s(%A)' is ambiguous", pretty_name,
5115 print_z_candidates (candidates);
5118 return error_mark_node;
5121 if (DECL_PURE_VIRTUAL_P (cand->fn)
5122 && instance == current_class_ref
5123 && (DECL_CONSTRUCTOR_P (current_function_decl)
5124 || DECL_DESTRUCTOR_P (current_function_decl))
5125 && ! (flags & LOOKUP_NONVIRTUAL)
5126 && value_member (cand->fn, CLASSTYPE_PURE_VIRTUALS (basetype)))
5127 error ((DECL_CONSTRUCTOR_P (current_function_decl) ?
5128 "abstract virtual `%#D' called from constructor"
5129 : "abstract virtual `%#D' called from destructor"),
5131 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
5132 && is_dummy_object (instance_ptr))
5134 error ("cannot call member function `%D' without object", cand->fn);
5135 return error_mark_node;
5138 if (DECL_VINDEX (cand->fn) && ! (flags & LOOKUP_NONVIRTUAL)
5139 && resolves_to_fixed_type_p (instance, 0))
5140 flags |= LOOKUP_NONVIRTUAL;
5142 if (TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE)
5143 call = build_over_call (cand, flags);
5146 call = build_over_call (cand, flags);
5147 /* In an expression of the form `a->f()' where `f' turns out to
5148 be a static member function, `a' is none-the-less evaluated. */
5149 if (!is_dummy_object (instance_ptr) && TREE_SIDE_EFFECTS (instance))
5150 call = build (COMPOUND_EXPR, TREE_TYPE (call), instance, call);
5153 if (processing_template_decl && call != error_mark_node)
5154 return build_min_non_dep
5156 build_min_nt (COMPONENT_REF, orig_instance, orig_fns),
5161 /* Returns true iff standard conversion sequence ICS1 is a proper
5162 subsequence of ICS2. */
5165 is_subseq (tree ics1, tree ics2)
5167 /* We can assume that a conversion of the same code
5168 between the same types indicates a subsequence since we only get
5169 here if the types we are converting from are the same. */
5171 while (TREE_CODE (ics1) == RVALUE_CONV
5172 || TREE_CODE (ics1) == LVALUE_CONV)
5173 ics1 = TREE_OPERAND (ics1, 0);
5177 while (TREE_CODE (ics2) == RVALUE_CONV
5178 || TREE_CODE (ics2) == LVALUE_CONV)
5179 ics2 = TREE_OPERAND (ics2, 0);
5181 if (TREE_CODE (ics2) == USER_CONV
5182 || TREE_CODE (ics2) == AMBIG_CONV
5183 || TREE_CODE (ics2) == IDENTITY_CONV)
5184 /* At this point, ICS1 cannot be a proper subsequence of
5185 ICS2. We can get a USER_CONV when we are comparing the
5186 second standard conversion sequence of two user conversion
5190 ics2 = TREE_OPERAND (ics2, 0);
5192 if (TREE_CODE (ics2) == TREE_CODE (ics1)
5193 && same_type_p (TREE_TYPE (ics2), TREE_TYPE (ics1))
5194 && same_type_p (TREE_TYPE (TREE_OPERAND (ics2, 0)),
5195 TREE_TYPE (TREE_OPERAND (ics1, 0))))
5200 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
5201 be any _TYPE nodes. */
5204 is_properly_derived_from (tree derived, tree base)
5206 if (!IS_AGGR_TYPE_CODE (TREE_CODE (derived))
5207 || !IS_AGGR_TYPE_CODE (TREE_CODE (base)))
5210 /* We only allow proper derivation here. The DERIVED_FROM_P macro
5211 considers every class derived from itself. */
5212 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
5213 && DERIVED_FROM_P (base, derived));
5216 /* We build the ICS for an implicit object parameter as a pointer
5217 conversion sequence. However, such a sequence should be compared
5218 as if it were a reference conversion sequence. If ICS is the
5219 implicit conversion sequence for an implicit object parameter,
5220 modify it accordingly. */
5223 maybe_handle_implicit_object (tree *ics)
5225 if (ICS_THIS_FLAG (*ics))
5227 /* [over.match.funcs]
5229 For non-static member functions, the type of the
5230 implicit object parameter is "reference to cv X"
5231 where X is the class of which the function is a
5232 member and cv is the cv-qualification on the member
5233 function declaration. */
5235 tree reference_type;
5237 /* The `this' parameter is a pointer to a class type. Make the
5238 implicit conversion talk about a reference to that same class
5240 reference_type = TREE_TYPE (TREE_TYPE (*ics));
5241 reference_type = build_reference_type (reference_type);
5243 if (TREE_CODE (t) == QUAL_CONV)
5244 t = TREE_OPERAND (t, 0);
5245 if (TREE_CODE (t) == PTR_CONV)
5246 t = TREE_OPERAND (t, 0);
5247 t = build1 (IDENTITY_CONV, TREE_TYPE (TREE_TYPE (t)), NULL_TREE);
5248 t = direct_reference_binding (reference_type, t);
5253 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
5254 and return the type to which the reference refers. Otherwise,
5255 leave *ICS unchanged and return NULL_TREE. */
5258 maybe_handle_ref_bind (tree *ics)
5260 if (TREE_CODE (*ics) == REF_BIND)
5262 tree old_ics = *ics;
5263 tree type = TREE_TYPE (TREE_TYPE (old_ics));
5264 *ics = TREE_OPERAND (old_ics, 0);
5265 ICS_USER_FLAG (*ics) = ICS_USER_FLAG (old_ics);
5266 ICS_BAD_FLAG (*ics) = ICS_BAD_FLAG (old_ics);
5273 /* Compare two implicit conversion sequences according to the rules set out in
5274 [over.ics.rank]. Return values:
5276 1: ics1 is better than ics2
5277 -1: ics2 is better than ics1
5278 0: ics1 and ics2 are indistinguishable */
5281 compare_ics (tree ics1, tree ics2)
5287 tree deref_from_type1 = NULL_TREE;
5288 tree deref_from_type2 = NULL_TREE;
5289 tree deref_to_type1 = NULL_TREE;
5290 tree deref_to_type2 = NULL_TREE;
5293 /* REF_BINDING is nonzero if the result of the conversion sequence
5294 is a reference type. In that case TARGET_TYPE is the
5295 type referred to by the reference. */
5299 /* Handle implicit object parameters. */
5300 maybe_handle_implicit_object (&ics1);
5301 maybe_handle_implicit_object (&ics2);
5303 /* Handle reference parameters. */
5304 target_type1 = maybe_handle_ref_bind (&ics1);
5305 target_type2 = maybe_handle_ref_bind (&ics2);
5309 When comparing the basic forms of implicit conversion sequences (as
5310 defined in _over.best.ics_)
5312 --a standard conversion sequence (_over.ics.scs_) is a better
5313 conversion sequence than a user-defined conversion sequence
5314 or an ellipsis conversion sequence, and
5316 --a user-defined conversion sequence (_over.ics.user_) is a
5317 better conversion sequence than an ellipsis conversion sequence
5318 (_over.ics.ellipsis_). */
5319 rank1 = ICS_RANK (ics1);
5320 rank2 = ICS_RANK (ics2);
5324 else if (rank1 < rank2)
5327 if (rank1 == BAD_RANK)
5329 /* XXX Isn't this an extension? */
5330 /* Both ICS are bad. We try to make a decision based on what
5331 would have happened if they'd been good. */
5332 if (ICS_USER_FLAG (ics1) > ICS_USER_FLAG (ics2)
5333 || ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
5335 else if (ICS_USER_FLAG (ics1) < ICS_USER_FLAG (ics2)
5336 || ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5339 /* We couldn't make up our minds; try to figure it out below. */
5342 if (ICS_ELLIPSIS_FLAG (ics1))
5343 /* Both conversions are ellipsis conversions. */
5346 /* User-defined conversion sequence U1 is a better conversion sequence
5347 than another user-defined conversion sequence U2 if they contain the
5348 same user-defined conversion operator or constructor and if the sec-
5349 ond standard conversion sequence of U1 is better than the second
5350 standard conversion sequence of U2. */
5352 if (ICS_USER_FLAG (ics1))
5356 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
5357 if (TREE_CODE (t1) == AMBIG_CONV)
5359 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
5360 if (TREE_CODE (t2) == AMBIG_CONV)
5363 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
5366 /* We can just fall through here, after setting up
5367 FROM_TYPE1 and FROM_TYPE2. */
5368 from_type1 = TREE_TYPE (t1);
5369 from_type2 = TREE_TYPE (t2);
5373 /* We're dealing with two standard conversion sequences.
5377 Standard conversion sequence S1 is a better conversion
5378 sequence than standard conversion sequence S2 if
5380 --S1 is a proper subsequence of S2 (comparing the conversion
5381 sequences in the canonical form defined by _over.ics.scs_,
5382 excluding any Lvalue Transformation; the identity
5383 conversion sequence is considered to be a subsequence of
5384 any non-identity conversion sequence */
5387 while (TREE_CODE (from_type1) != IDENTITY_CONV)
5388 from_type1 = TREE_OPERAND (from_type1, 0);
5389 from_type1 = TREE_TYPE (from_type1);
5392 while (TREE_CODE (from_type2) != IDENTITY_CONV)
5393 from_type2 = TREE_OPERAND (from_type2, 0);
5394 from_type2 = TREE_TYPE (from_type2);
5397 if (same_type_p (from_type1, from_type2))
5399 if (is_subseq (ics1, ics2))
5401 if (is_subseq (ics2, ics1))
5404 /* Otherwise, one sequence cannot be a subsequence of the other; they
5405 don't start with the same type. This can happen when comparing the
5406 second standard conversion sequence in two user-defined conversion
5413 --the rank of S1 is better than the rank of S2 (by the rules
5416 Standard conversion sequences are ordered by their ranks: an Exact
5417 Match is a better conversion than a Promotion, which is a better
5418 conversion than a Conversion.
5420 Two conversion sequences with the same rank are indistinguishable
5421 unless one of the following rules applies:
5423 --A conversion that is not a conversion of a pointer, or pointer
5424 to member, to bool is better than another conversion that is such
5427 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
5428 so that we do not have to check it explicitly. */
5429 if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
5431 else if (ICS_STD_RANK (ics2) < ICS_STD_RANK (ics1))
5434 to_type1 = TREE_TYPE (ics1);
5435 to_type2 = TREE_TYPE (ics2);
5437 if (TYPE_PTR_P (from_type1)
5438 && TYPE_PTR_P (from_type2)
5439 && TYPE_PTR_P (to_type1)
5440 && TYPE_PTR_P (to_type2))
5442 deref_from_type1 = TREE_TYPE (from_type1);
5443 deref_from_type2 = TREE_TYPE (from_type2);
5444 deref_to_type1 = TREE_TYPE (to_type1);
5445 deref_to_type2 = TREE_TYPE (to_type2);
5447 /* The rules for pointers to members A::* are just like the rules
5448 for pointers A*, except opposite: if B is derived from A then
5449 A::* converts to B::*, not vice versa. For that reason, we
5450 switch the from_ and to_ variables here. */
5451 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
5452 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
5453 || (TYPE_PTRMEMFUNC_P (from_type1)
5454 && TYPE_PTRMEMFUNC_P (from_type2)
5455 && TYPE_PTRMEMFUNC_P (to_type1)
5456 && TYPE_PTRMEMFUNC_P (to_type2)))
5458 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
5459 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
5460 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
5461 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
5464 if (deref_from_type1 != NULL_TREE
5465 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type1))
5466 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_from_type2)))
5468 /* This was one of the pointer or pointer-like conversions.
5472 --If class B is derived directly or indirectly from class A,
5473 conversion of B* to A* is better than conversion of B* to
5474 void*, and conversion of A* to void* is better than
5475 conversion of B* to void*. */
5476 if (TREE_CODE (deref_to_type1) == VOID_TYPE
5477 && TREE_CODE (deref_to_type2) == VOID_TYPE)
5479 if (is_properly_derived_from (deref_from_type1,
5482 else if (is_properly_derived_from (deref_from_type2,
5486 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
5487 || TREE_CODE (deref_to_type2) == VOID_TYPE)
5489 if (same_type_p (deref_from_type1, deref_from_type2))
5491 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
5493 if (is_properly_derived_from (deref_from_type1,
5497 /* We know that DEREF_TO_TYPE1 is `void' here. */
5498 else if (is_properly_derived_from (deref_from_type1,
5503 else if (IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type1))
5504 && IS_AGGR_TYPE_CODE (TREE_CODE (deref_to_type2)))
5508 --If class B is derived directly or indirectly from class A
5509 and class C is derived directly or indirectly from B,
5511 --conversion of C* to B* is better than conversion of C* to
5514 --conversion of B* to A* is better than conversion of C* to
5516 if (same_type_p (deref_from_type1, deref_from_type2))
5518 if (is_properly_derived_from (deref_to_type1,
5521 else if (is_properly_derived_from (deref_to_type2,
5525 else if (same_type_p (deref_to_type1, deref_to_type2))
5527 if (is_properly_derived_from (deref_from_type2,
5530 else if (is_properly_derived_from (deref_from_type1,
5536 else if (CLASS_TYPE_P (non_reference (from_type1))
5537 && same_type_p (from_type1, from_type2))
5539 tree from = non_reference (from_type1);
5543 --binding of an expression of type C to a reference of type
5544 B& is better than binding an expression of type C to a
5545 reference of type A&
5547 --conversion of C to B is better than conversion of C to A, */
5548 if (is_properly_derived_from (from, to_type1)
5549 && is_properly_derived_from (from, to_type2))
5551 if (is_properly_derived_from (to_type1, to_type2))
5553 else if (is_properly_derived_from (to_type2, to_type1))
5557 else if (CLASS_TYPE_P (non_reference (to_type1))
5558 && same_type_p (to_type1, to_type2))
5560 tree to = non_reference (to_type1);
5564 --binding of an expression of type B to a reference of type
5565 A& is better than binding an expression of type C to a
5566 reference of type A&,
5568 --onversion of B to A is better than conversion of C to A */
5569 if (is_properly_derived_from (from_type1, to)
5570 && is_properly_derived_from (from_type2, to))
5572 if (is_properly_derived_from (from_type2, from_type1))
5574 else if (is_properly_derived_from (from_type1, from_type2))
5581 --S1 and S2 differ only in their qualification conversion and yield
5582 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
5583 qualification signature of type T1 is a proper subset of the cv-
5584 qualification signature of type T2 */
5585 if (TREE_CODE (ics1) == QUAL_CONV
5586 && TREE_CODE (ics2) == QUAL_CONV
5587 && same_type_p (from_type1, from_type2))
5588 return comp_cv_qual_signature (to_type1, to_type2);
5592 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
5593 types to which the references refer are the same type except for
5594 top-level cv-qualifiers, and the type to which the reference
5595 initialized by S2 refers is more cv-qualified than the type to
5596 which the reference initialized by S1 refers */
5598 if (target_type1 && target_type2
5599 && same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
5600 return comp_cv_qualification (target_type2, target_type1);
5602 /* Neither conversion sequence is better than the other. */
5606 /* The source type for this standard conversion sequence. */
5609 source_type (tree t)
5611 for (;; t = TREE_OPERAND (t, 0))
5613 if (TREE_CODE (t) == USER_CONV
5614 || TREE_CODE (t) == AMBIG_CONV
5615 || TREE_CODE (t) == IDENTITY_CONV)
5616 return TREE_TYPE (t);
5621 /* Note a warning about preferring WINNER to LOSER. We do this by storing
5622 a pointer to LOSER and re-running joust to produce the warning if WINNER
5623 is actually used. */
5626 add_warning (struct z_candidate *winner, struct z_candidate *loser)
5628 winner->warnings = tree_cons (NULL_TREE,
5629 build_zc_wrapper (loser),
5633 /* Compare two candidates for overloading as described in
5634 [over.match.best]. Return values:
5636 1: cand1 is better than cand2
5637 -1: cand2 is better than cand1
5638 0: cand1 and cand2 are indistinguishable */
5641 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
5644 int i, off1 = 0, off2 = 0, len;
5646 /* Candidates that involve bad conversions are always worse than those
5648 if (cand1->viable > cand2->viable)
5650 if (cand1->viable < cand2->viable)
5653 /* If we have two pseudo-candidates for conversions to the same type,
5654 or two candidates for the same function, arbitrarily pick one. */
5655 if (cand1->fn == cand2->fn
5656 && (TYPE_P (cand1->fn) || DECL_P (cand1->fn)))
5659 /* a viable function F1
5660 is defined to be a better function than another viable function F2 if
5661 for all arguments i, ICSi(F1) is not a worse conversion sequence than
5662 ICSi(F2), and then */
5664 /* for some argument j, ICSj(F1) is a better conversion sequence than
5667 /* For comparing static and non-static member functions, we ignore
5668 the implicit object parameter of the non-static function. The
5669 standard says to pretend that the static function has an object
5670 parm, but that won't work with operator overloading. */
5671 len = TREE_VEC_LENGTH (cand1->convs);
5672 if (len != TREE_VEC_LENGTH (cand2->convs))
5674 if (DECL_STATIC_FUNCTION_P (cand1->fn)
5675 && ! DECL_STATIC_FUNCTION_P (cand2->fn))
5677 else if (! DECL_STATIC_FUNCTION_P (cand1->fn)
5678 && DECL_STATIC_FUNCTION_P (cand2->fn))
5687 for (i = 0; i < len; ++i)
5689 tree t1 = TREE_VEC_ELT (cand1->convs, i+off1);
5690 tree t2 = TREE_VEC_ELT (cand2->convs, i+off2);
5691 int comp = compare_ics (t1, t2);
5696 && ICS_RANK (t1) + ICS_RANK (t2) == STD_RANK + PROMO_RANK
5697 && TREE_CODE (t1) == STD_CONV
5698 && TREE_CODE (t2) == STD_CONV
5699 && TREE_CODE (TREE_TYPE (t1)) == INTEGER_TYPE
5700 && TREE_CODE (TREE_TYPE (t2)) == INTEGER_TYPE
5701 && (TYPE_PRECISION (TREE_TYPE (t1))
5702 == TYPE_PRECISION (TREE_TYPE (t2)))
5703 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t1, 0)))
5704 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (t1, 0)))
5707 tree type = TREE_TYPE (TREE_OPERAND (t1, 0));
5709 struct z_candidate *w, *l;
5711 type1 = TREE_TYPE (t1), type2 = TREE_TYPE (t2),
5712 w = cand1, l = cand2;
5714 type1 = TREE_TYPE (t2), type2 = TREE_TYPE (t1),
5715 w = cand2, l = cand1;
5719 warning ("passing `%T' chooses `%T' over `%T'",
5720 type, type1, type2);
5721 warning (" in call to `%D'", w->fn);
5727 if (winner && comp != winner)
5736 /* warn about confusing overload resolution for user-defined conversions,
5737 either between a constructor and a conversion op, or between two
5739 if (winner && warn_conversion && cand1->second_conv
5740 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
5741 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
5743 struct z_candidate *w, *l;
5744 bool give_warning = false;
5747 w = cand1, l = cand2;
5749 w = cand2, l = cand1;
5751 /* We don't want to complain about `X::operator T1 ()'
5752 beating `X::operator T2 () const', when T2 is a no less
5753 cv-qualified version of T1. */
5754 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
5755 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
5757 tree t = TREE_TYPE (TREE_TYPE (l->fn));
5758 tree f = TREE_TYPE (TREE_TYPE (w->fn));
5760 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
5765 if (!comp_ptr_ttypes (t, f))
5766 give_warning = true;
5769 give_warning = true;
5775 tree source = source_type (TREE_VEC_ELT (w->convs, 0));
5776 if (! DECL_CONSTRUCTOR_P (w->fn))
5777 source = TREE_TYPE (source);
5778 warning ("choosing `%D' over `%D'", w->fn, l->fn);
5779 warning (" for conversion from `%T' to `%T'",
5780 source, TREE_TYPE (w->second_conv));
5781 warning (" because conversion sequence for the argument is better");
5791 F1 is a non-template function and F2 is a template function
5794 if (! cand1->template && cand2->template)
5796 else if (cand1->template && ! cand2->template)
5800 F1 and F2 are template functions and the function template for F1 is
5801 more specialized than the template for F2 according to the partial
5804 if (cand1->template && cand2->template)
5806 winner = more_specialized
5807 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template),
5809 /* Tell the deduction code how many real function arguments
5810 we saw, not counting the implicit 'this' argument. But,
5811 add_function_candidate() suppresses the "this" argument
5814 [temp.func.order]: The presence of unused ellipsis and default
5815 arguments has no effect on the partial ordering of function
5817 TREE_VEC_LENGTH (cand1->convs)
5818 - (DECL_NONSTATIC_MEMBER_FUNCTION_P (cand1->fn)
5819 - DECL_CONSTRUCTOR_P (cand1->fn)));
5825 the context is an initialization by user-defined conversion (see
5826 _dcl.init_ and _over.match.user_) and the standard conversion
5827 sequence from the return type of F1 to the destination type (i.e.,
5828 the type of the entity being initialized) is a better conversion
5829 sequence than the standard conversion sequence from the return type
5830 of F2 to the destination type. */
5832 if (cand1->second_conv)
5834 winner = compare_ics (cand1->second_conv, cand2->second_conv);
5839 /* Check whether we can discard a builtin candidate, either because we
5840 have two identical ones or matching builtin and non-builtin candidates.
5842 (Pedantically in the latter case the builtin which matched the user
5843 function should not be added to the overload set, but we spot it here.
5846 ... the builtin candidates include ...
5847 - do not have the same parameter type list as any non-template
5848 non-member candidate. */
5850 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
5851 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
5853 for (i = 0; i < len; ++i)
5854 if (!same_type_p (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
5855 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i))))
5857 if (i == TREE_VEC_LENGTH (cand1->convs))
5859 if (cand1->fn == cand2->fn)
5860 /* Two built-in candidates; arbitrarily pick one. */
5862 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
5863 /* cand1 is built-in; prefer cand2. */
5866 /* cand2 is built-in; prefer cand1. */
5871 /* If the two functions are the same (this can happen with declarations
5872 in multiple scopes and arg-dependent lookup), arbitrarily choose one. */
5873 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
5874 && equal_functions (cand1->fn, cand2->fn))
5879 /* Extension: If the worst conversion for one candidate is worse than the
5880 worst conversion for the other, take the first. */
5883 int rank1 = IDENTITY_RANK, rank2 = IDENTITY_RANK;
5884 struct z_candidate *w = 0, *l = 0;
5886 for (i = 0; i < len; ++i)
5888 if (ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1)) > rank1)
5889 rank1 = ICS_RANK (TREE_VEC_ELT (cand1->convs, i+off1));
5890 if (ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2)) > rank2)
5891 rank2 = ICS_RANK (TREE_VEC_ELT (cand2->convs, i+off2));
5894 winner = 1, w = cand1, l = cand2;
5896 winner = -1, w = cand2, l = cand1;
5902 ISO C++ says that these are ambiguous, even \
5903 though the worst conversion for the first is better than \
5904 the worst conversion for the second:");
5905 print_z_candidate (_("candidate 1:"), w);
5906 print_z_candidate (_("candidate 2:"), l);
5914 my_friendly_assert (!winner, 20010121);
5918 /* Given a list of candidates for overloading, find the best one, if any.
5919 This algorithm has a worst case of O(2n) (winner is last), and a best
5920 case of O(n/2) (totally ambiguous); much better than a sorting
5923 static struct z_candidate *
5924 tourney (struct z_candidate *candidates)
5926 struct z_candidate *champ = candidates, *challenger;
5928 int champ_compared_to_predecessor = 0;
5930 /* Walk through the list once, comparing each current champ to the next
5931 candidate, knocking out a candidate or two with each comparison. */
5933 for (challenger = champ->next; challenger; )
5935 fate = joust (champ, challenger, 0);
5937 challenger = challenger->next;
5942 champ = challenger->next;
5945 champ_compared_to_predecessor = 0;
5950 champ_compared_to_predecessor = 1;
5953 challenger = champ->next;
5957 /* Make sure the champ is better than all the candidates it hasn't yet
5958 been compared to. */
5960 for (challenger = candidates;
5962 && !(champ_compared_to_predecessor && challenger->next == champ);
5963 challenger = challenger->next)
5965 fate = joust (champ, challenger, 0);
5973 /* Returns nonzero if things of type FROM can be converted to TO. */
5976 can_convert (tree to, tree from)
5978 return can_convert_arg (to, from, NULL_TREE);
5981 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
5984 can_convert_arg (tree to, tree from, tree arg)
5986 tree t = implicit_conversion (to, from, arg, LOOKUP_NORMAL);
5987 return (t && ! ICS_BAD_FLAG (t));
5990 /* Like can_convert_arg, but allows dubious conversions as well. */
5993 can_convert_arg_bad (tree to, tree from, tree arg)
5995 return implicit_conversion (to, from, arg, LOOKUP_NORMAL) != 0;
5998 /* Convert EXPR to TYPE. Return the converted expression.
6000 Note that we allow bad conversions here because by the time we get to
6001 this point we are committed to doing the conversion. If we end up
6002 doing a bad conversion, convert_like will complain. */
6005 perform_implicit_conversion (tree type, tree expr)
6009 if (error_operand_p (expr))
6010 return error_mark_node;
6011 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6015 error ("could not convert `%E' to `%T'", expr, type);
6016 return error_mark_node;
6019 return convert_like (conv, expr);
6022 /* Convert EXPR to TYPE (as a direct-initialization) if that is
6023 permitted. If the conversion is valid, the converted expression is
6024 returned. Otherwise, NULL_TREE is returned, except in the case
6025 that TYPE is a class type; in that case, an error is issued. */
6028 perform_direct_initialization_if_possible (tree type, tree expr)
6032 if (type == error_mark_node || error_operand_p (expr))
6033 return error_mark_node;
6036 If the destination type is a (possibly cv-qualified) class type:
6038 -- If the initialization is direct-initialization ...,
6039 constructors are considered. ... If no constructor applies, or
6040 the overload resolution is ambiguous, the initialization is
6042 if (CLASS_TYPE_P (type))
6044 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6045 build_tree_list (NULL_TREE, expr),
6048 return build_cplus_new (type, expr);
6050 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
6052 if (!conv || ICS_BAD_FLAG (conv))
6054 return convert_like_real (conv, expr, NULL_TREE, 0, 0,
6055 /*issue_conversion_warnings=*/false);
6058 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
6059 is being bound to a temporary. Create and return a new VAR_DECL
6060 with the indicated TYPE; this variable will store the value to
6061 which the reference is bound. */
6064 make_temporary_var_for_ref_to_temp (tree decl, tree type)
6068 /* Create the variable. */
6069 var = build_decl (VAR_DECL, NULL_TREE, type);
6070 DECL_ARTIFICIAL (var) = 1;
6071 TREE_USED (var) = 1;
6073 /* Register the variable. */
6074 if (TREE_STATIC (decl))
6076 /* Namespace-scope or local static; give it a mangled name. */
6079 TREE_STATIC (var) = 1;
6080 name = mangle_ref_init_variable (decl);
6081 DECL_NAME (var) = name;
6082 SET_DECL_ASSEMBLER_NAME (var, name);
6083 var = pushdecl_top_level (var);
6087 /* Create a new cleanup level if necessary. */
6088 maybe_push_cleanup_level (type);
6089 /* Don't push unnamed temps. Do set DECL_CONTEXT, though. */
6090 DECL_CONTEXT (var) = current_function_decl;
6096 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
6097 initializing a variable of that TYPE. If DECL is non-NULL, it is
6098 the VAR_DECL being initialized with the EXPR. (In that case, the
6099 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
6100 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
6101 return, if *CLEANUP is no longer NULL, it will be a CLEANUP_STMT
6102 that should be inserted after the returned expression is used to
6105 Return the converted expression. */
6108 initialize_reference (tree type, tree expr, tree decl, tree *cleanup)
6112 if (type == error_mark_node || error_operand_p (expr))
6113 return error_mark_node;
6115 conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
6116 if (!conv || ICS_BAD_FLAG (conv))
6118 if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
6119 && !real_lvalue_p (expr))
6120 error ("invalid initialization of non-const reference of "
6121 "type '%T' from a temporary of type '%T'",
6122 type, TREE_TYPE (expr));
6124 error ("invalid initialization of reference of type "
6125 "'%T' from expression of type '%T'", type,
6127 return error_mark_node;
6130 /* If DECL is non-NULL, then this special rule applies:
6134 The temporary to which the reference is bound or the temporary
6135 that is the complete object to which the reference is bound
6136 persists for the lifetime of the reference.
6138 The temporaries created during the evaluation of the expression
6139 initializing the reference, except the temporary to which the
6140 reference is bound, are destroyed at the end of the
6141 full-expression in which they are created.
6143 In that case, we store the converted expression into a new
6144 VAR_DECL in a new scope.
6146 However, we want to be careful not to create temporaries when
6147 they are not required. For example, given:
6150 struct D : public B {};
6154 there is no need to copy the return value from "f"; we can just
6155 extend its lifetime. Similarly, given:
6158 struct T { operator S(); };
6162 we can extend the lifetime of the return value of the conversion
6164 my_friendly_assert (TREE_CODE (conv) == REF_BIND, 20030302);
6168 tree base_conv_type;
6170 /* Skip over the REF_BIND. */
6171 conv = TREE_OPERAND (conv, 0);
6172 /* If the next conversion is a BASE_CONV, skip that too -- but
6173 remember that the conversion was required. */
6174 if (TREE_CODE (conv) == BASE_CONV && !NEED_TEMPORARY_P (conv))
6176 if (CHECK_COPY_CONSTRUCTOR_P (conv))
6177 check_constructor_callable (TREE_TYPE (expr), expr);
6178 base_conv_type = TREE_TYPE (conv);
6179 conv = TREE_OPERAND (conv, 0);
6182 base_conv_type = NULL_TREE;
6183 /* Perform the remainder of the conversion. */
6184 expr = convert_like_real (conv, expr,
6185 /*fn=*/NULL_TREE, /*argnum=*/0,
6187 /*issue_conversion_warnings=*/true);
6188 if (error_operand_p (expr))
6189 return error_mark_node;
6190 if (!real_lvalue_p (expr))
6195 /* Create the temporary variable. */
6196 type = TREE_TYPE (expr);
6197 var = make_temporary_var_for_ref_to_temp (decl, type);
6198 layout_decl (var, 0);
6199 /* If the rvalue is the result of a function call it will be
6200 a TARGET_EXPR. If it is some other construct (such as a
6201 member access expression where the underlying object is
6202 itself the result of a function call), turn it into a
6203 TARGET_EXPR here. It is important that EXPR be a
6204 TARGET_EXPR below since otherwise the INIT_EXPR will
6205 attempt to make a bitwise copy of EXPR to intialize
6207 if (TREE_CODE (expr) != TARGET_EXPR)
6208 expr = get_target_expr (expr);
6209 /* Create the INIT_EXPR that will initialize the temporary
6211 init = build (INIT_EXPR, type, var, expr);
6212 if (at_function_scope_p ())
6214 add_decl_stmt (var);
6215 *cleanup = cxx_maybe_build_cleanup (var);
6217 /* We must be careful to destroy the temporary only
6218 after its initialization has taken place. If the
6219 initialization throws an exception, then the
6220 destructor should not be run. We cannot simply
6221 transform INIT into something like:
6223 (INIT, ({ CLEANUP_STMT; }))
6225 because emit_local_var always treats the
6226 initializer as a full-expression. Thus, the
6227 destructor would run too early; it would run at the
6228 end of initializing the reference variable, rather
6229 than at the end of the block enclosing the
6232 The solution is to pass back a CLEANUP_STMT which
6233 the caller is responsible for attaching to the
6235 *cleanup = build_stmt (CLEANUP_STMT, var, *cleanup);
6239 rest_of_decl_compilation (var, NULL, /*toplev=*/1, at_eof);
6240 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6241 static_aggregates = tree_cons (NULL_TREE, var,
6244 /* Use its address to initialize the reference variable. */
6245 expr = build_address (var);
6246 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
6249 /* Take the address of EXPR. */
6250 expr = build_unary_op (ADDR_EXPR, expr, 0);
6251 /* If a BASE_CONV was required, perform it now. */
6253 expr = (perform_implicit_conversion
6254 (build_pointer_type (base_conv_type), expr));
6255 return build_nop (type, expr);
6258 /* Perform the conversion. */
6259 return convert_like (conv, expr);
6262 #include "gt-cp-call.h"