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, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com) and
6 modified by Brendan Kehoe (brendan@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* High-level class interface. */
29 #include "coretypes.h"
36 #include "diagnostic-core.h"
40 #include "langhooks.h"
42 /* The various kinds of conversion. */
44 typedef enum conversion_kind {
60 /* The rank of the conversion. Order of the enumerals matters; better
61 conversions should come earlier in the list. */
63 typedef enum conversion_rank {
74 /* An implicit conversion sequence, in the sense of [over.best.ics].
75 The first conversion to be performed is at the end of the chain.
76 That conversion is always a cr_identity conversion. */
78 typedef struct conversion conversion;
80 /* The kind of conversion represented by this step. */
82 /* The rank of this conversion. */
84 BOOL_BITFIELD user_conv_p : 1;
85 BOOL_BITFIELD ellipsis_p : 1;
86 BOOL_BITFIELD this_p : 1;
87 BOOL_BITFIELD bad_p : 1;
88 /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
89 temporary should be created to hold the result of the
91 BOOL_BITFIELD need_temporary_p : 1;
92 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
93 from a pointer-to-derived to pointer-to-base is being performed. */
94 BOOL_BITFIELD base_p : 1;
95 /* If KIND is ck_ref_bind, true when either an lvalue reference is
96 being bound to an lvalue expression or an rvalue reference is
97 being bound to an rvalue expression. */
98 BOOL_BITFIELD rvaluedness_matches_p: 1;
99 BOOL_BITFIELD check_narrowing: 1;
100 /* The type of the expression resulting from the conversion. */
103 /* The next conversion in the chain. Since the conversions are
104 arranged from outermost to innermost, the NEXT conversion will
105 actually be performed before this conversion. This variant is
106 used only when KIND is neither ck_identity nor ck_ambig. */
108 /* The expression at the beginning of the conversion chain. This
109 variant is used only if KIND is ck_identity or ck_ambig. */
111 /* The array of conversions for an initializer_list. */
114 /* The function candidate corresponding to this conversion
115 sequence. This field is only used if KIND is ck_user. */
116 struct z_candidate *cand;
119 #define CONVERSION_RANK(NODE) \
120 ((NODE)->bad_p ? cr_bad \
121 : (NODE)->ellipsis_p ? cr_ellipsis \
122 : (NODE)->user_conv_p ? cr_user \
125 #define BAD_CONVERSION_RANK(NODE) \
126 ((NODE)->ellipsis_p ? cr_ellipsis \
127 : (NODE)->user_conv_p ? cr_user \
130 static struct obstack conversion_obstack;
131 static bool conversion_obstack_initialized;
133 static struct z_candidate * tourney (struct z_candidate *);
134 static int equal_functions (tree, tree);
135 static int joust (struct z_candidate *, struct z_candidate *, bool);
136 static int compare_ics (conversion *, conversion *);
137 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
138 static tree build_java_interface_fn_ref (tree, tree);
139 #define convert_like(CONV, EXPR, COMPLAIN) \
140 convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
141 /*issue_conversion_warnings=*/true, \
142 /*c_cast_p=*/false, (COMPLAIN))
143 #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
144 convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
145 /*issue_conversion_warnings=*/true, \
146 /*c_cast_p=*/false, (COMPLAIN))
147 static tree convert_like_real (conversion *, tree, tree, int, int, bool,
148 bool, tsubst_flags_t);
149 static void op_error (enum tree_code, enum tree_code, tree, tree,
151 static VEC(tree,gc) *resolve_args (VEC(tree,gc) *);
152 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int);
153 static void print_z_candidate (const char *, struct z_candidate *);
154 static void print_z_candidates (struct z_candidate *);
155 static tree build_this (tree);
156 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
157 static bool any_strictly_viable (struct z_candidate *);
158 static struct z_candidate *add_template_candidate
159 (struct z_candidate **, tree, tree, tree, tree, const VEC(tree,gc) *,
160 tree, tree, tree, int, unification_kind_t);
161 static struct z_candidate *add_template_candidate_real
162 (struct z_candidate **, tree, tree, tree, tree, const VEC(tree,gc) *,
163 tree, tree, tree, int, tree, unification_kind_t);
164 static struct z_candidate *add_template_conv_candidate
165 (struct z_candidate **, tree, tree, tree, const VEC(tree,gc) *, tree,
167 static void add_builtin_candidates
168 (struct z_candidate **, enum tree_code, enum tree_code,
170 static void add_builtin_candidate
171 (struct z_candidate **, enum tree_code, enum tree_code,
172 tree, tree, tree, tree *, tree *, int);
173 static bool is_complete (tree);
174 static void build_builtin_candidate
175 (struct z_candidate **, tree, tree, tree, tree *, tree *,
177 static struct z_candidate *add_conv_candidate
178 (struct z_candidate **, tree, tree, tree, const VEC(tree,gc) *, tree,
180 static struct z_candidate *add_function_candidate
181 (struct z_candidate **, tree, tree, tree, const VEC(tree,gc) *, tree,
183 static conversion *implicit_conversion (tree, tree, tree, bool, int);
184 static conversion *standard_conversion (tree, tree, tree, bool, int);
185 static conversion *reference_binding (tree, tree, tree, bool, int);
186 static conversion *build_conv (conversion_kind, tree, conversion *);
187 static conversion *build_list_conv (tree, tree, int);
188 static bool is_subseq (conversion *, conversion *);
189 static conversion *maybe_handle_ref_bind (conversion **);
190 static void maybe_handle_implicit_object (conversion **);
191 static struct z_candidate *add_candidate
192 (struct z_candidate **, tree, tree, const VEC(tree,gc) *, size_t,
193 conversion **, tree, tree, int);
194 static tree source_type (conversion *);
195 static void add_warning (struct z_candidate *, struct z_candidate *);
196 static bool reference_compatible_p (tree, tree);
197 static conversion *convert_class_to_reference (tree, tree, tree, int);
198 static conversion *direct_reference_binding (tree, conversion *);
199 static bool promoted_arithmetic_type_p (tree);
200 static conversion *conditional_conversion (tree, tree);
201 static char *name_as_c_string (tree, tree, bool *);
202 static tree prep_operand (tree);
203 static void add_candidates (tree, tree, const VEC(tree,gc) *, tree, tree, bool,
204 tree, tree, int, struct z_candidate **);
205 static conversion *merge_conversion_sequences (conversion *, conversion *);
206 static bool magic_varargs_p (tree);
207 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
209 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
210 NAME can take many forms... */
213 check_dtor_name (tree basetype, tree name)
215 /* Just accept something we've already complained about. */
216 if (name == error_mark_node)
219 if (TREE_CODE (name) == TYPE_DECL)
220 name = TREE_TYPE (name);
221 else if (TYPE_P (name))
223 else if (TREE_CODE (name) == IDENTIFIER_NODE)
225 if ((MAYBE_CLASS_TYPE_P (basetype)
226 && name == constructor_name (basetype))
227 || (TREE_CODE (basetype) == ENUMERAL_TYPE
228 && name == TYPE_IDENTIFIER (basetype)))
231 name = get_type_value (name);
237 template <class T> struct S { ~S(); };
241 NAME will be a class template. */
242 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
246 if (!name || name == error_mark_node)
248 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
251 /* We want the address of a function or method. We avoid creating a
252 pointer-to-member function. */
255 build_addr_func (tree function)
257 tree type = TREE_TYPE (function);
259 /* We have to do these by hand to avoid real pointer to member
261 if (TREE_CODE (type) == METHOD_TYPE)
263 if (TREE_CODE (function) == OFFSET_REF)
265 tree object = build_address (TREE_OPERAND (function, 0));
266 return get_member_function_from_ptrfunc (&object,
267 TREE_OPERAND (function, 1));
269 function = build_address (function);
272 function = decay_conversion (function);
277 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
278 POINTER_TYPE to those. Note, pointer to member function types
279 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
280 two variants. build_call_a is the primitive taking an array of
281 arguments, while build_call_n is a wrapper that handles varargs. */
284 build_call_n (tree function, int n, ...)
287 return build_call_a (function, 0, NULL);
290 tree *argarray = XALLOCAVEC (tree, n);
295 for (i = 0; i < n; i++)
296 argarray[i] = va_arg (ap, tree);
298 return build_call_a (function, n, argarray);
303 build_call_a (tree function, int n, tree *argarray)
305 int is_constructor = 0;
312 function = build_addr_func (function);
314 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
315 fntype = TREE_TYPE (TREE_TYPE (function));
316 gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
317 || TREE_CODE (fntype) == METHOD_TYPE);
318 result_type = TREE_TYPE (fntype);
319 /* An rvalue has no cv-qualifiers. */
320 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
321 result_type = cv_unqualified (result_type);
323 if (TREE_CODE (function) == ADDR_EXPR
324 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
326 decl = TREE_OPERAND (function, 0);
327 if (!TREE_USED (decl))
329 /* We invoke build_call directly for several library
330 functions. These may have been declared normally if
331 we're building libgcc, so we can't just check
333 gcc_assert (DECL_ARTIFICIAL (decl)
334 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
342 /* We check both the decl and the type; a function may be known not to
343 throw without being declared throw(). */
344 nothrow = ((decl && TREE_NOTHROW (decl))
345 || TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (function))));
347 if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
348 current_function_returns_abnormally = 1;
350 if (decl && TREE_DEPRECATED (decl))
351 warn_deprecated_use (decl, NULL_TREE);
352 require_complete_eh_spec_types (fntype, decl);
354 if (decl && DECL_CONSTRUCTOR_P (decl))
357 /* Don't pass empty class objects by value. This is useful
358 for tags in STL, which are used to control overload resolution.
359 We don't need to handle other cases of copying empty classes. */
360 if (! decl || ! DECL_BUILT_IN (decl))
361 for (i = 0; i < n; i++)
362 if (is_empty_class (TREE_TYPE (argarray[i]))
363 && ! TREE_ADDRESSABLE (TREE_TYPE (argarray[i])))
365 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (argarray[i]));
366 argarray[i] = build2 (COMPOUND_EXPR, TREE_TYPE (t),
370 function = build_call_array_loc (input_location,
371 result_type, function, n, argarray);
372 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
373 TREE_NOTHROW (function) = nothrow;
378 /* Build something of the form ptr->method (args)
379 or object.method (args). This can also build
380 calls to constructors, and find friends.
382 Member functions always take their class variable
385 INSTANCE is a class instance.
387 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
389 PARMS help to figure out what that NAME really refers to.
391 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
392 down to the real instance type to use for access checking. We need this
393 information to get protected accesses correct.
395 FLAGS is the logical disjunction of zero or more LOOKUP_
396 flags. See cp-tree.h for more info.
398 If this is all OK, calls build_function_call with the resolved
401 This function must also handle being called to perform
402 initialization, promotion/coercion of arguments, and
403 instantiation of default parameters.
405 Note that NAME may refer to an instance variable name. If
406 `operator()()' is defined for the type of that field, then we return
409 /* New overloading code. */
411 typedef struct z_candidate z_candidate;
413 typedef struct candidate_warning candidate_warning;
414 struct candidate_warning {
416 candidate_warning *next;
420 /* The FUNCTION_DECL that will be called if this candidate is
421 selected by overload resolution. */
423 /* If not NULL_TREE, the first argument to use when calling this
426 /* The rest of the arguments to use when calling this function. If
427 there are no further arguments this may be NULL or it may be an
429 const VEC(tree,gc) *args;
430 /* The implicit conversion sequences for each of the arguments to
433 /* The number of implicit conversion sequences. */
435 /* If FN is a user-defined conversion, the standard conversion
436 sequence from the type returned by FN to the desired destination
438 conversion *second_conv;
440 /* If FN is a member function, the binfo indicating the path used to
441 qualify the name of FN at the call site. This path is used to
442 determine whether or not FN is accessible if it is selected by
443 overload resolution. The DECL_CONTEXT of FN will always be a
444 (possibly improper) base of this binfo. */
446 /* If FN is a non-static member function, the binfo indicating the
447 subobject to which the `this' pointer should be converted if FN
448 is selected by overload resolution. The type pointed to the by
449 the `this' pointer must correspond to the most derived class
450 indicated by the CONVERSION_PATH. */
451 tree conversion_path;
454 candidate_warning *warnings;
458 /* Returns true iff T is a null pointer constant in the sense of
462 null_ptr_cst_p (tree t)
466 A null pointer constant is an integral constant expression
467 (_expr.const_) rvalue of integer type that evaluates to zero or
468 an rvalue of type std::nullptr_t. */
469 if (NULLPTR_TYPE_P (TREE_TYPE (t)))
471 if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
473 if (cxx_dialect >= cxx0x)
475 t = fold_non_dependent_expr (t);
476 t = maybe_constant_value (t);
477 if (TREE_CONSTANT (t) && integer_zerop (t))
482 t = integral_constant_value (t);
484 if (integer_zerop (t) && !TREE_OVERFLOW (t))
491 /* Returns nonzero if PARMLIST consists of only default parms and/or
495 sufficient_parms_p (const_tree parmlist)
497 for (; parmlist && parmlist != void_list_node;
498 parmlist = TREE_CHAIN (parmlist))
499 if (!TREE_PURPOSE (parmlist))
504 /* Allocate N bytes of memory from the conversion obstack. The memory
505 is zeroed before being returned. */
508 conversion_obstack_alloc (size_t n)
511 if (!conversion_obstack_initialized)
513 gcc_obstack_init (&conversion_obstack);
514 conversion_obstack_initialized = true;
516 p = obstack_alloc (&conversion_obstack, n);
521 /* Dynamically allocate a conversion. */
524 alloc_conversion (conversion_kind kind)
527 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
532 #ifdef ENABLE_CHECKING
534 /* Make sure that all memory on the conversion obstack has been
538 validate_conversion_obstack (void)
540 if (conversion_obstack_initialized)
541 gcc_assert ((obstack_next_free (&conversion_obstack)
542 == obstack_base (&conversion_obstack)));
545 #endif /* ENABLE_CHECKING */
547 /* Dynamically allocate an array of N conversions. */
550 alloc_conversions (size_t n)
552 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
556 build_conv (conversion_kind code, tree type, conversion *from)
559 conversion_rank rank = CONVERSION_RANK (from);
561 /* Note that the caller is responsible for filling in t->cand for
562 user-defined conversions. */
563 t = alloc_conversion (code);
586 t->user_conv_p = (code == ck_user || from->user_conv_p);
587 t->bad_p = from->bad_p;
592 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
593 specialization of std::initializer_list<T>, if such a conversion is
597 build_list_conv (tree type, tree ctor, int flags)
599 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
600 unsigned len = CONSTRUCTOR_NELTS (ctor);
601 conversion **subconvs = alloc_conversions (len);
606 /* Within a list-initialization we can have more user-defined
608 flags &= ~LOOKUP_NO_CONVERSION;
609 /* But no narrowing conversions. */
610 flags |= LOOKUP_NO_NARROWING;
612 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
615 = implicit_conversion (elttype, TREE_TYPE (val), val,
623 t = alloc_conversion (ck_list);
625 t->u.list = subconvs;
628 for (i = 0; i < len; ++i)
630 conversion *sub = subconvs[i];
631 if (sub->rank > t->rank)
633 if (sub->user_conv_p)
634 t->user_conv_p = true;
642 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
643 aggregate class, if such a conversion is possible. */
646 build_aggr_conv (tree type, tree ctor, int flags)
648 unsigned HOST_WIDE_INT i = 0;
650 tree field = next_initializable_field (TYPE_FIELDS (type));
651 tree empty_ctor = NULL_TREE;
653 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
655 if (i < CONSTRUCTOR_NELTS (ctor))
657 constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
658 if (!can_convert_arg (TREE_TYPE (field), TREE_TYPE (ce->value),
662 if (TREE_CODE (type) == UNION_TYPE)
667 if (empty_ctor == NULL_TREE)
668 empty_ctor = build_constructor (init_list_type_node, NULL);
669 if (!can_convert_arg (TREE_TYPE (field), TREE_TYPE (empty_ctor),
675 if (i < CONSTRUCTOR_NELTS (ctor))
678 c = alloc_conversion (ck_aggr);
681 c->user_conv_p = true;
686 /* Build a representation of the identity conversion from EXPR to
687 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
690 build_identity_conv (tree type, tree expr)
694 c = alloc_conversion (ck_identity);
701 /* Converting from EXPR to TYPE was ambiguous in the sense that there
702 were multiple user-defined conversions to accomplish the job.
703 Build a conversion that indicates that ambiguity. */
706 build_ambiguous_conv (tree type, tree expr)
710 c = alloc_conversion (ck_ambig);
718 strip_top_quals (tree t)
720 if (TREE_CODE (t) == ARRAY_TYPE)
722 return cp_build_qualified_type (t, 0);
725 /* Returns the standard conversion path (see [conv]) from type FROM to type
726 TO, if any. For proper handling of null pointer constants, you must
727 also pass the expression EXPR to convert from. If C_CAST_P is true,
728 this conversion is coming from a C-style cast. */
731 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
734 enum tree_code fcode, tcode;
736 bool fromref = false;
738 to = non_reference (to);
739 if (TREE_CODE (from) == REFERENCE_TYPE)
742 from = TREE_TYPE (from);
744 to = strip_top_quals (to);
745 from = strip_top_quals (from);
747 if ((TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
748 && expr && type_unknown_p (expr))
750 tsubst_flags_t tflags = tf_conv;
751 if (!(flags & LOOKUP_PROTECT))
752 tflags |= tf_no_access_control;
753 expr = instantiate_type (to, expr, tflags);
754 if (expr == error_mark_node)
756 from = TREE_TYPE (expr);
759 fcode = TREE_CODE (from);
760 tcode = TREE_CODE (to);
762 conv = build_identity_conv (from, expr);
763 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
765 from = type_decays_to (from);
766 fcode = TREE_CODE (from);
767 conv = build_conv (ck_lvalue, from, conv);
769 else if (fromref || (expr && lvalue_p (expr)))
774 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
777 from = strip_top_quals (bitfield_type);
778 fcode = TREE_CODE (from);
781 conv = build_conv (ck_rvalue, from, conv);
784 /* Allow conversion between `__complex__' data types. */
785 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
787 /* The standard conversion sequence to convert FROM to TO is
788 the standard conversion sequence to perform componentwise
790 conversion *part_conv = standard_conversion
791 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
795 conv = build_conv (part_conv->kind, to, conv);
796 conv->rank = part_conv->rank;
804 if (same_type_p (from, to))
808 A null pointer constant can be converted to a pointer type; ... A
809 null pointer constant of integral type can be converted to an
810 rvalue of type std::nullptr_t. */
811 if ((tcode == POINTER_TYPE || TYPE_PTR_TO_MEMBER_P (to)
812 || NULLPTR_TYPE_P (to))
813 && expr && null_ptr_cst_p (expr))
814 conv = build_conv (ck_std, to, conv);
815 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
816 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
818 /* For backwards brain damage compatibility, allow interconversion of
819 pointers and integers with a pedwarn. */
820 conv = build_conv (ck_std, to, conv);
823 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
825 /* For backwards brain damage compatibility, allow interconversion of
826 enums and integers with a pedwarn. */
827 conv = build_conv (ck_std, to, conv);
830 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
831 || (TYPE_PTRMEM_P (to) && TYPE_PTRMEM_P (from)))
836 if (tcode == POINTER_TYPE
837 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
840 else if (VOID_TYPE_P (TREE_TYPE (to))
841 && !TYPE_PTRMEM_P (from)
842 && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
844 tree nfrom = TREE_TYPE (from);
845 if (c_dialect_objc ())
846 nfrom = objc_non_volatilized_type (nfrom);
847 from = build_pointer_type
848 (cp_build_qualified_type (void_type_node,
849 cp_type_quals (nfrom)));
850 conv = build_conv (ck_ptr, from, conv);
852 else if (TYPE_PTRMEM_P (from))
854 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
855 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
857 if (DERIVED_FROM_P (fbase, tbase)
858 && (same_type_ignoring_top_level_qualifiers_p
859 (TYPE_PTRMEM_POINTED_TO_TYPE (from),
860 TYPE_PTRMEM_POINTED_TO_TYPE (to))))
862 from = build_ptrmem_type (tbase,
863 TYPE_PTRMEM_POINTED_TO_TYPE (from));
864 conv = build_conv (ck_pmem, from, conv);
866 else if (!same_type_p (fbase, tbase))
869 else if (CLASS_TYPE_P (TREE_TYPE (from))
870 && CLASS_TYPE_P (TREE_TYPE (to))
873 An rvalue of type "pointer to cv D," where D is a
874 class type, can be converted to an rvalue of type
875 "pointer to cv B," where B is a base class (clause
876 _class.derived_) of D. If B is an inaccessible
877 (clause _class.access_) or ambiguous
878 (_class.member.lookup_) base class of D, a program
879 that necessitates this conversion is ill-formed.
880 Therefore, we use DERIVED_FROM_P, and do not check
881 access or uniqueness. */
882 && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
885 cp_build_qualified_type (TREE_TYPE (to),
886 cp_type_quals (TREE_TYPE (from)));
887 from = build_pointer_type (from);
888 conv = build_conv (ck_ptr, from, conv);
892 if (tcode == POINTER_TYPE)
894 to_pointee = TREE_TYPE (to);
895 from_pointee = TREE_TYPE (from);
899 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
900 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
903 if (same_type_p (from, to))
905 else if (c_cast_p && comp_ptr_ttypes_const (to, from))
906 /* In a C-style cast, we ignore CV-qualification because we
907 are allowed to perform a static_cast followed by a
909 conv = build_conv (ck_qual, to, conv);
910 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
911 conv = build_conv (ck_qual, to, conv);
912 else if (expr && string_conv_p (to, expr, 0))
913 /* converting from string constant to char *. */
914 conv = build_conv (ck_qual, to, conv);
915 /* Allow conversions among compatible ObjC pointer types (base
916 conversions have been already handled above). */
917 else if (c_dialect_objc ()
918 && objc_compare_types (to, from, -4, NULL_TREE))
919 conv = build_conv (ck_ptr, to, conv);
920 else if (ptr_reasonably_similar (to_pointee, from_pointee))
922 conv = build_conv (ck_ptr, to, conv);
930 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
932 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
933 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
934 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
935 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
937 if (!DERIVED_FROM_P (fbase, tbase)
938 || !same_type_p (TREE_TYPE (fromfn), TREE_TYPE (tofn))
939 || !compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
940 TREE_CHAIN (TYPE_ARG_TYPES (tofn)))
941 || cp_type_quals (fbase) != cp_type_quals (tbase))
944 from = build_memfn_type (fromfn, tbase, cp_type_quals (tbase));
945 from = build_ptrmemfunc_type (build_pointer_type (from));
946 conv = build_conv (ck_pmem, from, conv);
949 else if (tcode == BOOLEAN_TYPE)
953 An rvalue of arithmetic, unscoped enumeration, pointer, or
954 pointer to member type can be converted to an rvalue of type
955 bool. ... An rvalue of type std::nullptr_t can be converted
956 to an rvalue of type bool; */
957 if (ARITHMETIC_TYPE_P (from)
958 || UNSCOPED_ENUM_P (from)
959 || fcode == POINTER_TYPE
960 || TYPE_PTR_TO_MEMBER_P (from)
961 || NULLPTR_TYPE_P (from))
963 conv = build_conv (ck_std, to, conv);
964 if (fcode == POINTER_TYPE
965 || TYPE_PTRMEM_P (from)
966 || (TYPE_PTRMEMFUNC_P (from)
967 && conv->rank < cr_pbool)
968 || NULLPTR_TYPE_P (from))
969 conv->rank = cr_pbool;
975 /* We don't check for ENUMERAL_TYPE here because there are no standard
976 conversions to enum type. */
977 /* As an extension, allow conversion to complex type. */
978 else if (ARITHMETIC_TYPE_P (to))
980 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE)
981 || SCOPED_ENUM_P (from))
983 conv = build_conv (ck_std, to, conv);
985 /* Give this a better rank if it's a promotion. */
986 if (same_type_p (to, type_promotes_to (from))
987 && conv->u.next->rank <= cr_promotion)
988 conv->rank = cr_promotion;
990 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
991 && vector_types_convertible_p (from, to, false))
992 return build_conv (ck_std, to, conv);
993 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
994 && is_properly_derived_from (from, to))
996 if (conv->kind == ck_rvalue)
998 conv = build_conv (ck_base, to, conv);
999 /* The derived-to-base conversion indicates the initialization
1000 of a parameter with base type from an object of a derived
1001 type. A temporary object is created to hold the result of
1002 the conversion unless we're binding directly to a reference. */
1003 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1008 if (flags & LOOKUP_NO_NARROWING)
1009 conv->check_narrowing = true;
1014 /* Returns nonzero if T1 is reference-related to T2. */
1017 reference_related_p (tree t1, tree t2)
1019 if (t1 == error_mark_node || t2 == error_mark_node)
1022 t1 = TYPE_MAIN_VARIANT (t1);
1023 t2 = TYPE_MAIN_VARIANT (t2);
1027 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1028 to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
1030 return (same_type_p (t1, t2)
1031 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1032 && DERIVED_FROM_P (t1, t2)));
1035 /* Returns nonzero if T1 is reference-compatible with T2. */
1038 reference_compatible_p (tree t1, tree t2)
1042 "cv1 T1" is reference compatible with "cv2 T2" if T1 is
1043 reference-related to T2 and cv1 is the same cv-qualification as,
1044 or greater cv-qualification than, cv2. */
1045 return (reference_related_p (t1, t2)
1046 && at_least_as_qualified_p (t1, t2));
1049 /* Determine whether or not the EXPR (of class type S) can be
1050 converted to T as in [over.match.ref]. */
1053 convert_class_to_reference (tree reference_type, tree s, tree expr, int flags)
1059 struct z_candidate *candidates;
1060 struct z_candidate *cand;
1066 conversions = lookup_conversions (s, /*lookup_template_convs_p=*/true);
1072 Assuming that "cv1 T" is the underlying type of the reference
1073 being initialized, and "cv S" is the type of the initializer
1074 expression, with S a class type, the candidate functions are
1075 selected as follows:
1077 --The conversion functions of S and its base classes are
1078 considered. Those that are not hidden within S and yield type
1079 "reference to cv2 T2", where "cv1 T" is reference-compatible
1080 (_dcl.init.ref_) with "cv2 T2", are candidate functions.
1082 The argument list has one argument, which is the initializer
1087 /* Conceptually, we should take the address of EXPR and put it in
1088 the argument list. Unfortunately, however, that can result in
1089 error messages, which we should not issue now because we are just
1090 trying to find a conversion operator. Therefore, we use NULL,
1091 cast to the appropriate type. */
1092 first_arg = build_int_cst (build_pointer_type (s), 0);
1094 t = TREE_TYPE (reference_type);
1096 /* We're performing a user-defined conversion to a desired type, so set
1097 this for the benefit of add_candidates. */
1098 flags |= LOOKUP_NO_CONVERSION;
1100 for (; conversions; conversions = TREE_CHAIN (conversions))
1102 tree fns = TREE_VALUE (conversions);
1103 tree binfo = TREE_PURPOSE (conversions);
1104 struct z_candidate *old_candidates = candidates;;
1106 add_candidates (fns, first_arg, NULL, reference_type,
1108 binfo, TYPE_BINFO (s),
1109 flags, &candidates);
1111 for (cand = candidates; cand != old_candidates; cand = cand->next)
1113 /* Now, see if the conversion function really returns
1114 an lvalue of the appropriate type. From the
1115 point of view of unification, simply returning an
1116 rvalue of the right type is good enough. */
1118 tree t2 = TREE_TYPE (TREE_TYPE (f));
1119 if (TREE_CODE (t2) != REFERENCE_TYPE
1120 || !reference_compatible_p (t, TREE_TYPE (t2)))
1126 conversion *identity_conv;
1127 /* Build a standard conversion sequence indicating the
1128 binding from the reference type returned by the
1129 function to the desired REFERENCE_TYPE. */
1131 = build_identity_conv (TREE_TYPE (TREE_TYPE
1132 (TREE_TYPE (cand->fn))),
1135 = (direct_reference_binding
1136 (reference_type, identity_conv));
1137 cand->second_conv->rvaluedness_matches_p
1138 = TYPE_REF_IS_RVALUE (TREE_TYPE (TREE_TYPE (cand->fn)))
1139 == TYPE_REF_IS_RVALUE (reference_type);
1140 cand->second_conv->bad_p |= cand->convs[0]->bad_p;
1142 /* Don't allow binding of lvalues to rvalue references. */
1143 if (TYPE_REF_IS_RVALUE (reference_type)
1144 && !TYPE_REF_IS_RVALUE (TREE_TYPE (TREE_TYPE (cand->fn))))
1145 cand->second_conv->bad_p = true;
1150 candidates = splice_viable (candidates, pedantic, &any_viable_p);
1151 /* If none of the conversion functions worked out, let our caller
1156 cand = tourney (candidates);
1160 /* Now that we know that this is the function we're going to use fix
1161 the dummy first argument. */
1162 gcc_assert (cand->first_arg == NULL_TREE
1163 || integer_zerop (cand->first_arg));
1164 cand->first_arg = build_this (expr);
1166 /* Build a user-defined conversion sequence representing the
1168 conv = build_conv (ck_user,
1169 TREE_TYPE (TREE_TYPE (cand->fn)),
1170 build_identity_conv (TREE_TYPE (expr), expr));
1173 if (cand->viable == -1)
1176 /* Merge it with the standard conversion sequence from the
1177 conversion function's return type to the desired type. */
1178 cand->second_conv = merge_conversion_sequences (conv, cand->second_conv);
1180 return cand->second_conv;
1183 /* A reference of the indicated TYPE is being bound directly to the
1184 expression represented by the implicit conversion sequence CONV.
1185 Return a conversion sequence for this binding. */
1188 direct_reference_binding (tree type, conversion *conv)
1192 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1193 gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
1195 t = TREE_TYPE (type);
1199 When a parameter of reference type binds directly
1200 (_dcl.init.ref_) to an argument expression, the implicit
1201 conversion sequence is the identity conversion, unless the
1202 argument expression has a type that is a derived class of the
1203 parameter type, in which case the implicit conversion sequence is
1204 a derived-to-base Conversion.
1206 If the parameter binds directly to the result of applying a
1207 conversion function to the argument expression, the implicit
1208 conversion sequence is a user-defined conversion sequence
1209 (_over.ics.user_), with the second standard conversion sequence
1210 either an identity conversion or, if the conversion function
1211 returns an entity of a type that is a derived class of the
1212 parameter type, a derived-to-base conversion. */
1213 if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
1215 /* Represent the derived-to-base conversion. */
1216 conv = build_conv (ck_base, t, conv);
1217 /* We will actually be binding to the base-class subobject in
1218 the derived class, so we mark this conversion appropriately.
1219 That way, convert_like knows not to generate a temporary. */
1220 conv->need_temporary_p = false;
1222 return build_conv (ck_ref_bind, type, conv);
1225 /* Returns the conversion path from type FROM to reference type TO for
1226 purposes of reference binding. For lvalue binding, either pass a
1227 reference type to FROM or an lvalue expression to EXPR. If the
1228 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1229 the conversion returned. If C_CAST_P is true, this
1230 conversion is coming from a C-style cast. */
1233 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags)
1235 conversion *conv = NULL;
1236 tree to = TREE_TYPE (rto);
1241 cp_lvalue_kind is_lvalue = clk_none;
1243 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1245 expr = instantiate_type (to, expr, tf_none);
1246 if (expr == error_mark_node)
1248 from = TREE_TYPE (expr);
1251 if (TREE_CODE (from) == REFERENCE_TYPE)
1253 /* Anything with reference type is an lvalue. */
1254 is_lvalue = clk_ordinary;
1255 from = TREE_TYPE (from);
1258 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1260 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1261 conv = implicit_conversion (to, from, expr, c_cast_p,
1263 if (!CLASS_TYPE_P (to)
1264 && CONSTRUCTOR_NELTS (expr) == 1)
1266 expr = CONSTRUCTOR_ELT (expr, 0)->value;
1267 if (error_operand_p (expr))
1269 from = TREE_TYPE (expr);
1273 if (is_lvalue == clk_none && expr)
1274 is_lvalue = real_lvalue_p (expr);
1277 if ((is_lvalue & clk_bitfield) != 0)
1278 tfrom = unlowered_expr_type (expr);
1280 /* Figure out whether or not the types are reference-related and
1281 reference compatible. We have do do this after stripping
1282 references from FROM. */
1283 related_p = reference_related_p (to, tfrom);
1284 /* If this is a C cast, first convert to an appropriately qualified
1285 type, so that we can later do a const_cast to the desired type. */
1286 if (related_p && c_cast_p
1287 && !at_least_as_qualified_p (to, tfrom))
1288 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1289 compatible_p = reference_compatible_p (to, tfrom);
1291 /* Directly bind reference when target expression's type is compatible with
1292 the reference and expression is an lvalue. In DR391, the wording in
1293 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1294 const and rvalue references to rvalues of compatible class type.
1295 We should also do direct bindings for non-class "rvalues" derived from
1296 rvalue references. */
1299 || (((CP_TYPE_CONST_NON_VOLATILE_P (to)
1300 && !(flags & LOOKUP_NO_TEMP_BIND))
1301 || TYPE_REF_IS_RVALUE (rto))
1302 && (CLASS_TYPE_P (from) || (expr && lvalue_p (expr))))))
1306 If the initializer expression
1308 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1309 is reference-compatible with "cv2 T2,"
1311 the reference is bound directly to the initializer expression
1315 If the initializer expression is an rvalue, with T2 a class type,
1316 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1317 is bound to the object represented by the rvalue or to a sub-object
1318 within that object. */
1320 conv = build_identity_conv (tfrom, expr);
1321 conv = direct_reference_binding (rto, conv);
1323 if (flags & LOOKUP_PREFER_RVALUE)
1324 /* The top-level caller requested that we pretend that the lvalue
1325 be treated as an rvalue. */
1326 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1328 conv->rvaluedness_matches_p
1329 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1331 if ((is_lvalue & clk_bitfield) != 0
1332 || ((is_lvalue & clk_packed) != 0 && !TYPE_PACKED (to)))
1333 /* For the purposes of overload resolution, we ignore the fact
1334 this expression is a bitfield or packed field. (In particular,
1335 [over.ics.ref] says specifically that a function with a
1336 non-const reference parameter is viable even if the
1337 argument is a bitfield.)
1339 However, when we actually call the function we must create
1340 a temporary to which to bind the reference. If the
1341 reference is volatile, or isn't const, then we cannot make
1342 a temporary, so we just issue an error when the conversion
1344 conv->need_temporary_p = true;
1346 /* Don't allow binding of lvalues to rvalue references. */
1347 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1348 && !(flags & LOOKUP_PREFER_RVALUE))
1353 /* [class.conv.fct] A conversion function is never used to convert a
1354 (possibly cv-qualified) object to the (possibly cv-qualified) same
1355 object type (or a reference to it), to a (possibly cv-qualified) base
1356 class of that type (or a reference to it).... */
1357 else if (CLASS_TYPE_P (from) && !related_p
1358 && !(flags & LOOKUP_NO_CONVERSION))
1362 If the initializer expression
1364 -- has a class type (i.e., T2 is a class type) can be
1365 implicitly converted to an lvalue of type "cv3 T3," where
1366 "cv1 T1" is reference-compatible with "cv3 T3". (this
1367 conversion is selected by enumerating the applicable
1368 conversion functions (_over.match.ref_) and choosing the
1369 best one through overload resolution. (_over.match_).
1371 the reference is bound to the lvalue result of the conversion
1372 in the second case. */
1373 conv = convert_class_to_reference (rto, from, expr, flags);
1378 /* From this point on, we conceptually need temporaries, even if we
1379 elide them. Only the cases above are "direct bindings". */
1380 if (flags & LOOKUP_NO_TEMP_BIND)
1385 When a parameter of reference type is not bound directly to an
1386 argument expression, the conversion sequence is the one required
1387 to convert the argument expression to the underlying type of the
1388 reference according to _over.best.ics_. Conceptually, this
1389 conversion sequence corresponds to copy-initializing a temporary
1390 of the underlying type with the argument expression. Any
1391 difference in top-level cv-qualification is subsumed by the
1392 initialization itself and does not constitute a conversion. */
1396 Otherwise, the reference shall be to a non-volatile const type.
1398 Under C++0x, [8.5.3/5 dcl.init.ref] it may also be an rvalue reference */
1399 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1404 Otherwise, a temporary of type "cv1 T1" is created and
1405 initialized from the initializer expression using the rules for a
1406 non-reference copy initialization. If T1 is reference-related to
1407 T2, cv1 must be the same cv-qualification as, or greater
1408 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1409 if (related_p && !at_least_as_qualified_p (to, from))
1412 /* We're generating a temporary now, but don't bind any more in the
1413 conversion (specifically, don't slice the temporary returned by a
1414 conversion operator). */
1415 flags |= LOOKUP_NO_TEMP_BIND;
1417 /* Core issue 899: When [copy-]initializing a temporary to be bound
1418 to the first parameter of a copy constructor (12.8) called with
1419 a single argument in the context of direct-initialization,
1420 explicit conversion functions are also considered.
1422 So don't set LOOKUP_ONLYCONVERTING in that case. */
1423 if (!(flags & LOOKUP_COPY_PARM))
1424 flags |= LOOKUP_ONLYCONVERTING;
1427 conv = implicit_conversion (to, from, expr, c_cast_p,
1432 conv = build_conv (ck_ref_bind, rto, conv);
1433 /* This reference binding, unlike those above, requires the
1434 creation of a temporary. */
1435 conv->need_temporary_p = true;
1436 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
1441 /* Returns the implicit conversion sequence (see [over.ics]) from type
1442 FROM to type TO. The optional expression EXPR may affect the
1443 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1444 true, this conversion is coming from a C-style cast. */
1447 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
1452 if (from == error_mark_node || to == error_mark_node
1453 || expr == error_mark_node)
1456 if (c_dialect_objc ())
1457 from = objc_non_volatilized_type (from);
1459 if (TREE_CODE (to) == REFERENCE_TYPE)
1460 conv = reference_binding (to, from, expr, c_cast_p, flags);
1462 conv = standard_conversion (to, from, expr, c_cast_p, flags);
1467 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1469 if (is_std_init_list (to))
1470 return build_list_conv (to, expr, flags);
1472 /* Allow conversion from an initializer-list with one element to a
1474 if (SCALAR_TYPE_P (to))
1476 int nelts = CONSTRUCTOR_NELTS (expr);
1480 elt = build_value_init (to, tf_none);
1481 else if (nelts == 1)
1482 elt = CONSTRUCTOR_ELT (expr, 0)->value;
1484 elt = error_mark_node;
1486 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
1490 conv->check_narrowing = true;
1491 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
1492 /* Too many levels of braces, i.e. '{{1}}'. */
1499 if (expr != NULL_TREE
1500 && (MAYBE_CLASS_TYPE_P (from)
1501 || MAYBE_CLASS_TYPE_P (to))
1502 && (flags & LOOKUP_NO_CONVERSION) == 0)
1504 struct z_candidate *cand;
1505 int convflags = (flags & (LOOKUP_NO_TEMP_BIND|LOOKUP_ONLYCONVERTING
1506 |LOOKUP_NO_NARROWING));
1508 if (CLASS_TYPE_P (to)
1509 && !CLASSTYPE_NON_AGGREGATE (complete_type (to))
1510 && BRACE_ENCLOSED_INITIALIZER_P (expr))
1511 return build_aggr_conv (to, expr, flags);
1513 cand = build_user_type_conversion_1 (to, expr, convflags);
1515 conv = cand->second_conv;
1517 /* We used to try to bind a reference to a temporary here, but that
1518 is now handled after the recursive call to this function at the end
1519 of reference_binding. */
1526 /* Add a new entry to the list of candidates. Used by the add_*_candidate
1527 functions. ARGS will not be changed until a single candidate is
1530 static struct z_candidate *
1531 add_candidate (struct z_candidate **candidates,
1532 tree fn, tree first_arg, const VEC(tree,gc) *args,
1533 size_t num_convs, conversion **convs,
1534 tree access_path, tree conversion_path,
1537 struct z_candidate *cand = (struct z_candidate *)
1538 conversion_obstack_alloc (sizeof (struct z_candidate));
1541 cand->first_arg = first_arg;
1543 cand->convs = convs;
1544 cand->num_convs = num_convs;
1545 cand->access_path = access_path;
1546 cand->conversion_path = conversion_path;
1547 cand->viable = viable;
1548 cand->next = *candidates;
1554 /* Create an overload candidate for the function or method FN called
1555 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
1556 FLAGS is passed on to implicit_conversion.
1558 This does not change ARGS.
1560 CTYPE, if non-NULL, is the type we want to pretend this function
1561 comes from for purposes of overload resolution. */
1563 static struct z_candidate *
1564 add_function_candidate (struct z_candidate **candidates,
1565 tree fn, tree ctype, tree first_arg,
1566 const VEC(tree,gc) *args, tree access_path,
1567 tree conversion_path, int flags)
1569 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
1573 tree orig_first_arg = first_arg;
1577 /* At this point we should not see any functions which haven't been
1578 explicitly declared, except for friend functions which will have
1579 been found using argument dependent lookup. */
1580 gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
1582 /* The `this', `in_chrg' and VTT arguments to constructors are not
1583 considered in overload resolution. */
1584 if (DECL_CONSTRUCTOR_P (fn))
1586 parmlist = skip_artificial_parms_for (fn, parmlist);
1587 skip = num_artificial_parms_for (fn);
1588 if (skip > 0 && first_arg != NULL_TREE)
1591 first_arg = NULL_TREE;
1597 len = VEC_length (tree, args) - skip + (first_arg != NULL_TREE ? 1 : 0);
1598 convs = alloc_conversions (len);
1600 /* 13.3.2 - Viable functions [over.match.viable]
1601 First, to be a viable function, a candidate function shall have enough
1602 parameters to agree in number with the arguments in the list.
1604 We need to check this first; otherwise, checking the ICSes might cause
1605 us to produce an ill-formed template instantiation. */
1607 parmnode = parmlist;
1608 for (i = 0; i < len; ++i)
1610 if (parmnode == NULL_TREE || parmnode == void_list_node)
1612 parmnode = TREE_CHAIN (parmnode);
1615 if (i < len && parmnode)
1618 /* Make sure there are default args for the rest of the parms. */
1619 else if (!sufficient_parms_p (parmnode))
1622 /* Kludge: When looking for a function from a subobject while generating
1623 an implicit copy/move constructor/operator=, don't consider anything
1624 that takes (a reference to) an unrelated type. See c++/44909. */
1626 && ((flags & LOOKUP_SPECULATIVE)
1627 || (current_function_decl
1628 && DECL_DEFAULTED_FN (current_function_decl))))
1630 if (DECL_CONSTRUCTOR_P (fn))
1632 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1633 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1639 parmnode = chain_index (i-1, parmlist);
1640 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
1649 /* Second, for F to be a viable function, there shall exist for each
1650 argument an implicit conversion sequence that converts that argument
1651 to the corresponding parameter of F. */
1653 parmnode = parmlist;
1655 for (i = 0; i < len; ++i)
1661 if (parmnode == void_list_node)
1664 if (i == 0 && first_arg != NULL_TREE)
1667 arg = VEC_index (tree, args,
1668 i + skip - (first_arg != NULL_TREE ? 1 : 0));
1669 argtype = lvalue_type (arg);
1671 is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1672 && ! DECL_CONSTRUCTOR_P (fn));
1676 tree parmtype = TREE_VALUE (parmnode);
1679 parmnode = TREE_CHAIN (parmnode);
1681 /* The type of the implicit object parameter ('this') for
1682 overload resolution is not always the same as for the
1683 function itself; conversion functions are considered to
1684 be members of the class being converted, and functions
1685 introduced by a using-declaration are considered to be
1686 members of the class that uses them.
1688 Since build_over_call ignores the ICS for the `this'
1689 parameter, we can just change the parm type. */
1690 if (ctype && is_this)
1692 parmtype = cp_build_qualified_type
1693 (ctype, cp_type_quals (TREE_TYPE (parmtype)));
1694 parmtype = build_pointer_type (parmtype);
1697 /* Core issue 899: When [copy-]initializing a temporary to be bound
1698 to the first parameter of a copy constructor (12.8) called with
1699 a single argument in the context of direct-initialization,
1700 explicit conversion functions are also considered.
1702 So set LOOKUP_COPY_PARM to let reference_binding know that
1703 it's being called in that context. We generalize the above
1704 to handle move constructors and template constructors as well;
1705 the standardese should soon be updated similarly. */
1706 if (ctype && i == 0 && (len-skip == 1)
1707 && !(flags & LOOKUP_ONLYCONVERTING)
1708 && DECL_CONSTRUCTOR_P (fn)
1709 && parmtype != error_mark_node
1710 && (same_type_ignoring_top_level_qualifiers_p
1711 (non_reference (parmtype), ctype)))
1713 lflags |= LOOKUP_COPY_PARM;
1714 /* We allow user-defined conversions within init-lists, but
1715 not for the copy constructor. */
1716 if (flags & LOOKUP_NO_COPY_CTOR_CONVERSION)
1717 lflags |= LOOKUP_NO_CONVERSION;
1720 lflags |= LOOKUP_ONLYCONVERTING;
1722 t = implicit_conversion (parmtype, argtype, arg,
1723 /*c_cast_p=*/false, lflags);
1727 t = build_identity_conv (argtype, arg);
1728 t->ellipsis_p = true;
1746 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
1747 access_path, conversion_path, viable);
1750 /* Create an overload candidate for the conversion function FN which will
1751 be invoked for expression OBJ, producing a pointer-to-function which
1752 will in turn be called with the argument list FIRST_ARG/ARGLIST,
1753 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
1754 passed on to implicit_conversion.
1756 Actually, we don't really care about FN; we care about the type it
1757 converts to. There may be multiple conversion functions that will
1758 convert to that type, and we rely on build_user_type_conversion_1 to
1759 choose the best one; so when we create our candidate, we record the type
1760 instead of the function. */
1762 static struct z_candidate *
1763 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
1764 tree first_arg, const VEC(tree,gc) *arglist,
1765 tree access_path, tree conversion_path)
1767 tree totype = TREE_TYPE (TREE_TYPE (fn));
1768 int i, len, viable, flags;
1769 tree parmlist, parmnode;
1772 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
1773 parmlist = TREE_TYPE (parmlist);
1774 parmlist = TYPE_ARG_TYPES (parmlist);
1776 len = VEC_length (tree, arglist) + (first_arg != NULL_TREE ? 1 : 0) + 1;
1777 convs = alloc_conversions (len);
1778 parmnode = parmlist;
1780 flags = LOOKUP_IMPLICIT;
1782 /* Don't bother looking up the same type twice. */
1783 if (*candidates && (*candidates)->fn == totype)
1786 for (i = 0; i < len; ++i)
1793 else if (i == 1 && first_arg != NULL_TREE)
1796 arg = VEC_index (tree, arglist,
1797 i - (first_arg != NULL_TREE ? 1 : 0) - 1);
1798 argtype = lvalue_type (arg);
1801 t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
1803 else if (parmnode == void_list_node)
1806 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
1807 /*c_cast_p=*/false, flags);
1810 t = build_identity_conv (argtype, arg);
1811 t->ellipsis_p = true;
1825 parmnode = TREE_CHAIN (parmnode);
1831 if (!sufficient_parms_p (parmnode))
1834 return add_candidate (candidates, totype, first_arg, arglist, len, convs,
1835 access_path, conversion_path, viable);
1839 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
1840 tree type1, tree type2, tree *args, tree *argtypes,
1852 num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
1853 convs = alloc_conversions (num_convs);
1855 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
1856 conversion ops are allowed. We handle that here by just checking for
1857 boolean_type_node because other operators don't ask for it. COND_EXPR
1858 also does contextual conversion to bool for the first operand, but we
1859 handle that in build_conditional_expr, and type1 here is operand 2. */
1860 if (type1 != boolean_type_node)
1861 flags |= LOOKUP_ONLYCONVERTING;
1863 for (i = 0; i < 2; ++i)
1868 t = implicit_conversion (types[i], argtypes[i], args[i],
1869 /*c_cast_p=*/false, flags);
1873 /* We need something for printing the candidate. */
1874 t = build_identity_conv (types[i], NULL_TREE);
1881 /* For COND_EXPR we rearranged the arguments; undo that now. */
1884 convs[2] = convs[1];
1885 convs[1] = convs[0];
1886 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
1887 /*c_cast_p=*/false, flags);
1894 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
1896 /*access_path=*/NULL_TREE,
1897 /*conversion_path=*/NULL_TREE,
1902 is_complete (tree t)
1904 return COMPLETE_TYPE_P (complete_type (t));
1907 /* Returns nonzero if TYPE is a promoted arithmetic type. */
1910 promoted_arithmetic_type_p (tree type)
1914 In this section, the term promoted integral type is used to refer
1915 to those integral types which are preserved by integral promotion
1916 (including e.g. int and long but excluding e.g. char).
1917 Similarly, the term promoted arithmetic type refers to promoted
1918 integral types plus floating types. */
1919 return ((CP_INTEGRAL_TYPE_P (type)
1920 && same_type_p (type_promotes_to (type), type))
1921 || TREE_CODE (type) == REAL_TYPE);
1924 /* Create any builtin operator overload candidates for the operator in
1925 question given the converted operand types TYPE1 and TYPE2. The other
1926 args are passed through from add_builtin_candidates to
1927 build_builtin_candidate.
1929 TYPE1 and TYPE2 may not be permissible, and we must filter them.
1930 If CODE is requires candidates operands of the same type of the kind
1931 of which TYPE1 and TYPE2 are, we add both candidates
1932 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
1935 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
1936 enum tree_code code2, tree fnname, tree type1,
1937 tree type2, tree *args, tree *argtypes, int flags)
1941 case POSTINCREMENT_EXPR:
1942 case POSTDECREMENT_EXPR:
1943 args[1] = integer_zero_node;
1944 type2 = integer_type_node;
1953 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
1954 and VQ is either volatile or empty, there exist candidate operator
1955 functions of the form
1956 VQ T& operator++(VQ T&);
1957 T operator++(VQ T&, int);
1958 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
1959 type other than bool, and VQ is either volatile or empty, there exist
1960 candidate operator functions of the form
1961 VQ T& operator--(VQ T&);
1962 T operator--(VQ T&, int);
1963 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
1964 complete object type, and VQ is either volatile or empty, there exist
1965 candidate operator functions of the form
1966 T*VQ& operator++(T*VQ&);
1967 T*VQ& operator--(T*VQ&);
1968 T* operator++(T*VQ&, int);
1969 T* operator--(T*VQ&, int); */
1971 case POSTDECREMENT_EXPR:
1972 case PREDECREMENT_EXPR:
1973 if (TREE_CODE (type1) == BOOLEAN_TYPE)
1975 case POSTINCREMENT_EXPR:
1976 case PREINCREMENT_EXPR:
1977 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
1979 type1 = build_reference_type (type1);
1984 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
1985 exist candidate operator functions of the form
1989 8 For every function type T, there exist candidate operator functions of
1991 T& operator*(T*); */
1994 if (TREE_CODE (type1) == POINTER_TYPE
1995 && (TYPE_PTROB_P (type1)
1996 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2000 /* 9 For every type T, there exist candidate operator functions of the form
2003 10For every promoted arithmetic type T, there exist candidate operator
2004 functions of the form
2008 case UNARY_PLUS_EXPR: /* unary + */
2009 if (TREE_CODE (type1) == POINTER_TYPE)
2012 if (ARITHMETIC_TYPE_P (type1))
2016 /* 11For every promoted integral type T, there exist candidate operator
2017 functions of the form
2021 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2025 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2026 is the same type as C2 or is a derived class of C2, T is a complete
2027 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2028 there exist candidate operator functions of the form
2029 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2030 where CV12 is the union of CV1 and CV2. */
2033 if (TREE_CODE (type1) == POINTER_TYPE
2034 && TYPE_PTR_TO_MEMBER_P (type2))
2036 tree c1 = TREE_TYPE (type1);
2037 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2039 if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2040 && (TYPE_PTRMEMFUNC_P (type2)
2041 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2046 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2047 didate operator functions of the form
2052 bool operator<(L, R);
2053 bool operator>(L, R);
2054 bool operator<=(L, R);
2055 bool operator>=(L, R);
2056 bool operator==(L, R);
2057 bool operator!=(L, R);
2058 where LR is the result of the usual arithmetic conversions between
2061 14For every pair of types T and I, where T is a cv-qualified or cv-
2062 unqualified complete object type and I is a promoted integral type,
2063 there exist candidate operator functions of the form
2064 T* operator+(T*, I);
2065 T& operator[](T*, I);
2066 T* operator-(T*, I);
2067 T* operator+(I, T*);
2068 T& operator[](I, T*);
2070 15For every T, where T is a pointer to complete object type, there exist
2071 candidate operator functions of the form112)
2072 ptrdiff_t operator-(T, T);
2074 16For every pointer or enumeration type T, there exist candidate operator
2075 functions of the form
2076 bool operator<(T, T);
2077 bool operator>(T, T);
2078 bool operator<=(T, T);
2079 bool operator>=(T, T);
2080 bool operator==(T, T);
2081 bool operator!=(T, T);
2083 17For every pointer to member type T, there exist candidate operator
2084 functions of the form
2085 bool operator==(T, T);
2086 bool operator!=(T, T); */
2089 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2091 if (TYPE_PTROB_P (type1)
2092 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2094 type2 = ptrdiff_type_node;
2098 case TRUNC_DIV_EXPR:
2099 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2105 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2106 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2)))
2108 if (TYPE_PTR_TO_MEMBER_P (type1) && null_ptr_cst_p (args[1]))
2113 if (TYPE_PTR_TO_MEMBER_P (type2) && null_ptr_cst_p (args[0]))
2125 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2127 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2129 if (TREE_CODE (type1) == ENUMERAL_TYPE
2130 && TREE_CODE (type2) == ENUMERAL_TYPE)
2132 if (TYPE_PTR_P (type1)
2133 && null_ptr_cst_p (args[1])
2134 && !uses_template_parms (type1))
2139 if (null_ptr_cst_p (args[0])
2140 && TYPE_PTR_P (type2)
2141 && !uses_template_parms (type2))
2149 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2152 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2154 type1 = ptrdiff_type_node;
2157 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2159 type2 = ptrdiff_type_node;
2164 /* 18For every pair of promoted integral types L and R, there exist candi-
2165 date operator functions of the form
2172 where LR is the result of the usual arithmetic conversions between
2175 case TRUNC_MOD_EXPR:
2181 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2185 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2186 type, VQ is either volatile or empty, and R is a promoted arithmetic
2187 type, there exist candidate operator functions of the form
2188 VQ L& operator=(VQ L&, R);
2189 VQ L& operator*=(VQ L&, R);
2190 VQ L& operator/=(VQ L&, R);
2191 VQ L& operator+=(VQ L&, R);
2192 VQ L& operator-=(VQ L&, R);
2194 20For every pair T, VQ), where T is any type and VQ is either volatile
2195 or empty, there exist candidate operator functions of the form
2196 T*VQ& operator=(T*VQ&, T*);
2198 21For every pair T, VQ), where T is a pointer to member type and VQ is
2199 either volatile or empty, there exist candidate operator functions of
2201 VQ T& operator=(VQ T&, T);
2203 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2204 unqualified complete object type, VQ is either volatile or empty, and
2205 I is a promoted integral type, there exist candidate operator func-
2207 T*VQ& operator+=(T*VQ&, I);
2208 T*VQ& operator-=(T*VQ&, I);
2210 23For every triple L, VQ, R), where L is an integral or enumeration
2211 type, VQ is either volatile or empty, and R is a promoted integral
2212 type, there exist candidate operator functions of the form
2214 VQ L& operator%=(VQ L&, R);
2215 VQ L& operator<<=(VQ L&, R);
2216 VQ L& operator>>=(VQ L&, R);
2217 VQ L& operator&=(VQ L&, R);
2218 VQ L& operator^=(VQ L&, R);
2219 VQ L& operator|=(VQ L&, R); */
2226 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2228 type2 = ptrdiff_type_node;
2232 case TRUNC_DIV_EXPR:
2233 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2237 case TRUNC_MOD_EXPR:
2243 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2248 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2250 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2251 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2252 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2253 || ((TYPE_PTRMEMFUNC_P (type1)
2254 || TREE_CODE (type1) == POINTER_TYPE)
2255 && null_ptr_cst_p (args[1])))
2265 type1 = build_reference_type (type1);
2271 For every pair of promoted arithmetic types L and R, there
2272 exist candidate operator functions of the form
2274 LR operator?(bool, L, R);
2276 where LR is the result of the usual arithmetic conversions
2277 between types L and R.
2279 For every type T, where T is a pointer or pointer-to-member
2280 type, there exist candidate operator functions of the form T
2281 operator?(bool, T, T); */
2283 if (promoted_arithmetic_type_p (type1)
2284 && promoted_arithmetic_type_p (type2))
2288 /* Otherwise, the types should be pointers. */
2289 if (!(TYPE_PTR_P (type1) || TYPE_PTR_TO_MEMBER_P (type1))
2290 || !(TYPE_PTR_P (type2) || TYPE_PTR_TO_MEMBER_P (type2)))
2293 /* We don't check that the two types are the same; the logic
2294 below will actually create two candidates; one in which both
2295 parameter types are TYPE1, and one in which both parameter
2303 /* If we're dealing with two pointer types or two enumeral types,
2304 we need candidates for both of them. */
2305 if (type2 && !same_type_p (type1, type2)
2306 && TREE_CODE (type1) == TREE_CODE (type2)
2307 && (TREE_CODE (type1) == REFERENCE_TYPE
2308 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2309 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2310 || TYPE_PTRMEMFUNC_P (type1)
2311 || MAYBE_CLASS_TYPE_P (type1)
2312 || TREE_CODE (type1) == ENUMERAL_TYPE))
2314 build_builtin_candidate
2315 (candidates, fnname, type1, type1, args, argtypes, flags);
2316 build_builtin_candidate
2317 (candidates, fnname, type2, type2, args, argtypes, flags);
2321 build_builtin_candidate
2322 (candidates, fnname, type1, type2, args, argtypes, flags);
2326 type_decays_to (tree type)
2328 if (TREE_CODE (type) == ARRAY_TYPE)
2329 return build_pointer_type (TREE_TYPE (type));
2330 if (TREE_CODE (type) == FUNCTION_TYPE)
2331 return build_pointer_type (type);
2332 if (!MAYBE_CLASS_TYPE_P (type))
2333 type = cv_unqualified (type);
2337 /* There are three conditions of builtin candidates:
2339 1) bool-taking candidates. These are the same regardless of the input.
2340 2) pointer-pair taking candidates. These are generated for each type
2341 one of the input types converts to.
2342 3) arithmetic candidates. According to the standard, we should generate
2343 all of these, but I'm trying not to...
2345 Here we generate a superset of the possible candidates for this particular
2346 case. That is a subset of the full set the standard defines, plus some
2347 other cases which the standard disallows. add_builtin_candidate will
2348 filter out the invalid set. */
2351 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
2352 enum tree_code code2, tree fnname, tree *args,
2357 tree type, argtypes[3], t;
2358 /* TYPES[i] is the set of possible builtin-operator parameter types
2359 we will consider for the Ith argument. */
2360 VEC(tree,gc) *types[2];
2363 for (i = 0; i < 3; ++i)
2366 argtypes[i] = unlowered_expr_type (args[i]);
2368 argtypes[i] = NULL_TREE;
2373 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2374 and VQ is either volatile or empty, there exist candidate operator
2375 functions of the form
2376 VQ T& operator++(VQ T&); */
2378 case POSTINCREMENT_EXPR:
2379 case PREINCREMENT_EXPR:
2380 case POSTDECREMENT_EXPR:
2381 case PREDECREMENT_EXPR:
2386 /* 24There also exist candidate operator functions of the form
2387 bool operator!(bool);
2388 bool operator&&(bool, bool);
2389 bool operator||(bool, bool); */
2391 case TRUTH_NOT_EXPR:
2392 build_builtin_candidate
2393 (candidates, fnname, boolean_type_node,
2394 NULL_TREE, args, argtypes, flags);
2397 case TRUTH_ORIF_EXPR:
2398 case TRUTH_ANDIF_EXPR:
2399 build_builtin_candidate
2400 (candidates, fnname, boolean_type_node,
2401 boolean_type_node, args, argtypes, flags);
2423 types[0] = make_tree_vector ();
2424 types[1] = make_tree_vector ();
2426 for (i = 0; i < 2; ++i)
2430 else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
2434 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
2437 convs = lookup_conversions (argtypes[i],
2438 /*lookup_template_convs_p=*/false);
2440 if (code == COND_EXPR)
2442 if (real_lvalue_p (args[i]))
2443 VEC_safe_push (tree, gc, types[i],
2444 build_reference_type (argtypes[i]));
2446 VEC_safe_push (tree, gc, types[i],
2447 TYPE_MAIN_VARIANT (argtypes[i]));
2453 for (; convs; convs = TREE_CHAIN (convs))
2455 type = TREE_TYPE (convs);
2458 && (TREE_CODE (type) != REFERENCE_TYPE
2459 || CP_TYPE_CONST_P (TREE_TYPE (type))))
2462 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2463 VEC_safe_push (tree, gc, types[i], type);
2465 type = non_reference (type);
2466 if (i != 0 || ! ref1)
2468 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2469 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
2470 VEC_safe_push (tree, gc, types[i], type);
2471 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2472 type = type_promotes_to (type);
2475 if (! vec_member (type, types[i]))
2476 VEC_safe_push (tree, gc, types[i], type);
2481 if (code == COND_EXPR && real_lvalue_p (args[i]))
2482 VEC_safe_push (tree, gc, types[i],
2483 build_reference_type (argtypes[i]));
2484 type = non_reference (argtypes[i]);
2485 if (i != 0 || ! ref1)
2487 type = TYPE_MAIN_VARIANT (type_decays_to (type));
2488 if (enum_p && UNSCOPED_ENUM_P (type))
2489 VEC_safe_push (tree, gc, types[i], type);
2490 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
2491 type = type_promotes_to (type);
2493 VEC_safe_push (tree, gc, types[i], type);
2497 /* Run through the possible parameter types of both arguments,
2498 creating candidates with those parameter types. */
2499 FOR_EACH_VEC_ELT_REVERSE (tree, types[0], ix, t)
2504 if (!VEC_empty (tree, types[1]))
2505 FOR_EACH_VEC_ELT_REVERSE (tree, types[1], jx, u)
2506 add_builtin_candidate
2507 (candidates, code, code2, fnname, t,
2508 u, args, argtypes, flags);
2510 add_builtin_candidate
2511 (candidates, code, code2, fnname, t,
2512 NULL_TREE, args, argtypes, flags);
2515 release_tree_vector (types[0]);
2516 release_tree_vector (types[1]);
2520 /* If TMPL can be successfully instantiated as indicated by
2521 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
2523 TMPL is the template. EXPLICIT_TARGS are any explicit template
2524 arguments. ARGLIST is the arguments provided at the call-site.
2525 This does not change ARGLIST. The RETURN_TYPE is the desired type
2526 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
2527 as for add_function_candidate. If an OBJ is supplied, FLAGS and
2528 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
2530 static struct z_candidate*
2531 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
2532 tree ctype, tree explicit_targs, tree first_arg,
2533 const VEC(tree,gc) *arglist, tree return_type,
2534 tree access_path, tree conversion_path,
2535 int flags, tree obj, unification_kind_t strict)
2537 int ntparms = DECL_NTPARMS (tmpl);
2538 tree targs = make_tree_vec (ntparms);
2539 unsigned int len = VEC_length (tree, arglist);
2540 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
2541 unsigned int skip_without_in_chrg = 0;
2542 tree first_arg_without_in_chrg = first_arg;
2543 tree *args_without_in_chrg;
2544 unsigned int nargs_without_in_chrg;
2545 unsigned int ia, ix;
2547 struct z_candidate *cand;
2551 /* We don't do deduction on the in-charge parameter, the VTT
2552 parameter or 'this'. */
2553 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
2555 if (first_arg_without_in_chrg != NULL_TREE)
2556 first_arg_without_in_chrg = NULL_TREE;
2558 ++skip_without_in_chrg;
2561 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
2562 || DECL_BASE_CONSTRUCTOR_P (tmpl))
2563 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
2565 if (first_arg_without_in_chrg != NULL_TREE)
2566 first_arg_without_in_chrg = NULL_TREE;
2568 ++skip_without_in_chrg;
2571 if (len < skip_without_in_chrg)
2574 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
2575 + (len - skip_without_in_chrg));
2576 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
2578 if (first_arg_without_in_chrg != NULL_TREE)
2580 args_without_in_chrg[ia] = first_arg_without_in_chrg;
2583 for (ix = skip_without_in_chrg;
2584 VEC_iterate (tree, arglist, ix, arg);
2587 args_without_in_chrg[ia] = arg;
2590 gcc_assert (ia == nargs_without_in_chrg);
2592 i = fn_type_unification (tmpl, explicit_targs, targs,
2593 args_without_in_chrg,
2594 nargs_without_in_chrg,
2595 return_type, strict, flags);
2600 fn = instantiate_template (tmpl, targs, tf_none);
2601 if (fn == error_mark_node)
2606 A member function template is never instantiated to perform the
2607 copy of a class object to an object of its class type.
2609 It's a little unclear what this means; the standard explicitly
2610 does allow a template to be used to copy a class. For example,
2615 template <class T> A(const T&);
2618 void g () { A a (f ()); }
2620 the member template will be used to make the copy. The section
2621 quoted above appears in the paragraph that forbids constructors
2622 whose only parameter is (a possibly cv-qualified variant of) the
2623 class type, and a logical interpretation is that the intent was
2624 to forbid the instantiation of member templates which would then
2626 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
2628 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
2629 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
2634 if (obj != NULL_TREE)
2635 /* Aha, this is a conversion function. */
2636 cand = add_conv_candidate (candidates, fn, obj, first_arg, arglist,
2637 access_path, conversion_path);
2639 cand = add_function_candidate (candidates, fn, ctype,
2640 first_arg, arglist, access_path,
2641 conversion_path, flags);
2642 if (DECL_TI_TEMPLATE (fn) != tmpl)
2643 /* This situation can occur if a member template of a template
2644 class is specialized. Then, instantiate_template might return
2645 an instantiation of the specialization, in which case the
2646 DECL_TI_TEMPLATE field will point at the original
2647 specialization. For example:
2649 template <class T> struct S { template <class U> void f(U);
2650 template <> void f(int) {}; };
2654 Here, TMPL will be template <class U> S<double>::f(U).
2655 And, instantiate template will give us the specialization
2656 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
2657 for this will point at template <class T> template <> S<T>::f(int),
2658 so that we can find the definition. For the purposes of
2659 overload resolution, however, we want the original TMPL. */
2660 cand->template_decl = build_template_info (tmpl, targs);
2662 cand->template_decl = DECL_TEMPLATE_INFO (fn);
2663 cand->explicit_targs = explicit_targs;
2667 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
2668 access_path, conversion_path, 0);
2672 static struct z_candidate *
2673 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
2674 tree explicit_targs, tree first_arg,
2675 const VEC(tree,gc) *arglist, tree return_type,
2676 tree access_path, tree conversion_path, int flags,
2677 unification_kind_t strict)
2680 add_template_candidate_real (candidates, tmpl, ctype,
2681 explicit_targs, first_arg, arglist,
2682 return_type, access_path, conversion_path,
2683 flags, NULL_TREE, strict);
2687 static struct z_candidate *
2688 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
2689 tree obj, tree first_arg,
2690 const VEC(tree,gc) *arglist,
2691 tree return_type, tree access_path,
2692 tree conversion_path)
2695 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
2696 first_arg, arglist, return_type, access_path,
2697 conversion_path, 0, obj, DEDUCE_CONV);
2700 /* The CANDS are the set of candidates that were considered for
2701 overload resolution. Return the set of viable candidates, or CANDS
2702 if none are viable. If any of the candidates were viable, set
2703 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
2704 considered viable only if it is strictly viable. */
2706 static struct z_candidate*
2707 splice_viable (struct z_candidate *cands,
2711 struct z_candidate *viable;
2712 struct z_candidate **last_viable;
2713 struct z_candidate **cand;
2716 last_viable = &viable;
2717 *any_viable_p = false;
2722 struct z_candidate *c = *cand;
2723 if (strict_p ? c->viable == 1 : c->viable)
2728 last_viable = &c->next;
2729 *any_viable_p = true;
2735 return viable ? viable : cands;
2739 any_strictly_viable (struct z_candidate *cands)
2741 for (; cands; cands = cands->next)
2742 if (cands->viable == 1)
2747 /* OBJ is being used in an expression like "OBJ.f (...)". In other
2748 words, it is about to become the "this" pointer for a member
2749 function call. Take the address of the object. */
2752 build_this (tree obj)
2754 /* In a template, we are only concerned about the type of the
2755 expression, so we can take a shortcut. */
2756 if (processing_template_decl)
2757 return build_address (obj);
2759 return cp_build_addr_expr (obj, tf_warning_or_error);
2762 /* Returns true iff functions are equivalent. Equivalent functions are
2763 not '==' only if one is a function-local extern function or if
2764 both are extern "C". */
2767 equal_functions (tree fn1, tree fn2)
2769 if (TREE_CODE (fn1) != TREE_CODE (fn2))
2771 if (TREE_CODE (fn1) == TEMPLATE_DECL)
2773 if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
2774 || DECL_EXTERN_C_FUNCTION_P (fn1))
2775 return decls_match (fn1, fn2);
2779 /* Print information about one overload candidate CANDIDATE. MSGSTR
2780 is the text to print before the candidate itself.
2782 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
2783 to have been run through gettext by the caller. This wart makes
2784 life simpler in print_z_candidates and for the translators. */
2787 print_z_candidate (const char *msgstr, struct z_candidate *candidate)
2789 if (TREE_CODE (candidate->fn) == IDENTIFIER_NODE)
2791 if (candidate->num_convs == 3)
2792 inform (input_location, "%s %D(%T, %T, %T) <built-in>", msgstr, candidate->fn,
2793 candidate->convs[0]->type,
2794 candidate->convs[1]->type,
2795 candidate->convs[2]->type);
2796 else if (candidate->num_convs == 2)
2797 inform (input_location, "%s %D(%T, %T) <built-in>", msgstr, candidate->fn,
2798 candidate->convs[0]->type,
2799 candidate->convs[1]->type);
2801 inform (input_location, "%s %D(%T) <built-in>", msgstr, candidate->fn,
2802 candidate->convs[0]->type);
2804 else if (TYPE_P (candidate->fn))
2805 inform (input_location, "%s %T <conversion>", msgstr, candidate->fn);
2806 else if (candidate->viable == -1)
2807 inform (input_location, "%s %+#D <near match>", msgstr, candidate->fn);
2808 else if (DECL_DELETED_FN (STRIP_TEMPLATE (candidate->fn)))
2809 inform (input_location, "%s %+#D <deleted>", msgstr, candidate->fn);
2811 inform (input_location, "%s %+#D", msgstr, candidate->fn);
2815 print_z_candidates (struct z_candidate *candidates)
2818 struct z_candidate *cand1;
2819 struct z_candidate **cand2;
2825 /* Remove non-viable deleted candidates. */
2827 for (cand2 = &cand1; *cand2; )
2829 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
2830 && !(*cand2)->viable
2831 && DECL_DELETED_FN ((*cand2)->fn))
2832 *cand2 = (*cand2)->next;
2834 cand2 = &(*cand2)->next;
2836 /* ...if there are any non-deleted ones. */
2840 /* There may be duplicates in the set of candidates. We put off
2841 checking this condition as long as possible, since we have no way
2842 to eliminate duplicates from a set of functions in less than n^2
2843 time. Now we are about to emit an error message, so it is more
2844 permissible to go slowly. */
2845 for (cand1 = candidates; cand1; cand1 = cand1->next)
2847 tree fn = cand1->fn;
2848 /* Skip builtin candidates and conversion functions. */
2851 cand2 = &cand1->next;
2854 if (DECL_P ((*cand2)->fn)
2855 && equal_functions (fn, (*cand2)->fn))
2856 *cand2 = (*cand2)->next;
2858 cand2 = &(*cand2)->next;
2862 str = candidates->next ? _("candidates are:") : _("candidate is:");
2864 for (; candidates; candidates = candidates->next)
2866 print_z_candidate (spaces ? spaces : str, candidates);
2867 spaces = spaces ? spaces : get_spaces (str);
2872 /* USER_SEQ is a user-defined conversion sequence, beginning with a
2873 USER_CONV. STD_SEQ is the standard conversion sequence applied to
2874 the result of the conversion function to convert it to the final
2875 desired type. Merge the two sequences into a single sequence,
2876 and return the merged sequence. */
2879 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
2883 gcc_assert (user_seq->kind == ck_user);
2885 /* Find the end of the second conversion sequence. */
2887 while ((*t)->kind != ck_identity)
2888 t = &((*t)->u.next);
2890 /* Replace the identity conversion with the user conversion
2894 /* The entire sequence is a user-conversion sequence. */
2895 std_seq->user_conv_p = true;
2900 /* Handle overload resolution for initializing an object of class type from
2901 an initializer list. First we look for a suitable constructor that
2902 takes a std::initializer_list; if we don't find one, we then look for a
2903 non-list constructor.
2905 Parameters are as for add_candidates, except that the arguments are in
2906 the form of a CONSTRUCTOR (the initializer list) rather than a VEC, and
2907 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
2910 add_list_candidates (tree fns, tree first_arg,
2911 tree init_list, tree totype,
2912 tree explicit_targs, bool template_only,
2913 tree conversion_path, tree access_path,
2915 struct z_candidate **candidates)
2919 gcc_assert (*candidates == NULL);
2921 /* For list-initialization we consider explicit constructors, but
2922 give an error if one is selected. */
2923 flags &= ~LOOKUP_ONLYCONVERTING;
2924 /* And we don't allow narrowing conversions. We also use this flag to
2925 avoid the copy constructor call for copy-list-initialization. */
2926 flags |= LOOKUP_NO_NARROWING;
2928 /* Always use the default constructor if the list is empty (DR 990). */
2929 if (CONSTRUCTOR_NELTS (init_list) == 0
2930 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
2932 /* If the class has a list ctor, try passing the list as a single
2933 argument first, but only consider list ctors. */
2934 else if (TYPE_HAS_LIST_CTOR (totype))
2936 flags |= LOOKUP_LIST_ONLY;
2937 args = make_tree_vector_single (init_list);
2938 add_candidates (fns, first_arg, args, NULL_TREE,
2939 explicit_targs, template_only, conversion_path,
2940 access_path, flags, candidates);
2941 if (any_strictly_viable (*candidates))
2945 args = ctor_to_vec (init_list);
2947 /* We aren't looking for list-ctors anymore. */
2948 flags &= ~LOOKUP_LIST_ONLY;
2949 /* We allow more user-defined conversions within an init-list. */
2950 flags &= ~LOOKUP_NO_CONVERSION;
2951 /* But not for the copy ctor. */
2952 flags |= LOOKUP_NO_COPY_CTOR_CONVERSION;
2954 add_candidates (fns, first_arg, args, NULL_TREE,
2955 explicit_targs, template_only, conversion_path,
2956 access_path, flags, candidates);
2959 /* Returns the best overload candidate to perform the requested
2960 conversion. This function is used for three the overloading situations
2961 described in [over.match.copy], [over.match.conv], and [over.match.ref].
2962 If TOTYPE is a REFERENCE_TYPE, we're trying to find an lvalue binding as
2963 per [dcl.init.ref], so we ignore temporary bindings. */
2965 static struct z_candidate *
2966 build_user_type_conversion_1 (tree totype, tree expr, int flags)
2968 struct z_candidate *candidates, *cand;
2969 tree fromtype = TREE_TYPE (expr);
2970 tree ctors = NULL_TREE;
2971 tree conv_fns = NULL_TREE;
2972 conversion *conv = NULL;
2973 tree first_arg = NULL_TREE;
2974 VEC(tree,gc) *args = NULL;
2978 /* We represent conversion within a hierarchy using RVALUE_CONV and
2979 BASE_CONV, as specified by [over.best.ics]; these become plain
2980 constructor calls, as specified in [dcl.init]. */
2981 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
2982 || !DERIVED_FROM_P (totype, fromtype));
2984 if (MAYBE_CLASS_TYPE_P (totype))
2985 ctors = lookup_fnfields (totype, complete_ctor_identifier, 0);
2987 if (MAYBE_CLASS_TYPE_P (fromtype))
2989 tree to_nonref = non_reference (totype);
2990 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
2991 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
2992 && DERIVED_FROM_P (to_nonref, fromtype)))
2994 /* [class.conv.fct] A conversion function is never used to
2995 convert a (possibly cv-qualified) object to the (possibly
2996 cv-qualified) same object type (or a reference to it), to a
2997 (possibly cv-qualified) base class of that type (or a
2998 reference to it)... */
3001 conv_fns = lookup_conversions (fromtype,
3002 /*lookup_template_convs_p=*/true);
3006 flags |= LOOKUP_NO_CONVERSION;
3007 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3008 flags |= LOOKUP_NO_NARROWING;
3010 /* It's OK to bind a temporary for converting constructor arguments, but
3011 not in converting the return value of a conversion operator. */
3012 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION);
3013 flags &= ~LOOKUP_NO_TEMP_BIND;
3017 int ctorflags = flags;
3018 ctors = BASELINK_FUNCTIONS (ctors);
3020 first_arg = build_int_cst (build_pointer_type (totype), 0);
3022 /* We should never try to call the abstract or base constructor
3024 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
3025 && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
3027 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
3029 /* List-initialization. */
3030 add_list_candidates (ctors, first_arg, expr, totype, NULL_TREE,
3031 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
3032 ctorflags, &candidates);
3036 args = make_tree_vector_single (expr);
3037 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
3038 TYPE_BINFO (totype), TYPE_BINFO (totype),
3039 ctorflags, &candidates);
3042 for (cand = candidates; cand; cand = cand->next)
3044 cand->second_conv = build_identity_conv (totype, NULL_TREE);
3046 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
3047 set, then this is copy-initialization. In that case, "The
3048 result of the call is then used to direct-initialize the
3049 object that is the destination of the copy-initialization."
3052 We represent this in the conversion sequence with an
3053 rvalue conversion, which means a constructor call. */
3054 if (TREE_CODE (totype) != REFERENCE_TYPE
3055 && !(convflags & LOOKUP_NO_TEMP_BIND))
3057 = build_conv (ck_rvalue, totype, cand->second_conv);
3062 first_arg = build_this (expr);
3064 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
3066 tree conversion_path = TREE_PURPOSE (conv_fns);
3067 struct z_candidate *old_candidates;
3069 /* If we are called to convert to a reference type, we are trying to
3070 find an lvalue binding, so don't even consider temporaries. If
3071 we don't find an lvalue binding, the caller will try again to
3072 look for a temporary binding. */
3073 if (TREE_CODE (totype) == REFERENCE_TYPE)
3074 convflags |= LOOKUP_NO_TEMP_BIND;
3076 old_candidates = candidates;
3077 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
3079 conversion_path, TYPE_BINFO (fromtype),
3080 flags, &candidates);
3082 for (cand = candidates; cand != old_candidates; cand = cand->next)
3085 = implicit_conversion (totype,
3086 TREE_TYPE (TREE_TYPE (cand->fn)),
3088 /*c_cast_p=*/false, convflags);
3090 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
3091 copy-initialization. In that case, "The result of the
3092 call is then used to direct-initialize the object that is
3093 the destination of the copy-initialization." [dcl.init]
3095 We represent this in the conversion sequence with an
3096 rvalue conversion, which means a constructor call. But
3097 don't add a second rvalue conversion if there's already
3098 one there. Which there really shouldn't be, but it's
3099 harmless since we'd add it here anyway. */
3100 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
3101 && !(convflags & LOOKUP_NO_TEMP_BIND))
3102 ics = build_conv (ck_rvalue, totype, ics);
3104 cand->second_conv = ics;
3108 else if (cand->viable == 1 && ics->bad_p)
3113 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3117 cand = tourney (candidates);
3120 if (flags & LOOKUP_COMPLAIN)
3122 error ("conversion from %qT to %qT is ambiguous",
3124 print_z_candidates (candidates);
3127 cand = candidates; /* any one will do */
3128 cand->second_conv = build_ambiguous_conv (totype, expr);
3129 cand->second_conv->user_conv_p = true;
3130 if (!any_strictly_viable (candidates))
3131 cand->second_conv->bad_p = true;
3132 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
3133 ambiguous conversion is no worse than another user-defined
3139 /* Build the user conversion sequence. */
3142 (DECL_CONSTRUCTOR_P (cand->fn)
3143 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
3144 build_identity_conv (TREE_TYPE (expr), expr));
3147 /* Remember that this was a list-initialization. */
3148 if (flags & LOOKUP_NO_NARROWING)
3149 conv->check_narrowing = true;
3151 /* Combine it with the second conversion sequence. */
3152 cand->second_conv = merge_conversion_sequences (conv,
3155 if (cand->viable == -1)
3156 cand->second_conv->bad_p = true;
3162 build_user_type_conversion (tree totype, tree expr, int flags)
3164 struct z_candidate *cand
3165 = build_user_type_conversion_1 (totype, expr, flags);
3169 if (cand->second_conv->kind == ck_ambig)
3170 return error_mark_node;
3171 expr = convert_like (cand->second_conv, expr, tf_warning_or_error);
3172 return convert_from_reference (expr);
3177 /* Subroutine of convert_nontype_argument.
3179 EXPR is an argument for a template non-type parameter of integral or
3180 enumeration type. Do any necessary conversions (that are permitted for
3181 non-type arguments) to convert it to the parameter type.
3183 If conversion is successful, returns the converted expression;
3184 otherwise, returns error_mark_node. */
3187 build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
3193 if (error_operand_p (expr))
3194 return error_mark_node;
3196 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3198 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3199 p = conversion_obstack_alloc (0);
3201 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
3205 /* for a non-type template-parameter of integral or
3206 enumeration type, integral promotions (4.5) and integral
3207 conversions (4.7) are applied. */
3208 /* It should be sufficient to check the outermost conversion step, since
3209 there are no qualification conversions to integer type. */
3213 /* A conversion function is OK. If it isn't constexpr, we'll
3214 complain later that the argument isn't constant. */
3216 /* The lvalue-to-rvalue conversion is OK. */
3222 t = conv->u.next->type;
3223 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
3226 if (complain & tf_error)
3227 error ("conversion from %qT to %qT not considered for "
3228 "non-type template argument", t, type);
3229 /* and fall through. */
3237 expr = convert_like (conv, expr, complain);
3239 expr = error_mark_node;
3241 /* Free all the conversions we allocated. */
3242 obstack_free (&conversion_obstack, p);
3247 /* Do any initial processing on the arguments to a function call. */
3249 static VEC(tree,gc) *
3250 resolve_args (VEC(tree,gc) *args)
3255 FOR_EACH_VEC_ELT (tree, args, ix, arg)
3257 if (error_operand_p (arg))
3259 else if (VOID_TYPE_P (TREE_TYPE (arg)))
3261 error ("invalid use of void expression");
3264 else if (invalid_nonstatic_memfn_p (arg, tf_warning_or_error))
3270 /* Perform overload resolution on FN, which is called with the ARGS.
3272 Return the candidate function selected by overload resolution, or
3273 NULL if the event that overload resolution failed. In the case
3274 that overload resolution fails, *CANDIDATES will be the set of
3275 candidates considered, and ANY_VIABLE_P will be set to true or
3276 false to indicate whether or not any of the candidates were
3279 The ARGS should already have gone through RESOLVE_ARGS before this
3280 function is called. */
3282 static struct z_candidate *
3283 perform_overload_resolution (tree fn,
3284 const VEC(tree,gc) *args,
3285 struct z_candidate **candidates,
3288 struct z_candidate *cand;
3289 tree explicit_targs = NULL_TREE;
3290 int template_only = 0;
3293 *any_viable_p = true;
3296 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
3297 || TREE_CODE (fn) == TEMPLATE_DECL
3298 || TREE_CODE (fn) == OVERLOAD
3299 || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
3301 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3303 explicit_targs = TREE_OPERAND (fn, 1);
3304 fn = TREE_OPERAND (fn, 0);
3308 /* Add the various candidate functions. */
3309 add_candidates (fn, NULL_TREE, args, NULL_TREE,
3310 explicit_targs, template_only,
3311 /*conversion_path=*/NULL_TREE,
3312 /*access_path=*/NULL_TREE,
3316 *candidates = splice_viable (*candidates, pedantic, any_viable_p);
3320 cand = tourney (*candidates);
3324 /* Return an expression for a call to FN (a namespace-scope function,
3325 or a static member function) with the ARGS. This may change
3329 build_new_function_call (tree fn, VEC(tree,gc) **args, bool koenig_p,
3330 tsubst_flags_t complain)
3332 struct z_candidate *candidates, *cand;
3337 if (args != NULL && *args != NULL)
3339 *args = resolve_args (*args);
3341 return error_mark_node;
3344 /* If this function was found without using argument dependent
3345 lookup, then we want to ignore any undeclared friend
3351 fn = remove_hidden_names (fn);
3354 if (complain & tf_error)
3355 error ("no matching function for call to %<%D(%A)%>",
3356 DECL_NAME (OVL_CURRENT (orig_fn)),
3357 build_tree_list_vec (*args));
3358 return error_mark_node;
3362 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3363 p = conversion_obstack_alloc (0);
3365 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p);
3369 if (complain & tf_error)
3371 if (!any_viable_p && candidates && ! candidates->next
3372 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
3373 return cp_build_function_call_vec (candidates->fn, args, complain);
3374 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3375 fn = TREE_OPERAND (fn, 0);
3377 error ("no matching function for call to %<%D(%A)%>",
3378 DECL_NAME (OVL_CURRENT (fn)), build_tree_list_vec (*args));
3380 error ("call of overloaded %<%D(%A)%> is ambiguous",
3381 DECL_NAME (OVL_CURRENT (fn)), build_tree_list_vec (*args));
3383 print_z_candidates (candidates);
3385 result = error_mark_node;
3388 result = build_over_call (cand, LOOKUP_NORMAL, complain);
3390 /* Free all the conversions we allocated. */
3391 obstack_free (&conversion_obstack, p);
3396 /* Build a call to a global operator new. FNNAME is the name of the
3397 operator (either "operator new" or "operator new[]") and ARGS are
3398 the arguments provided. This may change ARGS. *SIZE points to the
3399 total number of bytes required by the allocation, and is updated if
3400 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
3401 be used. If this function determines that no cookie should be
3402 used, after all, *COOKIE_SIZE is set to NULL_TREE. If FN is
3403 non-NULL, it will be set, upon return, to the allocation function
3407 build_operator_new_call (tree fnname, VEC(tree,gc) **args,
3408 tree *size, tree *cookie_size,
3412 struct z_candidate *candidates;
3413 struct z_candidate *cand;
3418 VEC_safe_insert (tree, gc, *args, 0, *size);
3419 *args = resolve_args (*args);
3421 return error_mark_node;
3427 If this lookup fails to find the name, or if the allocated type
3428 is not a class type, the allocation function's name is looked
3429 up in the global scope.
3431 we disregard block-scope declarations of "operator new". */
3432 fns = lookup_function_nonclass (fnname, *args, /*block_p=*/false);
3434 /* Figure out what function is being called. */
3435 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p);
3437 /* If no suitable function could be found, issue an error message
3442 error ("no matching function for call to %<%D(%A)%>",
3443 DECL_NAME (OVL_CURRENT (fns)), build_tree_list_vec (*args));
3445 error ("call of overloaded %<%D(%A)%> is ambiguous",
3446 DECL_NAME (OVL_CURRENT (fns)), build_tree_list_vec (*args));
3448 print_z_candidates (candidates);
3449 return error_mark_node;
3452 /* If a cookie is required, add some extra space. Whether
3453 or not a cookie is required cannot be determined until
3454 after we know which function was called. */
3457 bool use_cookie = true;
3458 if (!abi_version_at_least (2))
3460 /* In G++ 3.2, the check was implemented incorrectly; it
3461 looked at the placement expression, rather than the
3462 type of the function. */
3463 if (VEC_length (tree, *args) == 2
3464 && same_type_p (TREE_TYPE (VEC_index (tree, *args, 1)),
3472 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
3473 /* Skip the size_t parameter. */
3474 arg_types = TREE_CHAIN (arg_types);
3475 /* Check the remaining parameters (if any). */
3477 && TREE_CHAIN (arg_types) == void_list_node
3478 && same_type_p (TREE_VALUE (arg_types),
3482 /* If we need a cookie, adjust the number of bytes allocated. */
3485 /* Update the total size. */
3486 *size = size_binop (PLUS_EXPR, *size, *cookie_size);
3487 /* Update the argument list to reflect the adjusted size. */
3488 VEC_replace (tree, *args, 0, *size);
3491 *cookie_size = NULL_TREE;
3494 /* Tell our caller which function we decided to call. */
3498 /* Build the CALL_EXPR. */
3499 return build_over_call (cand, LOOKUP_NORMAL, tf_warning_or_error);
3502 /* Build a new call to operator(). This may change ARGS. */
3505 build_op_call (tree obj, VEC(tree,gc) **args, tsubst_flags_t complain)
3507 struct z_candidate *candidates = 0, *cand;
3508 tree fns, convs, first_mem_arg = NULL_TREE;
3509 tree type = TREE_TYPE (obj);
3511 tree result = NULL_TREE;
3514 if (error_operand_p (obj))
3515 return error_mark_node;
3517 obj = prep_operand (obj);
3519 if (TYPE_PTRMEMFUNC_P (type))
3521 if (complain & tf_error)
3522 /* It's no good looking for an overloaded operator() on a
3523 pointer-to-member-function. */
3524 error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
3525 return error_mark_node;
3528 if (TYPE_BINFO (type))
3530 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
3531 if (fns == error_mark_node)
3532 return error_mark_node;
3537 if (args != NULL && *args != NULL)
3539 *args = resolve_args (*args);
3541 return error_mark_node;
3544 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3545 p = conversion_obstack_alloc (0);
3549 first_mem_arg = build_this (obj);
3551 add_candidates (BASELINK_FUNCTIONS (fns),
3552 first_mem_arg, *args, NULL_TREE,
3554 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
3555 LOOKUP_NORMAL, &candidates);
3558 convs = lookup_conversions (type, /*lookup_template_convs_p=*/true);
3560 for (; convs; convs = TREE_CHAIN (convs))
3562 tree fns = TREE_VALUE (convs);
3563 tree totype = TREE_TYPE (convs);
3565 if ((TREE_CODE (totype) == POINTER_TYPE
3566 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3567 || (TREE_CODE (totype) == REFERENCE_TYPE
3568 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3569 || (TREE_CODE (totype) == REFERENCE_TYPE
3570 && TREE_CODE (TREE_TYPE (totype)) == POINTER_TYPE
3571 && TREE_CODE (TREE_TYPE (TREE_TYPE (totype))) == FUNCTION_TYPE))
3572 for (; fns; fns = OVL_NEXT (fns))
3574 tree fn = OVL_CURRENT (fns);
3576 if (DECL_NONCONVERTING_P (fn))
3579 if (TREE_CODE (fn) == TEMPLATE_DECL)
3580 add_template_conv_candidate
3581 (&candidates, fn, obj, NULL_TREE, *args, totype,
3582 /*access_path=*/NULL_TREE,
3583 /*conversion_path=*/NULL_TREE);
3585 add_conv_candidate (&candidates, fn, obj, NULL_TREE,
3586 *args, /*conversion_path=*/NULL_TREE,
3587 /*access_path=*/NULL_TREE);
3591 candidates = splice_viable (candidates, pedantic, &any_viable_p);
3594 if (complain & tf_error)
3596 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
3597 build_tree_list_vec (*args));
3598 print_z_candidates (candidates);
3600 result = error_mark_node;
3604 cand = tourney (candidates);
3607 if (complain & tf_error)
3609 error ("call of %<(%T) (%A)%> is ambiguous",
3610 TREE_TYPE (obj), build_tree_list_vec (*args));
3611 print_z_candidates (candidates);
3613 result = error_mark_node;
3615 /* Since cand->fn will be a type, not a function, for a conversion
3616 function, we must be careful not to unconditionally look at
3618 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
3619 && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
3620 result = build_over_call (cand, LOOKUP_NORMAL, complain);
3623 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1,
3625 obj = convert_from_reference (obj);
3626 result = cp_build_function_call_vec (obj, args, complain);
3630 /* Free all the conversions we allocated. */
3631 obstack_free (&conversion_obstack, p);
3637 op_error (enum tree_code code, enum tree_code code2,
3638 tree arg1, tree arg2, tree arg3, bool match)
3642 if (code == MODIFY_EXPR)
3643 opname = assignment_operator_name_info[code2].name;
3645 opname = operator_name_info[code].name;
3651 error ("ambiguous overload for ternary %<operator?:%> "
3652 "in %<%E ? %E : %E%>", arg1, arg2, arg3);
3654 error ("no match for ternary %<operator?:%> "
3655 "in %<%E ? %E : %E%>", arg1, arg2, arg3);
3658 case POSTINCREMENT_EXPR:
3659 case POSTDECREMENT_EXPR:
3661 error ("ambiguous overload for %<operator%s%> in %<%E%s%>",
3662 opname, arg1, opname);
3664 error ("no match for %<operator%s%> in %<%E%s%>",
3665 opname, arg1, opname);
3670 error ("ambiguous overload for %<operator[]%> in %<%E[%E]%>",
3673 error ("no match for %<operator[]%> in %<%E[%E]%>",
3680 error ("ambiguous overload for %qs in %<%s %E%>",
3681 opname, opname, arg1);
3683 error ("no match for %qs in %<%s %E%>",
3684 opname, opname, arg1);
3690 error ("ambiguous overload for %<operator%s%> in %<%E %s %E%>",
3691 opname, arg1, opname, arg2);
3693 error ("no match for %<operator%s%> in %<%E %s %E%>",
3694 opname, arg1, opname, arg2);
3697 error ("ambiguous overload for %<operator%s%> in %<%s%E%>",
3698 opname, opname, arg1);
3700 error ("no match for %<operator%s%> in %<%s%E%>",
3701 opname, opname, arg1);
3706 /* Return the implicit conversion sequence that could be used to
3707 convert E1 to E2 in [expr.cond]. */
3710 conditional_conversion (tree e1, tree e2)
3712 tree t1 = non_reference (TREE_TYPE (e1));
3713 tree t2 = non_reference (TREE_TYPE (e2));
3719 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
3720 implicitly converted (clause _conv_) to the type "reference to
3721 T2", subject to the constraint that in the conversion the
3722 reference must bind directly (_dcl.init.ref_) to E1. */
3723 if (real_lvalue_p (e2))
3725 conv = implicit_conversion (build_reference_type (t2),
3729 LOOKUP_NO_TEMP_BIND|LOOKUP_ONLYCONVERTING);
3736 If E1 and E2 have class type, and the underlying class types are
3737 the same or one is a base class of the other: E1 can be converted
3738 to match E2 if the class of T2 is the same type as, or a base
3739 class of, the class of T1, and the cv-qualification of T2 is the
3740 same cv-qualification as, or a greater cv-qualification than, the
3741 cv-qualification of T1. If the conversion is applied, E1 is
3742 changed to an rvalue of type T2 that still refers to the original
3743 source class object (or the appropriate subobject thereof). */
3744 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
3745 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
3747 if (good_base && at_least_as_qualified_p (t2, t1))
3749 conv = build_identity_conv (t1, e1);
3750 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
3751 TYPE_MAIN_VARIANT (t2)))
3752 conv = build_conv (ck_base, t2, conv);
3754 conv = build_conv (ck_rvalue, t2, conv);
3763 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
3764 converted to the type that expression E2 would have if E2 were
3765 converted to an rvalue (or the type it has, if E2 is an rvalue). */
3766 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
3770 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
3771 arguments to the conditional expression. */
3774 build_conditional_expr (tree arg1, tree arg2, tree arg3,
3775 tsubst_flags_t complain)
3779 tree result = NULL_TREE;
3780 tree result_type = NULL_TREE;
3781 bool lvalue_p = true;
3782 struct z_candidate *candidates = 0;
3783 struct z_candidate *cand;
3786 /* As a G++ extension, the second argument to the conditional can be
3787 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
3788 c'.) If the second operand is omitted, make sure it is
3789 calculated only once. */
3792 if (complain & tf_error)
3793 pedwarn (input_location, OPT_pedantic,
3794 "ISO C++ forbids omitting the middle term of a ?: expression");
3796 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
3797 if (real_lvalue_p (arg1))
3798 arg2 = arg1 = stabilize_reference (arg1);
3800 arg2 = arg1 = save_expr (arg1);
3805 The first expression is implicitly converted to bool (clause
3807 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
3810 /* If something has already gone wrong, just pass that fact up the
3812 if (error_operand_p (arg1)
3813 || error_operand_p (arg2)
3814 || error_operand_p (arg3))
3815 return error_mark_node;
3819 If either the second or the third operand has type (possibly
3820 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
3821 array-to-pointer (_conv.array_), and function-to-pointer
3822 (_conv.func_) standard conversions are performed on the second
3823 and third operands. */
3824 arg2_type = unlowered_expr_type (arg2);
3825 arg3_type = unlowered_expr_type (arg3);
3826 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
3828 /* Do the conversions. We don't these for `void' type arguments
3829 since it can't have any effect and since decay_conversion
3830 does not handle that case gracefully. */
3831 if (!VOID_TYPE_P (arg2_type))
3832 arg2 = decay_conversion (arg2);
3833 if (!VOID_TYPE_P (arg3_type))
3834 arg3 = decay_conversion (arg3);
3835 arg2_type = TREE_TYPE (arg2);
3836 arg3_type = TREE_TYPE (arg3);
3840 One of the following shall hold:
3842 --The second or the third operand (but not both) is a
3843 throw-expression (_except.throw_); the result is of the
3844 type of the other and is an rvalue.
3846 --Both the second and the third operands have type void; the
3847 result is of type void and is an rvalue.
3849 We must avoid calling force_rvalue for expressions of type
3850 "void" because it will complain that their value is being
3852 if (TREE_CODE (arg2) == THROW_EXPR
3853 && TREE_CODE (arg3) != THROW_EXPR)
3855 if (!VOID_TYPE_P (arg3_type))
3856 arg3 = force_rvalue (arg3);
3857 arg3_type = TREE_TYPE (arg3);
3858 result_type = arg3_type;
3860 else if (TREE_CODE (arg2) != THROW_EXPR
3861 && TREE_CODE (arg3) == THROW_EXPR)
3863 if (!VOID_TYPE_P (arg2_type))
3864 arg2 = force_rvalue (arg2);
3865 arg2_type = TREE_TYPE (arg2);
3866 result_type = arg2_type;
3868 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
3869 result_type = void_type_node;
3872 if (complain & tf_error)
3874 if (VOID_TYPE_P (arg2_type))
3875 error ("second operand to the conditional operator "
3876 "is of type %<void%>, "
3877 "but the third operand is neither a throw-expression "
3878 "nor of type %<void%>");
3880 error ("third operand to the conditional operator "
3881 "is of type %<void%>, "
3882 "but the second operand is neither a throw-expression "
3883 "nor of type %<void%>");
3885 return error_mark_node;
3889 goto valid_operands;
3893 Otherwise, if the second and third operand have different types,
3894 and either has (possibly cv-qualified) class type, an attempt is
3895 made to convert each of those operands to the type of the other. */
3896 else if (!same_type_p (arg2_type, arg3_type)
3897 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
3902 /* Get the high-water mark for the CONVERSION_OBSTACK. */
3903 p = conversion_obstack_alloc (0);
3905 conv2 = conditional_conversion (arg2, arg3);
3906 conv3 = conditional_conversion (arg3, arg2);
3910 If both can be converted, or one can be converted but the
3911 conversion is ambiguous, the program is ill-formed. If
3912 neither can be converted, the operands are left unchanged and
3913 further checking is performed as described below. If exactly
3914 one conversion is possible, that conversion is applied to the
3915 chosen operand and the converted operand is used in place of
3916 the original operand for the remainder of this section. */
3917 if ((conv2 && !conv2->bad_p
3918 && conv3 && !conv3->bad_p)
3919 || (conv2 && conv2->kind == ck_ambig)
3920 || (conv3 && conv3->kind == ck_ambig))
3922 error ("operands to ?: have different types %qT and %qT",
3923 arg2_type, arg3_type);
3924 result = error_mark_node;
3926 else if (conv2 && (!conv2->bad_p || !conv3))
3928 arg2 = convert_like (conv2, arg2, complain);
3929 arg2 = convert_from_reference (arg2);
3930 arg2_type = TREE_TYPE (arg2);
3931 /* Even if CONV2 is a valid conversion, the result of the
3932 conversion may be invalid. For example, if ARG3 has type
3933 "volatile X", and X does not have a copy constructor
3934 accepting a "volatile X&", then even if ARG2 can be
3935 converted to X, the conversion will fail. */
3936 if (error_operand_p (arg2))
3937 result = error_mark_node;
3939 else if (conv3 && (!conv3->bad_p || !conv2))
3941 arg3 = convert_like (conv3, arg3, complain);
3942 arg3 = convert_from_reference (arg3);
3943 arg3_type = TREE_TYPE (arg3);
3944 if (error_operand_p (arg3))
3945 result = error_mark_node;
3948 /* Free all the conversions we allocated. */
3949 obstack_free (&conversion_obstack, p);
3954 /* If, after the conversion, both operands have class type,
3955 treat the cv-qualification of both operands as if it were the
3956 union of the cv-qualification of the operands.
3958 The standard is not clear about what to do in this
3959 circumstance. For example, if the first operand has type
3960 "const X" and the second operand has a user-defined
3961 conversion to "volatile X", what is the type of the second
3962 operand after this step? Making it be "const X" (matching
3963 the first operand) seems wrong, as that discards the
3964 qualification without actually performing a copy. Leaving it
3965 as "volatile X" seems wrong as that will result in the
3966 conditional expression failing altogether, even though,
3967 according to this step, the one operand could be converted to
3968 the type of the other. */
3969 if ((conv2 || conv3)
3970 && CLASS_TYPE_P (arg2_type)
3971 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
3972 arg2_type = arg3_type =
3973 cp_build_qualified_type (arg2_type,
3974 cp_type_quals (arg2_type)
3975 | cp_type_quals (arg3_type));
3980 If the second and third operands are lvalues and have the same
3981 type, the result is of that type and is an lvalue. */
3982 if (real_lvalue_p (arg2)
3983 && real_lvalue_p (arg3)
3984 && same_type_p (arg2_type, arg3_type))
3986 result_type = arg2_type;
3987 arg2 = mark_lvalue_use (arg2);
3988 arg3 = mark_lvalue_use (arg3);
3989 goto valid_operands;
3994 Otherwise, the result is an rvalue. If the second and third
3995 operand do not have the same type, and either has (possibly
3996 cv-qualified) class type, overload resolution is used to
3997 determine the conversions (if any) to be applied to the operands
3998 (_over.match.oper_, _over.built_). */
4000 if (!same_type_p (arg2_type, arg3_type)
4001 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
4007 /* Rearrange the arguments so that add_builtin_candidate only has
4008 to know about two args. In build_builtin_candidate, the
4009 arguments are unscrambled. */
4013 add_builtin_candidates (&candidates,
4016 ansi_opname (COND_EXPR),
4022 If the overload resolution fails, the program is
4024 candidates = splice_viable (candidates, pedantic, &any_viable_p);
4027 if (complain & tf_error)
4029 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4030 print_z_candidates (candidates);
4032 return error_mark_node;
4034 cand = tourney (candidates);
4037 if (complain & tf_error)
4039 op_error (COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
4040 print_z_candidates (candidates);
4042 return error_mark_node;
4047 Otherwise, the conversions thus determined are applied, and
4048 the converted operands are used in place of the original
4049 operands for the remainder of this section. */
4050 conv = cand->convs[0];
4051 arg1 = convert_like (conv, arg1, complain);
4052 conv = cand->convs[1];
4053 arg2 = convert_like (conv, arg2, complain);
4054 arg2_type = TREE_TYPE (arg2);
4055 conv = cand->convs[2];
4056 arg3 = convert_like (conv, arg3, complain);
4057 arg3_type = TREE_TYPE (arg3);
4062 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
4063 and function-to-pointer (_conv.func_) standard conversions are
4064 performed on the second and third operands.
4066 We need to force the lvalue-to-rvalue conversion here for class types,
4067 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
4068 that isn't wrapped with a TARGET_EXPR plays havoc with exception
4071 arg2 = force_rvalue (arg2);
4072 if (!CLASS_TYPE_P (arg2_type))
4073 arg2_type = TREE_TYPE (arg2);
4075 arg3 = force_rvalue (arg3);
4076 if (!CLASS_TYPE_P (arg3_type))
4077 arg3_type = TREE_TYPE (arg3);
4079 if (arg2 == error_mark_node || arg3 == error_mark_node)
4080 return error_mark_node;
4084 After those conversions, one of the following shall hold:
4086 --The second and third operands have the same type; the result is of
4088 if (same_type_p (arg2_type, arg3_type))
4089 result_type = arg2_type;
4092 --The second and third operands have arithmetic or enumeration
4093 type; the usual arithmetic conversions are performed to bring
4094 them to a common type, and the result is of that type. */
4095 else if ((ARITHMETIC_TYPE_P (arg2_type)
4096 || UNSCOPED_ENUM_P (arg2_type))
4097 && (ARITHMETIC_TYPE_P (arg3_type)
4098 || UNSCOPED_ENUM_P (arg3_type)))
4100 /* In this case, there is always a common type. */
4101 result_type = type_after_usual_arithmetic_conversions (arg2_type,
4103 do_warn_double_promotion (result_type, arg2_type, arg3_type,
4104 "implicit conversion from %qT to %qT to "
4105 "match other result of conditional",
4108 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
4109 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
4111 if (complain & tf_warning)
4113 "enumeral mismatch in conditional expression: %qT vs %qT",
4114 arg2_type, arg3_type);
4116 else if (extra_warnings
4117 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
4118 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
4119 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
4120 && !same_type_p (arg2_type, type_promotes_to (arg3_type)))))
4122 if (complain & tf_warning)
4124 "enumeral and non-enumeral type in conditional expression");
4127 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4128 arg3 = perform_implicit_conversion (result_type, arg3, complain);
4132 --The second and third operands have pointer type, or one has
4133 pointer type and the other is a null pointer constant; pointer
4134 conversions (_conv.ptr_) and qualification conversions
4135 (_conv.qual_) are performed to bring them to their composite
4136 pointer type (_expr.rel_). The result is of the composite
4139 --The second and third operands have pointer to member type, or
4140 one has pointer to member type and the other is a null pointer
4141 constant; pointer to member conversions (_conv.mem_) and
4142 qualification conversions (_conv.qual_) are performed to bring
4143 them to a common type, whose cv-qualification shall match the
4144 cv-qualification of either the second or the third operand.
4145 The result is of the common type. */
4146 else if ((null_ptr_cst_p (arg2)
4147 && (TYPE_PTR_P (arg3_type) || TYPE_PTR_TO_MEMBER_P (arg3_type)))
4148 || (null_ptr_cst_p (arg3)
4149 && (TYPE_PTR_P (arg2_type) || TYPE_PTR_TO_MEMBER_P (arg2_type)))
4150 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
4151 || (TYPE_PTRMEM_P (arg2_type) && TYPE_PTRMEM_P (arg3_type))
4152 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
4154 result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
4155 arg3, CPO_CONDITIONAL_EXPR,
4157 if (result_type == error_mark_node)
4158 return error_mark_node;
4159 arg2 = perform_implicit_conversion (result_type, arg2, complain);
4160 arg3 = perform_implicit_conversion (result_type, arg3, complain);
4165 if (complain & tf_error)
4166 error ("operands to ?: have different types %qT and %qT",
4167 arg2_type, arg3_type);
4168 return error_mark_node;
4172 result = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
4173 if (!cp_unevaluated_operand)
4174 /* Avoid folding within decltype (c++/42013) and noexcept. */
4175 result = fold_if_not_in_template (result);
4177 /* We can't use result_type below, as fold might have returned a
4182 /* Expand both sides into the same slot, hopefully the target of
4183 the ?: expression. We used to check for TARGET_EXPRs here,
4184 but now we sometimes wrap them in NOP_EXPRs so the test would
4186 if (CLASS_TYPE_P (TREE_TYPE (result)))
4187 result = get_target_expr (result);
4188 /* If this expression is an rvalue, but might be mistaken for an
4189 lvalue, we must add a NON_LVALUE_EXPR. */
4190 result = rvalue (result);
4196 /* OPERAND is an operand to an expression. Perform necessary steps
4197 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
4201 prep_operand (tree operand)
4205 if (CLASS_TYPE_P (TREE_TYPE (operand))
4206 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
4207 /* Make sure the template type is instantiated now. */
4208 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
4214 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
4215 OVERLOAD) to the CANDIDATES, returning an updated list of
4216 CANDIDATES. The ARGS are the arguments provided to the call;
4217 if FIRST_ARG is non-null it is the implicit object argument,
4218 otherwise the first element of ARGS is used if needed. The
4219 EXPLICIT_TARGS are explicit template arguments provided.
4220 TEMPLATE_ONLY is true if only template functions should be
4221 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
4222 add_function_candidate. */
4225 add_candidates (tree fns, tree first_arg, const VEC(tree,gc) *args,
4227 tree explicit_targs, bool template_only,
4228 tree conversion_path, tree access_path,
4230 struct z_candidate **candidates)
4233 const VEC(tree,gc) *non_static_args;
4234 bool check_list_ctor;
4235 bool check_converting;
4236 unification_kind_t strict;
4242 /* Precalculate special handling of constructors and conversion ops. */
4243 fn = OVL_CURRENT (fns);
4244 if (DECL_CONV_FN_P (fn))
4246 check_list_ctor = false;
4247 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
4248 if (flags & LOOKUP_NO_CONVERSION)
4249 /* We're doing return_type(x). */
4250 strict = DEDUCE_CONV;
4252 /* We're doing x.operator return_type(). */
4253 strict = DEDUCE_EXACT;
4254 /* [over.match.funcs] For conversion functions, the function
4255 is considered to be a member of the class of the implicit
4256 object argument for the purpose of defining the type of
4257 the implicit object parameter. */
4258 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (first_arg)));
4262 if (DECL_CONSTRUCTOR_P (fn))
4264 check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
4265 check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
4269 check_list_ctor = false;
4270 check_converting = false;
4272 strict = DEDUCE_CALL;
4273 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
4277 non_static_args = args;
4279 /* Delay creating the implicit this parameter until it is needed. */
4280 non_static_args = NULL;
4282 for (; fns; fns = OVL_NEXT (fns))
4285 const VEC(tree,gc) *fn_args;
4287 fn = OVL_CURRENT (fns);
4289 if (check_converting && DECL_NONCONVERTING_P (fn))
4291 if (check_list_ctor && !is_list_ctor (fn))
4294 /* Figure out which set of arguments to use. */
4295 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4297 /* If this function is a non-static member and we didn't get an
4298 implicit object argument, move it out of args. */
4299 if (first_arg == NULL_TREE)
4303 VEC(tree,gc) *tempvec
4304 = VEC_alloc (tree, gc, VEC_length (tree, args) - 1);
4305 for (ix = 1; VEC_iterate (tree, args, ix, arg); ++ix)
4306 VEC_quick_push (tree, tempvec, arg);
4307 non_static_args = tempvec;
4308 first_arg = build_this (VEC_index (tree, args, 0));
4311 fn_first_arg = first_arg;
4312 fn_args = non_static_args;
4316 /* Otherwise, just use the list of arguments provided. */
4317 fn_first_arg = NULL_TREE;
4321 if (TREE_CODE (fn) == TEMPLATE_DECL)
4322 add_template_candidate (candidates,
4333 else if (!template_only)
4334 add_function_candidate (candidates,
4345 /* Even unsigned enum types promote to signed int. We don't want to
4346 issue -Wsign-compare warnings for this case. Here ORIG_ARG is the
4347 original argument and ARG is the argument after any conversions
4348 have been applied. We set TREE_NO_WARNING if we have added a cast
4349 from an unsigned enum type to a signed integer type. */
4352 avoid_sign_compare_warnings (tree orig_arg, tree arg)
4354 if (orig_arg != NULL_TREE
4357 && TREE_CODE (TREE_TYPE (orig_arg)) == ENUMERAL_TYPE
4358 && TYPE_UNSIGNED (TREE_TYPE (orig_arg))
4359 && INTEGRAL_TYPE_P (TREE_TYPE (arg))
4360 && !TYPE_UNSIGNED (TREE_TYPE (arg)))
4361 TREE_NO_WARNING (arg) = 1;
4365 build_new_op (enum tree_code code, int flags, tree arg1, tree arg2, tree arg3,
4366 bool *overloaded_p, tsubst_flags_t complain)
4368 tree orig_arg1 = arg1;
4369 tree orig_arg2 = arg2;
4370 tree orig_arg3 = arg3;
4371 struct z_candidate *candidates = 0, *cand;
4372 VEC(tree,gc) *arglist;
4375 tree result = NULL_TREE;
4376 bool result_valid_p = false;
4377 enum tree_code code2 = NOP_EXPR;
4378 enum tree_code code_orig_arg1 = ERROR_MARK;
4379 enum tree_code code_orig_arg2 = ERROR_MARK;
4385 if (error_operand_p (arg1)
4386 || error_operand_p (arg2)
4387 || error_operand_p (arg3))
4388 return error_mark_node;
4390 if (code == MODIFY_EXPR)
4392 code2 = TREE_CODE (arg3);
4394 fnname = ansi_assopname (code2);
4397 fnname = ansi_opname (code);
4399 arg1 = prep_operand (arg1);
4405 case VEC_DELETE_EXPR:
4407 /* Use build_op_new_call and build_op_delete_call instead. */
4411 /* Use build_op_call instead. */
4414 case TRUTH_ORIF_EXPR:
4415 case TRUTH_ANDIF_EXPR:
4416 case TRUTH_AND_EXPR:
4418 /* These are saved for the sake of warn_logical_operator. */
4419 code_orig_arg1 = TREE_CODE (arg1);
4420 code_orig_arg2 = TREE_CODE (arg2);
4426 arg2 = prep_operand (arg2);
4427 arg3 = prep_operand (arg3);
4429 if (code == COND_EXPR)
4430 /* Use build_conditional_expr instead. */
4432 else if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
4433 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2))))
4436 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
4437 arg2 = integer_zero_node;
4439 arglist = VEC_alloc (tree, gc, 3);
4440 VEC_quick_push (tree, arglist, arg1);
4441 if (arg2 != NULL_TREE)
4442 VEC_quick_push (tree, arglist, arg2);
4443 if (arg3 != NULL_TREE)
4444 VEC_quick_push (tree, arglist, arg3);
4446 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4447 p = conversion_obstack_alloc (0);
4449 /* Add namespace-scope operators to the list of functions to
4451 add_candidates (lookup_function_nonclass (fnname, arglist, /*block_p=*/true),
4452 NULL_TREE, arglist, NULL_TREE,
4453 NULL_TREE, false, NULL_TREE, NULL_TREE,
4454 flags, &candidates);
4455 /* Add class-member operators to the candidate set. */
4456 if (CLASS_TYPE_P (TREE_TYPE (arg1)))
4460 fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
4461 if (fns == error_mark_node)
4463 result = error_mark_node;
4464 goto user_defined_result_ready;
4467 add_candidates (BASELINK_FUNCTIONS (fns),
4468 NULL_TREE, arglist, NULL_TREE,
4470 BASELINK_BINFO (fns),
4471 BASELINK_ACCESS_BINFO (fns),
4472 flags, &candidates);
4477 args[2] = NULL_TREE;
4479 add_builtin_candidates (&candidates, code, code2, fnname, args, flags);
4485 /* For these, the built-in candidates set is empty
4486 [over.match.oper]/3. We don't want non-strict matches
4487 because exact matches are always possible with built-in
4488 operators. The built-in candidate set for COMPONENT_REF
4489 would be empty too, but since there are no such built-in
4490 operators, we accept non-strict matches for them. */
4495 strict_p = pedantic;
4499 candidates = splice_viable (candidates, strict_p, &any_viable_p);
4504 case POSTINCREMENT_EXPR:
4505 case POSTDECREMENT_EXPR:
4506 /* Don't try anything fancy if we're not allowed to produce
4508 if (!(complain & tf_error))
4509 return error_mark_node;
4511 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
4512 distinguish between prefix and postfix ++ and
4513 operator++() was used for both, so we allow this with
4515 if (flags & LOOKUP_COMPLAIN)
4517 const char *msg = (flag_permissive)
4518 ? G_("no %<%D(int)%> declared for postfix %qs,"
4519 " trying prefix operator instead")
4520 : G_("no %<%D(int)%> declared for postfix %qs");
4521 permerror (input_location, msg, fnname,
4522 operator_name_info[code].name);
4525 if (!flag_permissive)
4526 return error_mark_node;
4528 if (code == POSTINCREMENT_EXPR)
4529 code = PREINCREMENT_EXPR;
4531 code = PREDECREMENT_EXPR;
4532 result = build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE,
4533 overloaded_p, complain);
4536 /* The caller will deal with these. */
4541 result_valid_p = true;
4545 if ((flags & LOOKUP_COMPLAIN) && (complain & tf_error))
4547 /* If one of the arguments of the operator represents
4548 an invalid use of member function pointer, try to report
4549 a meaningful error ... */
4550 if (invalid_nonstatic_memfn_p (arg1, tf_error)
4551 || invalid_nonstatic_memfn_p (arg2, tf_error)
4552 || invalid_nonstatic_memfn_p (arg3, tf_error))
4553 /* We displayed the error message. */;
4556 /* ... Otherwise, report the more generic
4557 "no matching operator found" error */
4558 op_error (code, code2, arg1, arg2, arg3, FALSE);
4559 print_z_candidates (candidates);
4562 result = error_mark_node;
4568 cand = tourney (candidates);
4571 if ((flags & LOOKUP_COMPLAIN) && (complain & tf_error))
4573 op_error (code, code2, arg1, arg2, arg3, TRUE);
4574 print_z_candidates (candidates);
4576 result = error_mark_node;
4578 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
4581 *overloaded_p = true;
4583 if (resolve_args (arglist) == NULL)
4584 result = error_mark_node;
4586 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4590 /* Give any warnings we noticed during overload resolution. */
4591 if (cand->warnings && (complain & tf_warning))
4593 struct candidate_warning *w;
4594 for (w = cand->warnings; w; w = w->next)
4595 joust (cand, w->loser, 1);
4598 /* Check for comparison of different enum types. */
4607 if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
4608 && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
4609 && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
4610 != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
4611 && (complain & tf_warning))
4613 warning (OPT_Wenum_compare,
4614 "comparison between %q#T and %q#T",
4615 TREE_TYPE (arg1), TREE_TYPE (arg2));
4622 /* We need to strip any leading REF_BIND so that bitfields
4623 don't cause errors. This should not remove any important
4624 conversions, because builtins don't apply to class
4625 objects directly. */
4626 conv = cand->convs[0];
4627 if (conv->kind == ck_ref_bind)
4628 conv = conv->u.next;
4629 arg1 = convert_like (conv, arg1, complain);
4633 /* We need to call warn_logical_operator before
4634 converting arg2 to a boolean_type. */
4635 if (complain & tf_warning)
4636 warn_logical_operator (input_location, code, boolean_type_node,
4637 code_orig_arg1, arg1,
4638 code_orig_arg2, arg2);
4640 conv = cand->convs[1];
4641 if (conv->kind == ck_ref_bind)
4642 conv = conv->u.next;
4643 arg2 = convert_like (conv, arg2, complain);
4647 conv = cand->convs[2];
4648 if (conv->kind == ck_ref_bind)
4649 conv = conv->u.next;
4650 arg3 = convert_like (conv, arg3, complain);
4656 user_defined_result_ready:
4658 /* Free all the conversions we allocated. */
4659 obstack_free (&conversion_obstack, p);
4661 if (result || result_valid_p)
4665 avoid_sign_compare_warnings (orig_arg1, arg1);
4666 avoid_sign_compare_warnings (orig_arg2, arg2);
4667 avoid_sign_compare_warnings (orig_arg3, arg3);
4672 return cp_build_modify_expr (arg1, code2, arg2, complain);
4675 return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
4677 case TRUTH_ANDIF_EXPR:
4678 case TRUTH_ORIF_EXPR:
4679 case TRUTH_AND_EXPR:
4681 warn_logical_operator (input_location, code, boolean_type_node,
4682 code_orig_arg1, arg1, code_orig_arg2, arg2);
4687 case TRUNC_DIV_EXPR:
4698 case TRUNC_MOD_EXPR:
4702 return cp_build_binary_op (input_location, code, arg1, arg2, complain);
4704 case UNARY_PLUS_EXPR:
4707 case TRUTH_NOT_EXPR:
4708 case PREINCREMENT_EXPR:
4709 case POSTINCREMENT_EXPR:
4710 case PREDECREMENT_EXPR:
4711 case POSTDECREMENT_EXPR:
4714 return cp_build_unary_op (code, arg1, candidates != 0, complain);
4717 return cp_build_array_ref (input_location, arg1, arg2, complain);
4720 return build_m_component_ref (cp_build_indirect_ref (arg1, RO_NULL,
4724 /* The caller will deal with these. */
4736 /* Returns true iff T, an element of an OVERLOAD chain, is a usual
4737 deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
4740 non_placement_deallocation_fn_p (tree t)
4742 /* A template instance is never a usual deallocation function,
4743 regardless of its signature. */
4744 if (TREE_CODE (t) == TEMPLATE_DECL
4745 || primary_template_instantiation_p (t))
4748 /* If a class T has a member deallocation function named operator delete
4749 with exactly one parameter, then that function is a usual
4750 (non-placement) deallocation function. If class T does not declare
4751 such an operator delete but does declare a member deallocation
4752 function named operator delete with exactly two parameters, the second
4753 of which has type std::size_t (18.2), then this function is a usual
4754 deallocation function. */
4755 t = FUNCTION_ARG_CHAIN (t);
4756 if (t == void_list_node
4757 || (t && same_type_p (TREE_VALUE (t), size_type_node)
4758 && TREE_CHAIN (t) == void_list_node))
4763 /* Build a call to operator delete. This has to be handled very specially,
4764 because the restrictions on what signatures match are different from all
4765 other call instances. For a normal delete, only a delete taking (void *)
4766 or (void *, size_t) is accepted. For a placement delete, only an exact
4767 match with the placement new is accepted.
4769 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
4770 ADDR is the pointer to be deleted.
4771 SIZE is the size of the memory block to be deleted.
4772 GLOBAL_P is true if the delete-expression should not consider
4773 class-specific delete operators.
4774 PLACEMENT is the corresponding placement new call, or NULL_TREE.
4776 If this call to "operator delete" is being generated as part to
4777 deallocate memory allocated via a new-expression (as per [expr.new]
4778 which requires that if the initialization throws an exception then
4779 we call a deallocation function), then ALLOC_FN is the allocation
4783 build_op_delete_call (enum tree_code code, tree addr, tree size,
4784 bool global_p, tree placement,
4787 tree fn = NULL_TREE;
4788 tree fns, fnname, type, t;
4790 if (addr == error_mark_node)
4791 return error_mark_node;
4793 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
4795 fnname = ansi_opname (code);
4797 if (CLASS_TYPE_P (type)
4798 && COMPLETE_TYPE_P (complete_type (type))
4802 If the result of the lookup is ambiguous or inaccessible, or if
4803 the lookup selects a placement deallocation function, the
4804 program is ill-formed.
4806 Therefore, we ask lookup_fnfields to complain about ambiguity. */
4808 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
4809 if (fns == error_mark_node)
4810 return error_mark_node;
4815 if (fns == NULL_TREE)
4816 fns = lookup_name_nonclass (fnname);
4818 /* Strip const and volatile from addr. */
4819 addr = cp_convert (ptr_type_node, addr);
4823 /* "A declaration of a placement deallocation function matches the
4824 declaration of a placement allocation function if it has the same
4825 number of parameters and, after parameter transformations (8.3.5),
4826 all parameter types except the first are identical."
4828 So we build up the function type we want and ask instantiate_type
4829 to get it for us. */
4830 t = FUNCTION_ARG_CHAIN (alloc_fn);
4831 t = tree_cons (NULL_TREE, ptr_type_node, t);
4832 t = build_function_type (void_type_node, t);
4834 fn = instantiate_type (t, fns, tf_none);
4835 if (fn == error_mark_node)
4838 if (BASELINK_P (fn))
4839 fn = BASELINK_FUNCTIONS (fn);
4841 /* "If the lookup finds the two-parameter form of a usual deallocation
4842 function (3.7.4.2) and that function, considered as a placement
4843 deallocation function, would have been selected as a match for the
4844 allocation function, the program is ill-formed." */
4845 if (non_placement_deallocation_fn_p (fn))
4847 /* But if the class has an operator delete (void *), then that is
4848 the usual deallocation function, so we shouldn't complain
4849 about using the operator delete (void *, size_t). */
4850 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
4851 t; t = OVL_NEXT (t))
4853 tree elt = OVL_CURRENT (t);
4854 if (non_placement_deallocation_fn_p (elt)
4855 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
4858 permerror (0, "non-placement deallocation function %q+D", fn);
4859 permerror (input_location, "selected for placement delete");
4864 /* "Any non-placement deallocation function matches a non-placement
4865 allocation function. If the lookup finds a single matching
4866 deallocation function, that function will be called; otherwise, no
4867 deallocation function will be called." */
4868 for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
4869 t; t = OVL_NEXT (t))
4871 tree elt = OVL_CURRENT (t);
4872 if (non_placement_deallocation_fn_p (elt))
4875 /* "If a class T has a member deallocation function named
4876 operator delete with exactly one parameter, then that
4877 function is a usual (non-placement) deallocation
4878 function. If class T does not declare such an operator
4879 delete but does declare a member deallocation function named
4880 operator delete with exactly two parameters, the second of
4881 which has type std::size_t (18.2), then this function is a
4882 usual deallocation function."
4884 So (void*) beats (void*, size_t). */
4885 if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
4890 /* If we have a matching function, call it. */
4893 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
4895 /* If the FN is a member function, make sure that it is
4897 if (BASELINK_P (fns))
4898 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn);
4900 /* Core issue 901: It's ok to new a type with deleted delete. */
4901 if (DECL_DELETED_FN (fn) && alloc_fn)
4906 /* The placement args might not be suitable for overload
4907 resolution at this point, so build the call directly. */
4908 int nargs = call_expr_nargs (placement);
4909 tree *argarray = XALLOCAVEC (tree, nargs);
4912 for (i = 1; i < nargs; i++)
4913 argarray[i] = CALL_EXPR_ARG (placement, i);
4915 return build_cxx_call (fn, nargs, argarray);
4920 VEC(tree,gc) *args = VEC_alloc (tree, gc, 2);
4921 VEC_quick_push (tree, args, addr);
4922 if (FUNCTION_ARG_CHAIN (fn) != void_list_node)
4923 VEC_quick_push (tree, args, size);
4924 ret = cp_build_function_call_vec (fn, &args, tf_warning_or_error);
4925 VEC_free (tree, gc, args);
4932 If no unambiguous matching deallocation function can be found,
4933 propagating the exception does not cause the object's memory to
4938 warning (0, "no corresponding deallocation function for %qD",
4943 error ("no suitable %<operator %s%> for %qT",
4944 operator_name_info[(int)code].name, type);
4945 return error_mark_node;
4948 /* If the current scope isn't allowed to access DECL along
4949 BASETYPE_PATH, give an error. The most derived class in
4950 BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
4951 the declaration to use in the error diagnostic. */
4954 enforce_access (tree basetype_path, tree decl, tree diag_decl)
4956 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
4958 if (!accessible_p (basetype_path, decl, true))
4960 if (TREE_PRIVATE (decl))
4961 error ("%q+#D is private", diag_decl);
4962 else if (TREE_PROTECTED (decl))
4963 error ("%q+#D is protected", diag_decl);
4965 error ("%q+#D is inaccessible", diag_decl);
4966 error ("within this context");
4973 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
4974 bitwise or of LOOKUP_* values. If any errors are warnings are
4975 generated, set *DIAGNOSTIC_FN to "error" or "warning",
4976 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
4980 build_temp (tree expr, tree type, int flags,
4981 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
4986 savew = warningcount, savee = errorcount;
4987 args = make_tree_vector_single (expr);
4988 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4989 &args, type, flags, complain);
4990 release_tree_vector (args);
4991 if (warningcount > savew)
4992 *diagnostic_kind = DK_WARNING;
4993 else if (errorcount > savee)
4994 *diagnostic_kind = DK_ERROR;
4996 *diagnostic_kind = DK_UNSPECIFIED;
5000 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
5001 EXPR is implicitly converted to type TOTYPE.
5002 FN and ARGNUM are used for diagnostics. */
5005 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
5007 tree t = non_reference (totype);
5009 /* Issue warnings about peculiar, but valid, uses of NULL. */
5010 if (expr == null_node && TREE_CODE (t) != BOOLEAN_TYPE && ARITHMETIC_TYPE_P (t))
5013 warning_at (input_location, OPT_Wconversion_null,
5014 "passing NULL to non-pointer argument %P of %qD",
5017 warning_at (input_location, OPT_Wconversion_null,
5018 "converting to non-pointer type %qT from NULL", t);
5021 /* Issue warnings if "false" is converted to a NULL pointer */
5022 else if (expr == boolean_false_node && fn && POINTER_TYPE_P (t))
5023 warning_at (input_location, OPT_Wconversion_null,
5024 "converting %<false%> to pointer type for argument %P of %qD",
5028 /* Perform the conversions in CONVS on the expression EXPR. FN and
5029 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
5030 indicates the `this' argument of a method. INNER is nonzero when
5031 being called to continue a conversion chain. It is negative when a
5032 reference binding will be applied, positive otherwise. If
5033 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
5034 conversions will be emitted if appropriate. If C_CAST_P is true,
5035 this conversion is coming from a C-style cast; in that case,
5036 conversions to inaccessible bases are permitted. */
5039 convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
5040 int inner, bool issue_conversion_warnings,
5041 bool c_cast_p, tsubst_flags_t complain)
5043 tree totype = convs->type;
5044 diagnostic_t diag_kind;
5048 && convs->kind != ck_user
5049 && convs->kind != ck_list
5050 && convs->kind != ck_ambig
5051 && convs->kind != ck_ref_bind
5052 && convs->kind != ck_rvalue
5053 && convs->kind != ck_base)
5055 conversion *t = convs;
5057 /* Give a helpful error if this is bad because of excess braces. */
5058 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5059 && SCALAR_TYPE_P (totype)
5060 && CONSTRUCTOR_NELTS (expr) > 0
5061 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
5062 permerror (input_location, "too many braces around initializer for %qT", totype);
5064 for (; t; t = convs->u.next)
5066 if (t->kind == ck_user || !t->bad_p)
5068 expr = convert_like_real (t, expr, fn, argnum, 1,
5069 /*issue_conversion_warnings=*/false,
5074 else if (t->kind == ck_ambig)
5075 return convert_like_real (t, expr, fn, argnum, 1,
5076 /*issue_conversion_warnings=*/false,
5079 else if (t->kind == ck_identity)
5082 if (complain & tf_error)
5084 permerror (input_location, "invalid conversion from %qT to %qT", TREE_TYPE (expr), totype);
5086 permerror (DECL_SOURCE_LOCATION (fn),
5087 " initializing argument %P of %qD", argnum, fn);
5090 return error_mark_node;
5092 return cp_convert (totype, expr);
5095 if (issue_conversion_warnings && (complain & tf_warning))
5096 conversion_null_warnings (totype, expr, fn, argnum);
5098 switch (convs->kind)
5102 struct z_candidate *cand = convs->cand;
5103 tree convfn = cand->fn;
5106 expr = mark_rvalue_use (expr);
5108 /* When converting from an init list we consider explicit
5109 constructors, but actually trying to call one is an error. */
5110 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
5111 /* Unless we're calling it for value-initialization from an
5112 empty list, since that is handled separately in 8.5.4. */
5113 && cand->num_convs > 0)
5115 if (complain & tf_error)
5116 error ("converting to %qT from initializer list would use "
5117 "explicit constructor %qD", totype, convfn);
5119 return error_mark_node;
5122 /* Set user_conv_p on the argument conversions, so rvalue/base
5123 handling knows not to allow any more UDCs. */
5124 for (i = 0; i < cand->num_convs; ++i)
5125 cand->convs[i]->user_conv_p = true;
5127 expr = build_over_call (cand, LOOKUP_NORMAL, complain);
5129 /* If this is a constructor or a function returning an aggr type,
5130 we need to build up a TARGET_EXPR. */
5131 if (DECL_CONSTRUCTOR_P (convfn))
5133 expr = build_cplus_new (totype, expr);
5135 /* Remember that this was list-initialization. */
5136 if (convs->check_narrowing)
5137 TARGET_EXPR_LIST_INIT_P (expr) = true;
5143 expr = mark_rvalue_use (expr);
5144 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
5146 int nelts = CONSTRUCTOR_NELTS (expr);
5148 expr = build_value_init (totype, tf_warning_or_error);
5149 else if (nelts == 1)
5150 expr = CONSTRUCTOR_ELT (expr, 0)->value;
5155 if (type_unknown_p (expr))
5156 expr = instantiate_type (totype, expr, complain);
5157 /* Convert a constant to its underlying value, unless we are
5158 about to bind it to a reference, in which case we need to
5159 leave it as an lvalue. */
5162 expr = decl_constant_value (expr);
5163 if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
5164 /* If __null has been converted to an integer type, we do not
5165 want to warn about uses of EXPR as an integer, rather than
5167 expr = build_int_cst (totype, 0);
5171 if (complain & tf_error)
5173 /* Call build_user_type_conversion again for the error. */
5174 build_user_type_conversion (totype, convs->u.expr, LOOKUP_NORMAL);
5176 error (" initializing argument %P of %q+D", argnum, fn);
5178 return error_mark_node;
5182 /* Conversion to std::initializer_list<T>. */
5183 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
5184 tree new_ctor = build_constructor (init_list_type_node, NULL);
5185 unsigned len = CONSTRUCTOR_NELTS (expr);
5187 VEC(tree,gc) *parms;
5190 /* Convert all the elements. */
5191 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
5193 tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
5194 1, false, false, complain);
5195 if (sub == error_mark_node)
5197 if (!BRACE_ENCLOSED_INITIALIZER_P (val))
5198 check_narrowing (TREE_TYPE (sub), val);
5199 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
5201 /* Build up the array. */
5202 elttype = cp_build_qualified_type
5203 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
5204 array = build_array_of_n_type (elttype, len);
5205 array = finish_compound_literal (array, new_ctor);
5207 parms = make_tree_vector ();
5208 VEC_safe_push (tree, gc, parms, decay_conversion (array));
5209 VEC_safe_push (tree, gc, parms, size_int (len));
5210 /* Call the private constructor. */
5211 push_deferring_access_checks (dk_no_check);
5212 new_ctor = build_special_member_call
5213 (NULL_TREE, complete_ctor_identifier, &parms, totype, 0, complain);
5214 release_tree_vector (parms);
5215 pop_deferring_access_checks ();
5216 return build_cplus_new (totype, new_ctor);
5220 return get_target_expr (digest_init (totype, expr));
5226 expr = convert_like_real (convs->u.next, expr, fn, argnum,
5227 convs->kind == ck_ref_bind ? -1 : 1,
5228 convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
5231 if (expr == error_mark_node)
5232 return error_mark_node;
5234 switch (convs->kind)
5237 expr = decay_conversion (expr);
5238 if (! MAYBE_CLASS_TYPE_P (totype))
5240 /* Else fall through. */
5242 if (convs->kind == ck_base && !convs->need_temporary_p)
5244 /* We are going to bind a reference directly to a base-class
5245 subobject of EXPR. */
5246 /* Build an expression for `*((base*) &expr)'. */
5247 expr = cp_build_addr_expr (expr, complain);
5248 expr = convert_to_base (expr, build_pointer_type (totype),
5249 !c_cast_p, /*nonnull=*/true, complain);
5250 expr = cp_build_indirect_ref (expr, RO_IMPLICIT_CONVERSION, complain);
5254 /* Copy-initialization where the cv-unqualified version of the source
5255 type is the same class as, or a derived class of, the class of the
5256 destination [is treated as direct-initialization]. [dcl.init] */
5257 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
5258 if (convs->user_conv_p)
5259 /* This conversion is being done in the context of a user-defined
5260 conversion (i.e. the second step of copy-initialization), so
5261 don't allow any more. */
5262 flags |= LOOKUP_NO_CONVERSION;
5263 expr = build_temp (expr, totype, flags, &diag_kind, complain);
5264 if (diag_kind && fn)
5266 if ((complain & tf_error))
5267 emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), 0,
5268 " initializing argument %P of %qD", argnum, fn);
5269 else if (diag_kind == DK_ERROR)
5270 return error_mark_node;
5272 return build_cplus_new (totype, expr);
5276 tree ref_type = totype;
5278 if (convs->bad_p && TYPE_REF_IS_RVALUE (ref_type)
5279 && real_lvalue_p (expr))
5281 if (complain & tf_error)
5283 error ("cannot bind %qT lvalue to %qT",
5284 TREE_TYPE (expr), totype);
5286 error (" initializing argument %P of %q+D", argnum, fn);
5288 return error_mark_node;
5291 /* If necessary, create a temporary.
5293 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
5294 that need temporaries, even when their types are reference
5295 compatible with the type of reference being bound, so the
5296 upcoming call to cp_build_addr_expr doesn't fail. */
5297 if (convs->need_temporary_p
5298 || TREE_CODE (expr) == CONSTRUCTOR
5299 || TREE_CODE (expr) == VA_ARG_EXPR)
5301 /* Otherwise, a temporary of type "cv1 T1" is created and
5302 initialized from the initializer expression using the rules
5303 for a non-reference copy-initialization (8.5). */
5305 tree type = TREE_TYPE (ref_type);
5306 cp_lvalue_kind lvalue = real_lvalue_p (expr);
5308 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5309 (type, convs->u.next->type));
5310 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
5311 && !TYPE_REF_IS_RVALUE (ref_type))
5313 if (complain & tf_error)
5315 /* If the reference is volatile or non-const, we
5316 cannot create a temporary. */
5317 if (lvalue & clk_bitfield)
5318 error ("cannot bind bitfield %qE to %qT",
5320 else if (lvalue & clk_packed)
5321 error ("cannot bind packed field %qE to %qT",
5324 error ("cannot bind rvalue %qE to %qT", expr, ref_type);
5326 return error_mark_node;
5328 /* If the source is a packed field, and we must use a copy
5329 constructor, then building the target expr will require
5330 binding the field to the reference parameter to the
5331 copy constructor, and we'll end up with an infinite
5332 loop. If we can use a bitwise copy, then we'll be
5334 if ((lvalue & clk_packed)
5335 && CLASS_TYPE_P (type)
5336 && type_has_nontrivial_copy_init (type))
5338 if (complain & tf_error)
5339 error ("cannot bind packed field %qE to %qT",
5341 return error_mark_node;
5343 if (lvalue & clk_bitfield)
5345 expr = convert_bitfield_to_declared_type (expr);
5346 expr = fold_convert (type, expr);
5348 expr = build_target_expr_with_type (expr, type);
5351 /* Take the address of the thing to which we will bind the
5353 expr = cp_build_addr_expr (expr, complain);
5354 if (expr == error_mark_node)
5355 return error_mark_node;
5357 /* Convert it to a pointer to the type referred to by the
5358 reference. This will adjust the pointer if a derived to
5359 base conversion is being performed. */
5360 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
5362 /* Convert the pointer to the desired reference type. */
5363 return build_nop (ref_type, expr);
5367 return decay_conversion (expr);
5370 /* Warn about deprecated conversion if appropriate. */
5371 string_conv_p (totype, expr, 1);
5376 expr = convert_to_base (expr, totype, !c_cast_p,
5377 /*nonnull=*/false, complain);
5378 return build_nop (totype, expr);
5381 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
5382 c_cast_p, complain);
5388 if (convs->check_narrowing)
5389 check_narrowing (totype, expr);
5391 if (issue_conversion_warnings && (complain & tf_warning))
5392 expr = convert_and_check (totype, expr);
5394 expr = convert (totype, expr);
5399 /* ARG is being passed to a varargs function. Perform any conversions
5400 required. Return the converted value. */
5403 convert_arg_to_ellipsis (tree arg)
5409 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
5410 standard conversions are performed. */
5411 arg = decay_conversion (arg);
5412 arg_type = TREE_TYPE (arg);
5415 If the argument has integral or enumeration type that is subject
5416 to the integral promotions (_conv.prom_), or a floating point
5417 type that is subject to the floating point promotion
5418 (_conv.fpprom_), the value of the argument is converted to the
5419 promoted type before the call. */
5420 if (TREE_CODE (arg_type) == REAL_TYPE
5421 && (TYPE_PRECISION (arg_type)
5422 < TYPE_PRECISION (double_type_node))
5423 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
5425 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5426 warning (OPT_Wdouble_promotion,
5427 "implicit conversion from %qT to %qT when passing "
5428 "argument to function",
5429 arg_type, double_type_node);
5430 arg = convert_to_real (double_type_node, arg);
5432 else if (NULLPTR_TYPE_P (arg_type))
5433 arg = null_pointer_node;
5434 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
5435 arg = perform_integral_promotions (arg);
5437 arg = require_complete_type (arg);
5438 arg_type = TREE_TYPE (arg);
5440 if (arg != error_mark_node
5441 && (type_has_nontrivial_copy_init (arg_type)
5442 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
5444 /* [expr.call] 5.2.2/7:
5445 Passing a potentially-evaluated argument of class type (Clause 9)
5446 with a non-trivial copy constructor or a non-trivial destructor
5447 with no corresponding parameter is conditionally-supported, with
5448 implementation-defined semantics.
5450 We used to just warn here and do a bitwise copy, but now
5451 cp_expr_size will abort if we try to do that.
5453 If the call appears in the context of a sizeof expression,
5454 it is not potentially-evaluated. */
5455 if (cp_unevaluated_operand == 0)
5456 error ("cannot pass objects of non-trivially-copyable "
5457 "type %q#T through %<...%>", arg_type);
5463 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
5466 build_x_va_arg (tree expr, tree type)
5468 if (processing_template_decl)
5469 return build_min (VA_ARG_EXPR, type, expr);
5471 type = complete_type_or_else (type, NULL_TREE);
5473 if (expr == error_mark_node || !type)
5474 return error_mark_node;
5476 expr = mark_lvalue_use (expr);
5478 if (type_has_nontrivial_copy_init (type)
5479 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5480 || TREE_CODE (type) == REFERENCE_TYPE)
5482 /* Remove reference types so we don't ICE later on. */
5483 tree type1 = non_reference (type);
5484 /* conditionally-supported behavior [expr.call] 5.2.2/7. */
5485 error ("cannot receive objects of non-trivially-copyable type %q#T "
5486 "through %<...%>; ", type);
5487 expr = convert (build_pointer_type (type1), null_node);
5488 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
5492 return build_va_arg (input_location, expr, type);
5495 /* TYPE has been given to va_arg. Apply the default conversions which
5496 would have happened when passed via ellipsis. Return the promoted
5497 type, or the passed type if there is no change. */
5500 cxx_type_promotes_to (tree type)
5504 /* Perform the array-to-pointer and function-to-pointer
5506 type = type_decays_to (type);
5508 promote = type_promotes_to (type);
5509 if (same_type_p (type, promote))
5515 /* ARG is a default argument expression being passed to a parameter of
5516 the indicated TYPE, which is a parameter to FN. Do any required
5517 conversions. Return the converted value. */
5519 static GTY(()) VEC(tree,gc) *default_arg_context;
5522 convert_default_arg (tree type, tree arg, tree fn, int parmnum)
5527 /* If the ARG is an unparsed default argument expression, the
5528 conversion cannot be performed. */
5529 if (TREE_CODE (arg) == DEFAULT_ARG)
5531 error ("the default argument for parameter %d of %qD has "
5532 "not yet been parsed",
5534 return error_mark_node;
5537 /* Detect recursion. */
5538 FOR_EACH_VEC_ELT (tree, default_arg_context, i, t)
5541 error ("recursive evaluation of default argument for %q#D", fn);
5542 return error_mark_node;
5544 VEC_safe_push (tree, gc, default_arg_context, fn);
5546 if (fn && DECL_TEMPLATE_INFO (fn))
5547 arg = tsubst_default_argument (fn, type, arg);
5553 The names in the expression are bound, and the semantic
5554 constraints are checked, at the point where the default
5555 expressions appears.
5557 we must not perform access checks here. */
5558 push_deferring_access_checks (dk_no_check);
5559 arg = break_out_target_exprs (arg);
5560 if (TREE_CODE (arg) == CONSTRUCTOR)
5562 arg = digest_init (type, arg);
5563 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
5564 ICR_DEFAULT_ARGUMENT, fn, parmnum,
5565 tf_warning_or_error);
5569 /* We must make a copy of ARG, in case subsequent processing
5570 alters any part of it. For example, during gimplification a
5571 cast of the form (T) &X::f (where "f" is a member function)
5572 will lead to replacing the PTRMEM_CST for &X::f with a
5573 VAR_DECL. We can avoid the copy for constants, since they
5574 are never modified in place. */
5575 if (!CONSTANT_CLASS_P (arg))
5576 arg = unshare_expr (arg);
5577 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
5578 ICR_DEFAULT_ARGUMENT, fn, parmnum,
5579 tf_warning_or_error);
5580 arg = convert_for_arg_passing (type, arg);
5582 pop_deferring_access_checks();
5584 VEC_pop (tree, default_arg_context);
5589 /* Returns the type which will really be used for passing an argument of
5593 type_passed_as (tree type)
5595 /* Pass classes with copy ctors by invisible reference. */
5596 if (TREE_ADDRESSABLE (type))
5598 type = build_reference_type (type);
5599 /* There are no other pointers to this temporary. */
5600 type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
5602 else if (targetm.calls.promote_prototypes (type)
5603 && INTEGRAL_TYPE_P (type)
5604 && COMPLETE_TYPE_P (type)
5605 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
5606 TYPE_SIZE (integer_type_node)))
5607 type = integer_type_node;
5612 /* Actually perform the appropriate conversion. */
5615 convert_for_arg_passing (tree type, tree val)
5619 /* If VAL is a bitfield, then -- since it has already been converted
5620 to TYPE -- it cannot have a precision greater than TYPE.
5622 If it has a smaller precision, we must widen it here. For
5623 example, passing "int f:3;" to a function expecting an "int" will
5624 not result in any conversion before this point.
5626 If the precision is the same we must not risk widening. For
5627 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
5628 often have type "int", even though the C++ type for the field is
5629 "long long". If the value is being passed to a function
5630 expecting an "int", then no conversions will be required. But,
5631 if we call convert_bitfield_to_declared_type, the bitfield will
5632 be converted to "long long". */
5633 bitfield_type = is_bitfield_expr_with_lowered_type (val);
5635 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
5636 val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
5638 if (val == error_mark_node)
5640 /* Pass classes with copy ctors by invisible reference. */
5641 else if (TREE_ADDRESSABLE (type))
5642 val = build1 (ADDR_EXPR, build_reference_type (type), val);
5643 else if (targetm.calls.promote_prototypes (type)
5644 && INTEGRAL_TYPE_P (type)
5645 && COMPLETE_TYPE_P (type)
5646 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
5647 TYPE_SIZE (integer_type_node)))
5648 val = perform_integral_promotions (val);
5649 if (warn_missing_format_attribute)
5651 tree rhstype = TREE_TYPE (val);
5652 const enum tree_code coder = TREE_CODE (rhstype);
5653 const enum tree_code codel = TREE_CODE (type);
5654 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5656 && check_missing_format_attribute (type, rhstype))
5657 warning (OPT_Wmissing_format_attribute,
5658 "argument of function call might be a candidate for a format attribute");
5663 /* Returns true iff FN is a function with magic varargs, i.e. ones for
5664 which no conversions at all should be done. This is true for some
5665 builtins which don't act like normal functions. */
5668 magic_varargs_p (tree fn)
5670 if (DECL_BUILT_IN (fn))
5671 switch (DECL_FUNCTION_CODE (fn))
5673 case BUILT_IN_CLASSIFY_TYPE:
5674 case BUILT_IN_CONSTANT_P:
5675 case BUILT_IN_NEXT_ARG:
5676 case BUILT_IN_VA_START:
5680 return lookup_attribute ("type generic",
5681 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
5687 /* Subroutine of the various build_*_call functions. Overload resolution
5688 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
5689 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
5690 bitmask of various LOOKUP_* flags which apply to the call itself. */
5693 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
5696 const VEC(tree,gc) *args = cand->args;
5697 tree first_arg = cand->first_arg;
5698 conversion **convs = cand->convs;
5700 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
5705 unsigned int arg_index = 0;
5709 bool already_used = false;
5711 /* In a template, there is no need to perform all of the work that
5712 is normally done. We are only interested in the type of the call
5713 expression, i.e., the return type of the function. Any semantic
5714 errors will be deferred until the template is instantiated. */
5715 if (processing_template_decl)
5719 const tree *argarray;
5722 return_type = TREE_TYPE (TREE_TYPE (fn));
5723 nargs = VEC_length (tree, args);
5724 if (first_arg == NULL_TREE)
5725 argarray = VEC_address (tree, CONST_CAST (VEC(tree,gc) *, args));
5733 alcarray = XALLOCAVEC (tree, nargs);
5734 alcarray[0] = first_arg;
5735 FOR_EACH_VEC_ELT (tree, args, ix, arg)
5736 alcarray[ix + 1] = arg;
5737 argarray = alcarray;
5739 expr = build_call_array_loc (input_location,
5740 return_type, build_addr_func (fn), nargs,
5742 if (TREE_THIS_VOLATILE (fn) && cfun)
5743 current_function_returns_abnormally = 1;
5744 if (!VOID_TYPE_P (return_type))
5745 require_complete_type_sfinae (return_type, complain);
5746 return convert_from_reference (expr);
5749 /* Give any warnings we noticed during overload resolution. */
5750 if (cand->warnings && (complain & tf_warning))
5752 struct candidate_warning *w;
5753 for (w = cand->warnings; w; w = w->next)
5754 joust (cand, w->loser, 1);
5757 /* Make =delete work with SFINAE. */
5758 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
5759 return error_mark_node;
5761 if (DECL_FUNCTION_MEMBER_P (fn))
5764 /* If FN is a template function, two cases must be considered.
5769 template <class T> void f();
5771 template <class T> struct B {
5775 struct C : A, B<int> {
5777 using B<int>::g; // #2
5780 In case #1 where `A::f' is a member template, DECL_ACCESS is
5781 recorded in the primary template but not in its specialization.
5782 We check access of FN using its primary template.
5784 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
5785 because it is a member of class template B, DECL_ACCESS is
5786 recorded in the specialization `B<int>::g'. We cannot use its
5787 primary template because `B<T>::g' and `B<int>::g' may have
5788 different access. */
5789 if (DECL_TEMPLATE_INFO (fn)
5790 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
5791 access_fn = DECL_TI_TEMPLATE (fn);
5794 if (flags & LOOKUP_SPECULATIVE)
5796 /* If we're checking for implicit delete, we don't want access
5798 if (!accessible_p (cand->access_path, access_fn, true))
5800 /* Unless we're under maybe_explain_implicit_delete. */
5801 if (flags & LOOKUP_COMPLAIN)
5802 enforce_access (cand->access_path, access_fn, fn);
5803 return error_mark_node;
5807 perform_or_defer_access_check (cand->access_path, access_fn, fn);
5810 /* If we're checking for implicit delete, don't bother with argument
5812 if (flags & LOOKUP_SPECULATIVE)
5814 if (DECL_DELETED_FN (fn))
5816 if (flags & LOOKUP_COMPLAIN)
5818 return error_mark_node;
5820 if (cand->viable == 1)
5822 else if (!(flags & LOOKUP_COMPLAIN))
5823 /* Reject bad conversions now. */
5824 return error_mark_node;
5825 /* else continue to get conversion error. */
5828 /* Find maximum size of vector to hold converted arguments. */
5829 parmlen = list_length (parm);
5830 nargs = VEC_length (tree, args) + (first_arg != NULL_TREE ? 1 : 0);
5831 if (parmlen > nargs)
5833 argarray = XALLOCAVEC (tree, nargs);
5835 /* The implicit parameters to a constructor are not considered by overload
5836 resolution, and must be of the proper type. */
5837 if (DECL_CONSTRUCTOR_P (fn))
5839 if (first_arg != NULL_TREE)
5841 argarray[j++] = first_arg;
5842 first_arg = NULL_TREE;
5846 argarray[j++] = VEC_index (tree, args, arg_index);
5849 parm = TREE_CHAIN (parm);
5850 /* We should never try to call the abstract constructor. */
5851 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
5853 if (DECL_HAS_VTT_PARM_P (fn))
5855 argarray[j++] = VEC_index (tree, args, arg_index);
5857 parm = TREE_CHAIN (parm);
5860 /* Bypass access control for 'this' parameter. */
5861 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5863 tree parmtype = TREE_VALUE (parm);
5864 tree arg = (first_arg != NULL_TREE
5866 : VEC_index (tree, args, arg_index));
5867 tree argtype = TREE_TYPE (arg);
5871 if (convs[i]->bad_p)
5873 if (complain & tf_error)
5874 permerror (input_location, "passing %qT as %<this%> argument of %q#D discards qualifiers",
5875 TREE_TYPE (argtype), fn);
5877 return error_mark_node;
5880 /* [class.mfct.nonstatic]: If a nonstatic member function of a class
5881 X is called for an object that is not of type X, or of a type
5882 derived from X, the behavior is undefined.
5884 So we can assume that anything passed as 'this' is non-null, and
5885 optimize accordingly. */
5886 gcc_assert (TREE_CODE (parmtype) == POINTER_TYPE);
5887 /* Convert to the base in which the function was declared. */
5888 gcc_assert (cand->conversion_path != NULL_TREE);
5889 converted_arg = build_base_path (PLUS_EXPR,
5891 cand->conversion_path,
5893 /* Check that the base class is accessible. */
5894 if (!accessible_base_p (TREE_TYPE (argtype),
5895 BINFO_TYPE (cand->conversion_path), true))
5896 error ("%qT is not an accessible base of %qT",
5897 BINFO_TYPE (cand->conversion_path),
5898 TREE_TYPE (argtype));
5899 /* If fn was found by a using declaration, the conversion path
5900 will be to the derived class, not the base declaring fn. We
5901 must convert from derived to base. */
5902 base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
5903 TREE_TYPE (parmtype), ba_unique, NULL);
5904 converted_arg = build_base_path (PLUS_EXPR, converted_arg,
5907 argarray[j++] = converted_arg;
5908 parm = TREE_CHAIN (parm);
5909 if (first_arg != NULL_TREE)
5910 first_arg = NULL_TREE;
5917 gcc_assert (first_arg == NULL_TREE);
5918 for (; arg_index < VEC_length (tree, args) && parm;
5919 parm = TREE_CHAIN (parm), ++arg_index, ++i)
5921 tree type = TREE_VALUE (parm);
5922 tree arg = VEC_index (tree, args, arg_index);
5926 /* Don't make a copy here if build_call is going to. */
5927 if (conv->kind == ck_rvalue
5928 && COMPLETE_TYPE_P (complete_type (type))
5929 && !TREE_ADDRESSABLE (type))
5930 conv = conv->u.next;
5932 /* Warn about initializer_list deduction that isn't currently in the
5934 if (cxx_dialect > cxx98
5935 && flag_deduce_init_list
5936 && cand->template_decl
5937 && is_std_init_list (non_reference (type))
5938 && BRACE_ENCLOSED_INITIALIZER_P (arg))
5940 tree tmpl = TI_TEMPLATE (cand->template_decl);
5941 tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
5942 tree patparm = get_pattern_parm (realparm, tmpl);
5943 tree pattype = TREE_TYPE (patparm);
5944 if (PACK_EXPANSION_P (pattype))
5945 pattype = PACK_EXPANSION_PATTERN (pattype);
5946 pattype = non_reference (pattype);
5948 if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
5949 && (cand->explicit_targs == NULL_TREE
5950 || (TREE_VEC_LENGTH (cand->explicit_targs)
5951 <= TEMPLATE_TYPE_IDX (pattype))))
5953 pedwarn (input_location, 0, "deducing %qT as %qT",
5954 non_reference (TREE_TYPE (patparm)),
5955 non_reference (type));
5956 pedwarn (input_location, 0, " in call to %q+D", cand->fn);
5957 pedwarn (input_location, 0,
5958 " (you can disable this with -fno-deduce-init-list)");
5962 val = convert_like_with_context (conv, arg, fn, i-is_method, complain);
5964 val = convert_for_arg_passing (type, val);
5965 if (val == error_mark_node)
5966 return error_mark_node;
5968 argarray[j++] = val;
5971 /* Default arguments */
5972 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
5973 argarray[j++] = convert_default_arg (TREE_VALUE (parm),
5974 TREE_PURPOSE (parm),
5977 for (; arg_index < VEC_length (tree, args); ++arg_index)
5979 tree a = VEC_index (tree, args, arg_index);
5980 if (magic_varargs_p (fn))
5981 /* Do no conversions for magic varargs. */
5982 a = mark_type_use (a);
5984 a = convert_arg_to_ellipsis (a);
5988 gcc_assert (j <= nargs);
5991 check_function_arguments (TYPE_ATTRIBUTES (TREE_TYPE (fn)),
5992 nargs, argarray, TYPE_ARG_TYPES (TREE_TYPE (fn)));
5994 /* Avoid actually calling copy constructors and copy assignment operators,
5997 if (! flag_elide_constructors)
5998 /* Do things the hard way. */;
5999 else if (cand->num_convs == 1
6000 && (DECL_COPY_CONSTRUCTOR_P (fn)
6001 || DECL_MOVE_CONSTRUCTOR_P (fn)))
6004 tree arg = argarray[num_artificial_parms_for (fn)];
6006 bool trivial = trivial_fn_p (fn);
6008 /* Pull out the real argument, disregarding const-correctness. */
6010 while (CONVERT_EXPR_P (targ)
6011 || TREE_CODE (targ) == NON_LVALUE_EXPR)
6012 targ = TREE_OPERAND (targ, 0);
6013 if (TREE_CODE (targ) == ADDR_EXPR)
6015 targ = TREE_OPERAND (targ, 0);
6016 if (!same_type_ignoring_top_level_qualifiers_p
6017 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
6026 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
6028 if (TREE_CODE (arg) == TARGET_EXPR
6029 && TARGET_EXPR_LIST_INIT_P (arg))
6031 /* Copy-list-initialization doesn't require the constructor
6034 /* [class.copy]: the copy constructor is implicitly defined even if
6035 the implementation elided its use. */
6039 already_used = true;
6042 /* If we're creating a temp and we already have one, don't create a
6043 new one. If we're not creating a temp but we get one, use
6044 INIT_EXPR to collapse the temp into our target. Otherwise, if the
6045 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
6046 temp or an INIT_EXPR otherwise. */
6048 if (integer_zerop (fa))
6050 if (TREE_CODE (arg) == TARGET_EXPR)
6053 return force_target_expr (DECL_CONTEXT (fn), arg);
6055 else if (TREE_CODE (arg) == TARGET_EXPR || trivial)
6057 tree to = stabilize_reference (cp_build_indirect_ref (fa, RO_NULL,
6060 val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
6064 else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
6065 && trivial_fn_p (fn))
6067 tree to = stabilize_reference
6068 (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
6069 tree type = TREE_TYPE (to);
6070 tree as_base = CLASSTYPE_AS_BASE (type);
6071 tree arg = argarray[1];
6073 if (is_really_empty_class (type))
6075 /* Avoid copying empty classes. */
6076 val = build2 (COMPOUND_EXPR, void_type_node, to, arg);
6077 TREE_NO_WARNING (val) = 1;
6078 val = build2 (COMPOUND_EXPR, type, val, to);
6079 TREE_NO_WARNING (val) = 1;
6081 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
6083 arg = cp_build_indirect_ref (arg, RO_NULL, complain);
6084 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
6088 /* We must only copy the non-tail padding parts.
6089 Use __builtin_memcpy for the bitwise copy.
6090 FIXME fix 22488 so we can go back to using MODIFY_EXPR
6091 instead of an explicit call to memcpy. */
6093 tree arg0, arg1, arg2, t;
6094 tree test = NULL_TREE;
6096 arg2 = TYPE_SIZE_UNIT (as_base);
6098 arg0 = cp_build_addr_expr (to, complain);
6100 if (!can_trust_pointer_alignment ())
6102 /* If we can't be sure about pointer alignment, a call
6103 to __builtin_memcpy is expanded as a call to memcpy, which
6104 is invalid with identical args. Otherwise it is
6105 expanded as a block move, which should be safe. */
6106 arg0 = save_expr (arg0);
6107 arg1 = save_expr (arg1);
6108 test = build2 (EQ_EXPR, boolean_type_node, arg0, arg1);
6110 t = implicit_built_in_decls[BUILT_IN_MEMCPY];
6111 t = build_call_n (t, 3, arg0, arg1, arg2);
6113 t = convert (TREE_TYPE (arg0), t);
6115 t = build3 (COND_EXPR, TREE_TYPE (t), test, arg0, t);
6116 val = cp_build_indirect_ref (t, RO_NULL, complain);
6117 TREE_NO_WARNING (val) = 1;
6122 /* FIXME handle trivial default constructor and destructor, too. */
6127 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
6130 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
6133 gcc_assert (binfo && binfo != error_mark_node);
6135 /* Warn about deprecated virtual functions now, since we're about
6136 to throw away the decl. */
6137 if (TREE_DEPRECATED (fn))
6138 warn_deprecated_use (fn, NULL_TREE);
6140 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1);
6141 if (TREE_SIDE_EFFECTS (argarray[0]))
6142 argarray[0] = save_expr (argarray[0]);
6143 t = build_pointer_type (TREE_TYPE (fn));
6144 if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
6145 fn = build_java_interface_fn_ref (fn, argarray[0]);
6147 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
6151 fn = build_addr_func (fn);
6153 return build_cxx_call (fn, nargs, argarray);
6156 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
6157 This function performs no overload resolution, conversion, or other
6158 high-level operations. */
6161 build_cxx_call (tree fn, int nargs, tree *argarray)
6165 fn = build_call_a (fn, nargs, argarray);
6167 /* If this call might throw an exception, note that fact. */
6168 fndecl = get_callee_fndecl (fn);
6169 if ((!fndecl || !TREE_NOTHROW (fndecl))
6170 && at_function_scope_p ()
6172 && cp_function_chain)
6173 cp_function_chain->can_throw = 1;
6175 /* Check that arguments to builtin functions match the expectations. */
6177 && DECL_BUILT_IN (fndecl)
6178 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6179 && !check_builtin_function_arguments (fndecl, nargs, argarray))
6180 return error_mark_node;
6182 /* Some built-in function calls will be evaluated at compile-time in
6184 fn = fold_if_not_in_template (fn);
6186 if (VOID_TYPE_P (TREE_TYPE (fn)))
6189 fn = require_complete_type (fn);
6190 if (fn == error_mark_node)
6191 return error_mark_node;
6193 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
6194 fn = build_cplus_new (TREE_TYPE (fn), fn);
6195 return convert_from_reference (fn);
6198 static GTY(()) tree java_iface_lookup_fn;
6200 /* Make an expression which yields the address of the Java interface
6201 method FN. This is achieved by generating a call to libjava's
6202 _Jv_LookupInterfaceMethodIdx(). */
6205 build_java_interface_fn_ref (tree fn, tree instance)
6207 tree lookup_fn, method, idx;
6208 tree klass_ref, iface, iface_ref;
6211 if (!java_iface_lookup_fn)
6213 tree ftype = build_function_type_list (ptr_type_node,
6214 ptr_type_node, ptr_type_node,
6215 java_int_type_node, NULL_TREE);
6216 java_iface_lookup_fn
6217 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx", ftype,
6218 0, NOT_BUILT_IN, NULL, NULL_TREE);
6221 /* Look up the pointer to the runtime java.lang.Class object for `instance'.
6222 This is the first entry in the vtable. */
6223 klass_ref = build_vtbl_ref (cp_build_indirect_ref (instance, RO_NULL,
6224 tf_warning_or_error),
6227 /* Get the java.lang.Class pointer for the interface being called. */
6228 iface = DECL_CONTEXT (fn);
6229 iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
6230 if (!iface_ref || TREE_CODE (iface_ref) != VAR_DECL
6231 || DECL_CONTEXT (iface_ref) != iface)
6233 error ("could not find class$ field in java interface type %qT",
6235 return error_mark_node;
6237 iface_ref = build_address (iface_ref);
6238 iface_ref = convert (build_pointer_type (iface), iface_ref);
6240 /* Determine the itable index of FN. */
6242 for (method = TYPE_METHODS (iface); method; method = DECL_CHAIN (method))
6244 if (!DECL_VIRTUAL_P (method))
6250 idx = build_int_cst (NULL_TREE, i);
6252 lookup_fn = build1 (ADDR_EXPR,
6253 build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
6254 java_iface_lookup_fn);
6255 return build_call_nary (ptr_type_node, lookup_fn,
6256 3, klass_ref, iface_ref, idx);
6259 /* Returns the value to use for the in-charge parameter when making a
6260 call to a function with the indicated NAME.
6262 FIXME:Can't we find a neater way to do this mapping? */
6265 in_charge_arg_for_name (tree name)
6267 if (name == base_ctor_identifier
6268 || name == base_dtor_identifier)
6269 return integer_zero_node;
6270 else if (name == complete_ctor_identifier)
6271 return integer_one_node;
6272 else if (name == complete_dtor_identifier)
6273 return integer_two_node;
6274 else if (name == deleting_dtor_identifier)
6275 return integer_three_node;
6277 /* This function should only be called with one of the names listed
6283 /* Build a call to a constructor, destructor, or an assignment
6284 operator for INSTANCE, an expression with class type. NAME
6285 indicates the special member function to call; *ARGS are the
6286 arguments. ARGS may be NULL. This may change ARGS. BINFO
6287 indicates the base of INSTANCE that is to be passed as the `this'
6288 parameter to the member function called.
6290 FLAGS are the LOOKUP_* flags to use when processing the call.
6292 If NAME indicates a complete object constructor, INSTANCE may be
6293 NULL_TREE. In this case, the caller will call build_cplus_new to
6294 store the newly constructed object into a VAR_DECL. */
6297 build_special_member_call (tree instance, tree name, VEC(tree,gc) **args,
6298 tree binfo, int flags, tsubst_flags_t complain)
6301 /* The type of the subobject to be constructed or destroyed. */
6303 VEC(tree,gc) *allocated = NULL;
6306 gcc_assert (name == complete_ctor_identifier
6307 || name == base_ctor_identifier
6308 || name == complete_dtor_identifier
6309 || name == base_dtor_identifier
6310 || name == deleting_dtor_identifier
6311 || name == ansi_assopname (NOP_EXPR));
6314 /* Resolve the name. */
6315 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
6316 return error_mark_node;
6318 binfo = TYPE_BINFO (binfo);
6321 gcc_assert (binfo != NULL_TREE);
6323 class_type = BINFO_TYPE (binfo);
6325 /* Handle the special case where INSTANCE is NULL_TREE. */
6326 if (name == complete_ctor_identifier && !instance)
6328 instance = build_int_cst (build_pointer_type (class_type), 0);
6329 instance = build1 (INDIRECT_REF, class_type, instance);
6333 if (name == complete_dtor_identifier
6334 || name == base_dtor_identifier
6335 || name == deleting_dtor_identifier)
6336 gcc_assert (args == NULL || VEC_empty (tree, *args));
6338 /* Convert to the base class, if necessary. */
6339 if (!same_type_ignoring_top_level_qualifiers_p
6340 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
6342 if (name != ansi_assopname (NOP_EXPR))
6343 /* For constructors and destructors, either the base is
6344 non-virtual, or it is virtual but we are doing the
6345 conversion from a constructor or destructor for the
6346 complete object. In either case, we can convert
6348 instance = convert_to_base_statically (instance, binfo);
6350 /* However, for assignment operators, we must convert
6351 dynamically if the base is virtual. */
6352 instance = build_base_path (PLUS_EXPR, instance,
6353 binfo, /*nonnull=*/1);
6357 gcc_assert (instance != NULL_TREE);
6359 fns = lookup_fnfields (binfo, name, 1);
6361 /* When making a call to a constructor or destructor for a subobject
6362 that uses virtual base classes, pass down a pointer to a VTT for
6364 if ((name == base_ctor_identifier
6365 || name == base_dtor_identifier)
6366 && CLASSTYPE_VBASECLASSES (class_type))
6371 /* If the current function is a complete object constructor
6372 or destructor, then we fetch the VTT directly.
6373 Otherwise, we look it up using the VTT we were given. */
6374 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
6375 vtt = decay_conversion (vtt);
6376 vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
6377 build2 (EQ_EXPR, boolean_type_node,
6378 current_in_charge_parm, integer_zero_node),
6381 gcc_assert (BINFO_SUBVTT_INDEX (binfo));
6382 sub_vtt = build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtt), vtt,
6383 BINFO_SUBVTT_INDEX (binfo));
6387 allocated = make_tree_vector ();
6391 VEC_safe_insert (tree, gc, *args, 0, sub_vtt);
6394 ret = build_new_method_call (instance, fns, args,
6395 TYPE_BINFO (BINFO_TYPE (binfo)),
6399 if (allocated != NULL)
6400 release_tree_vector (allocated);
6405 /* Return the NAME, as a C string. The NAME indicates a function that
6406 is a member of TYPE. *FREE_P is set to true if the caller must
6407 free the memory returned.
6409 Rather than go through all of this, we should simply set the names
6410 of constructors and destructors appropriately, and dispense with
6411 ctor_identifier, dtor_identifier, etc. */
6414 name_as_c_string (tree name, tree type, bool *free_p)
6418 /* Assume that we will not allocate memory. */
6420 /* Constructors and destructors are special. */
6421 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
6424 = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))));
6425 /* For a destructor, add the '~'. */
6426 if (name == complete_dtor_identifier
6427 || name == base_dtor_identifier
6428 || name == deleting_dtor_identifier)
6430 pretty_name = concat ("~", pretty_name, NULL);
6431 /* Remember that we need to free the memory allocated. */
6435 else if (IDENTIFIER_TYPENAME_P (name))
6437 pretty_name = concat ("operator ",
6438 type_as_string_translate (TREE_TYPE (name),
6439 TFF_PLAIN_IDENTIFIER),
6441 /* Remember that we need to free the memory allocated. */
6445 pretty_name = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (name)));
6450 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
6451 be set, upon return, to the function called. ARGS may be NULL.
6452 This may change ARGS. */
6455 build_new_method_call (tree instance, tree fns, VEC(tree,gc) **args,
6456 tree conversion_path, int flags,
6457 tree *fn_p, tsubst_flags_t complain)
6459 struct z_candidate *candidates = 0, *cand;
6460 tree explicit_targs = NULL_TREE;
6461 tree basetype = NULL_TREE;
6464 tree first_mem_arg = NULL_TREE;
6467 bool skip_first_for_error;
6468 VEC(tree,gc) *user_args;
6471 int template_only = 0;
6475 VEC(tree,gc) *orig_args = NULL;
6478 gcc_assert (instance != NULL_TREE);
6480 /* We don't know what function we're going to call, yet. */
6484 if (error_operand_p (instance)
6485 || !fns || error_operand_p (fns))
6486 return error_mark_node;
6488 if (!BASELINK_P (fns))
6490 if (complain & tf_error)
6491 error ("call to non-function %qD", fns);
6492 return error_mark_node;
6495 orig_instance = instance;
6498 /* Dismantle the baselink to collect all the information we need. */
6499 if (!conversion_path)
6500 conversion_path = BASELINK_BINFO (fns);
6501 access_binfo = BASELINK_ACCESS_BINFO (fns);
6502 optype = BASELINK_OPTYPE (fns);
6503 fns = BASELINK_FUNCTIONS (fns);
6504 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
6506 explicit_targs = TREE_OPERAND (fns, 1);
6507 fns = TREE_OPERAND (fns, 0);
6510 gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
6511 || TREE_CODE (fns) == TEMPLATE_DECL
6512 || TREE_CODE (fns) == OVERLOAD);
6513 fn = get_first_fn (fns);
6514 name = DECL_NAME (fn);
6516 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
6517 gcc_assert (CLASS_TYPE_P (basetype));
6519 if (processing_template_decl)
6521 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
6522 instance = build_non_dependent_expr (instance);
6524 make_args_non_dependent (*args);
6527 user_args = args == NULL ? NULL : *args;
6528 /* Under DR 147 A::A() is an invalid constructor call,
6529 not a functional cast. */
6530 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
6532 if (! (complain & tf_error))
6533 return error_mark_node;
6535 permerror (input_location,
6536 "cannot call constructor %<%T::%D%> directly",
6538 permerror (input_location, " for a function-style cast, remove the "
6539 "redundant %<::%D%>", name);
6540 call = build_functional_cast (basetype, build_tree_list_vec (user_args),
6545 /* Figure out whether to skip the first argument for the error
6546 message we will display to users if an error occurs. We don't
6547 want to display any compiler-generated arguments. The "this"
6548 pointer hasn't been added yet. However, we must remove the VTT
6549 pointer if this is a call to a base-class constructor or
6551 skip_first_for_error = false;
6552 if (IDENTIFIER_CTOR_OR_DTOR_P (name))
6554 /* Callers should explicitly indicate whether they want to construct
6555 the complete object or just the part without virtual bases. */
6556 gcc_assert (name != ctor_identifier);
6557 /* Similarly for destructors. */
6558 gcc_assert (name != dtor_identifier);
6559 /* Remove the VTT pointer, if present. */
6560 if ((name == base_ctor_identifier || name == base_dtor_identifier)
6561 && CLASSTYPE_VBASECLASSES (basetype))
6562 skip_first_for_error = true;
6565 /* Process the argument list. */
6566 if (args != NULL && *args != NULL)
6568 *args = resolve_args (*args);
6570 return error_mark_node;
6573 instance_ptr = build_this (instance);
6575 /* It's OK to call destructors and constructors on cv-qualified objects.
6576 Therefore, convert the INSTANCE_PTR to the unqualified type, if
6578 if (DECL_DESTRUCTOR_P (fn)
6579 || DECL_CONSTRUCTOR_P (fn))
6581 tree type = build_pointer_type (basetype);
6582 if (!same_type_p (type, TREE_TYPE (instance_ptr)))
6583 instance_ptr = build_nop (type, instance_ptr);
6585 if (DECL_DESTRUCTOR_P (fn))
6586 name = complete_dtor_identifier;
6588 first_mem_arg = instance_ptr;
6590 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6591 p = conversion_obstack_alloc (0);
6593 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
6594 initializer, not T({ }). */
6595 if (DECL_CONSTRUCTOR_P (fn) && args != NULL && !VEC_empty (tree, *args)
6596 && BRACE_ENCLOSED_INITIALIZER_P (VEC_index (tree, *args, 0))
6597 && CONSTRUCTOR_IS_DIRECT_INIT (VEC_index (tree, *args, 0)))
6599 gcc_assert (VEC_length (tree, *args) == 1
6600 && !(flags & LOOKUP_ONLYCONVERTING));
6602 add_list_candidates (fns, first_mem_arg, VEC_index (tree, *args, 0),
6603 basetype, explicit_targs, template_only,
6604 conversion_path, access_binfo, flags, &candidates);
6608 add_candidates (fns, first_mem_arg, user_args, optype,
6609 explicit_targs, template_only, conversion_path,
6610 access_binfo, flags, &candidates);
6612 any_viable_p = false;
6613 candidates = splice_viable (candidates, pedantic, &any_viable_p);
6617 if (complain & tf_error)
6619 if (!COMPLETE_TYPE_P (basetype))
6620 cxx_incomplete_type_error (instance_ptr, basetype);
6622 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
6623 basetype, optype, build_tree_list_vec (user_args),
6624 TREE_TYPE (TREE_TYPE (instance_ptr)));
6631 pretty_name = name_as_c_string (name, basetype, &free_p);
6632 arglist = build_tree_list_vec (user_args);
6633 if (skip_first_for_error)
6634 arglist = TREE_CHAIN (arglist);
6635 error ("no matching function for call to %<%T::%s(%A)%#V%>",
6636 basetype, pretty_name, arglist,
6637 TREE_TYPE (TREE_TYPE (instance_ptr)));
6641 print_z_candidates (candidates);
6643 call = error_mark_node;
6647 cand = tourney (candidates);
6654 if (complain & tf_error)
6656 pretty_name = name_as_c_string (name, basetype, &free_p);
6657 arglist = build_tree_list_vec (user_args);
6658 if (skip_first_for_error)
6659 arglist = TREE_CHAIN (arglist);
6660 error ("call of overloaded %<%s(%A)%> is ambiguous", pretty_name,
6662 print_z_candidates (candidates);
6666 call = error_mark_node;
6672 if (!(flags & LOOKUP_NONVIRTUAL)
6673 && DECL_PURE_VIRTUAL_P (fn)
6674 && instance == current_class_ref
6675 && (DECL_CONSTRUCTOR_P (current_function_decl)
6676 || DECL_DESTRUCTOR_P (current_function_decl))
6677 && (complain & tf_warning))
6678 /* This is not an error, it is runtime undefined
6680 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ?
6681 "pure virtual %q#D called from constructor"
6682 : "pure virtual %q#D called from destructor"),
6685 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
6686 && is_dummy_object (instance_ptr))
6688 if (complain & tf_error)
6689 error ("cannot call member function %qD without object",
6691 call = error_mark_node;
6695 if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
6696 && resolves_to_fixed_type_p (instance, 0))
6697 flags |= LOOKUP_NONVIRTUAL;
6698 /* Now we know what function is being called. */
6701 /* Build the actual CALL_EXPR. */
6702 call = build_over_call (cand, flags, complain);
6703 /* In an expression of the form `a->f()' where `f' turns
6704 out to be a static member function, `a' is
6705 none-the-less evaluated. */
6706 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
6707 && !is_dummy_object (instance_ptr)
6708 && TREE_SIDE_EFFECTS (instance_ptr))
6709 call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
6710 instance_ptr, call);
6711 else if (call != error_mark_node
6712 && DECL_DESTRUCTOR_P (cand->fn)
6713 && !VOID_TYPE_P (TREE_TYPE (call)))
6714 /* An explicit call of the form "x->~X()" has type
6715 "void". However, on platforms where destructors
6716 return "this" (i.e., those where
6717 targetm.cxx.cdtor_returns_this is true), such calls
6718 will appear to have a return value of pointer type
6719 to the low-level call machinery. We do not want to
6720 change the low-level machinery, since we want to be
6721 able to optimize "delete f()" on such platforms as
6722 "operator delete(~X(f()))" (rather than generating
6723 "t = f(), ~X(t), operator delete (t)"). */
6724 call = build_nop (void_type_node, call);
6729 if (processing_template_decl && call != error_mark_node)
6731 bool cast_to_void = false;
6733 if (TREE_CODE (call) == COMPOUND_EXPR)
6734 call = TREE_OPERAND (call, 1);
6735 else if (TREE_CODE (call) == NOP_EXPR)
6737 cast_to_void = true;
6738 call = TREE_OPERAND (call, 0);
6740 if (TREE_CODE (call) == INDIRECT_REF)
6741 call = TREE_OPERAND (call, 0);
6742 call = (build_min_non_dep_call_vec
6744 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
6745 orig_instance, orig_fns, NULL_TREE),
6747 call = convert_from_reference (call);
6749 call = build_nop (void_type_node, call);
6752 /* Free all the conversions we allocated. */
6753 obstack_free (&conversion_obstack, p);
6755 if (orig_args != NULL)
6756 release_tree_vector (orig_args);
6761 /* Returns true iff standard conversion sequence ICS1 is a proper
6762 subsequence of ICS2. */
6765 is_subseq (conversion *ics1, conversion *ics2)
6767 /* We can assume that a conversion of the same code
6768 between the same types indicates a subsequence since we only get
6769 here if the types we are converting from are the same. */
6771 while (ics1->kind == ck_rvalue
6772 || ics1->kind == ck_lvalue)
6773 ics1 = ics1->u.next;
6777 while (ics2->kind == ck_rvalue
6778 || ics2->kind == ck_lvalue)
6779 ics2 = ics2->u.next;
6781 if (ics2->kind == ck_user
6782 || ics2->kind == ck_ambig
6783 || ics2->kind == ck_aggr
6784 || ics2->kind == ck_list
6785 || ics2->kind == ck_identity)
6786 /* At this point, ICS1 cannot be a proper subsequence of
6787 ICS2. We can get a USER_CONV when we are comparing the
6788 second standard conversion sequence of two user conversion
6792 ics2 = ics2->u.next;
6794 if (ics2->kind == ics1->kind
6795 && same_type_p (ics2->type, ics1->type)
6796 && same_type_p (ics2->u.next->type,
6797 ics1->u.next->type))
6802 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
6803 be any _TYPE nodes. */
6806 is_properly_derived_from (tree derived, tree base)
6808 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
6811 /* We only allow proper derivation here. The DERIVED_FROM_P macro
6812 considers every class derived from itself. */
6813 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
6814 && DERIVED_FROM_P (base, derived));
6817 /* We build the ICS for an implicit object parameter as a pointer
6818 conversion sequence. However, such a sequence should be compared
6819 as if it were a reference conversion sequence. If ICS is the
6820 implicit conversion sequence for an implicit object parameter,
6821 modify it accordingly. */
6824 maybe_handle_implicit_object (conversion **ics)
6828 /* [over.match.funcs]
6830 For non-static member functions, the type of the
6831 implicit object parameter is "reference to cv X"
6832 where X is the class of which the function is a
6833 member and cv is the cv-qualification on the member
6834 function declaration. */
6835 conversion *t = *ics;
6836 tree reference_type;
6838 /* The `this' parameter is a pointer to a class type. Make the
6839 implicit conversion talk about a reference to that same class
6841 reference_type = TREE_TYPE (t->type);
6842 reference_type = build_reference_type (reference_type);
6844 if (t->kind == ck_qual)
6846 if (t->kind == ck_ptr)
6848 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
6849 t = direct_reference_binding (reference_type, t);
6851 t->rvaluedness_matches_p = 0;
6856 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
6857 and return the initial reference binding conversion. Otherwise,
6858 leave *ICS unchanged and return NULL. */
6861 maybe_handle_ref_bind (conversion **ics)
6863 if ((*ics)->kind == ck_ref_bind)
6865 conversion *old_ics = *ics;
6866 *ics = old_ics->u.next;
6867 (*ics)->user_conv_p = old_ics->user_conv_p;
6874 /* Compare two implicit conversion sequences according to the rules set out in
6875 [over.ics.rank]. Return values:
6877 1: ics1 is better than ics2
6878 -1: ics2 is better than ics1
6879 0: ics1 and ics2 are indistinguishable */
6882 compare_ics (conversion *ics1, conversion *ics2)
6888 tree deref_from_type1 = NULL_TREE;
6889 tree deref_from_type2 = NULL_TREE;
6890 tree deref_to_type1 = NULL_TREE;
6891 tree deref_to_type2 = NULL_TREE;
6892 conversion_rank rank1, rank2;
6894 /* REF_BINDING is nonzero if the result of the conversion sequence
6895 is a reference type. In that case REF_CONV is the reference
6896 binding conversion. */
6897 conversion *ref_conv1;
6898 conversion *ref_conv2;
6900 /* Handle implicit object parameters. */
6901 maybe_handle_implicit_object (&ics1);
6902 maybe_handle_implicit_object (&ics2);
6904 /* Handle reference parameters. */
6905 ref_conv1 = maybe_handle_ref_bind (&ics1);
6906 ref_conv2 = maybe_handle_ref_bind (&ics2);
6908 /* List-initialization sequence L1 is a better conversion sequence than
6909 list-initialization sequence L2 if L1 converts to
6910 std::initializer_list<X> for some X and L2 does not. */
6911 if (ics1->kind == ck_list && ics2->kind != ck_list)
6913 if (ics2->kind == ck_list && ics1->kind != ck_list)
6918 When comparing the basic forms of implicit conversion sequences (as
6919 defined in _over.best.ics_)
6921 --a standard conversion sequence (_over.ics.scs_) is a better
6922 conversion sequence than a user-defined conversion sequence
6923 or an ellipsis conversion sequence, and
6925 --a user-defined conversion sequence (_over.ics.user_) is a
6926 better conversion sequence than an ellipsis conversion sequence
6927 (_over.ics.ellipsis_). */
6928 rank1 = CONVERSION_RANK (ics1);
6929 rank2 = CONVERSION_RANK (ics2);
6933 else if (rank1 < rank2)
6936 if (rank1 == cr_bad)
6938 /* Both ICS are bad. We try to make a decision based on what would
6939 have happened if they'd been good. This is not an extension,
6940 we'll still give an error when we build up the call; this just
6941 helps us give a more helpful error message. */
6942 rank1 = BAD_CONVERSION_RANK (ics1);
6943 rank2 = BAD_CONVERSION_RANK (ics2);
6947 else if (rank1 < rank2)
6950 /* We couldn't make up our minds; try to figure it out below. */
6953 if (ics1->ellipsis_p)
6954 /* Both conversions are ellipsis conversions. */
6957 /* User-defined conversion sequence U1 is a better conversion sequence
6958 than another user-defined conversion sequence U2 if they contain the
6959 same user-defined conversion operator or constructor and if the sec-
6960 ond standard conversion sequence of U1 is better than the second
6961 standard conversion sequence of U2. */
6963 /* Handle list-conversion with the same code even though it isn't always
6964 ranked as a user-defined conversion and it doesn't have a second
6965 standard conversion sequence; it will still have the desired effect.
6966 Specifically, we need to do the reference binding comparison at the
6967 end of this function. */
6969 if (ics1->user_conv_p || ics1->kind == ck_list)
6974 for (t1 = ics1; t1->kind != ck_user; t1 = t1->u.next)
6975 if (t1->kind == ck_ambig || t1->kind == ck_aggr
6976 || t1->kind == ck_list)
6978 for (t2 = ics2; t2->kind != ck_user; t2 = t2->u.next)
6979 if (t2->kind == ck_ambig || t2->kind == ck_aggr
6980 || t2->kind == ck_list)
6983 if (t1->kind != t2->kind)
6985 else if (t1->kind == ck_user)
6987 if (t1->cand->fn != t2->cand->fn)
6992 /* For ambiguous or aggregate conversions, use the target type as
6993 a proxy for the conversion function. */
6994 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
6998 /* We can just fall through here, after setting up
6999 FROM_TYPE1 and FROM_TYPE2. */
7000 from_type1 = t1->type;
7001 from_type2 = t2->type;
7008 /* We're dealing with two standard conversion sequences.
7012 Standard conversion sequence S1 is a better conversion
7013 sequence than standard conversion sequence S2 if
7015 --S1 is a proper subsequence of S2 (comparing the conversion
7016 sequences in the canonical form defined by _over.ics.scs_,
7017 excluding any Lvalue Transformation; the identity
7018 conversion sequence is considered to be a subsequence of
7019 any non-identity conversion sequence */
7022 while (t1->kind != ck_identity)
7024 from_type1 = t1->type;
7027 while (t2->kind != ck_identity)
7029 from_type2 = t2->type;
7032 /* One sequence can only be a subsequence of the other if they start with
7033 the same type. They can start with different types when comparing the
7034 second standard conversion sequence in two user-defined conversion
7036 if (same_type_p (from_type1, from_type2))
7038 if (is_subseq (ics1, ics2))
7040 if (is_subseq (ics2, ics1))
7048 --the rank of S1 is better than the rank of S2 (by the rules
7051 Standard conversion sequences are ordered by their ranks: an Exact
7052 Match is a better conversion than a Promotion, which is a better
7053 conversion than a Conversion.
7055 Two conversion sequences with the same rank are indistinguishable
7056 unless one of the following rules applies:
7058 --A conversion that does not a convert a pointer, pointer to member,
7059 or std::nullptr_t to bool is better than one that does.
7061 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
7062 so that we do not have to check it explicitly. */
7063 if (ics1->rank < ics2->rank)
7065 else if (ics2->rank < ics1->rank)
7068 to_type1 = ics1->type;
7069 to_type2 = ics2->type;
7071 /* A conversion from scalar arithmetic type to complex is worse than a
7072 conversion between scalar arithmetic types. */
7073 if (same_type_p (from_type1, from_type2)
7074 && ARITHMETIC_TYPE_P (from_type1)
7075 && ARITHMETIC_TYPE_P (to_type1)
7076 && ARITHMETIC_TYPE_P (to_type2)
7077 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
7078 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
7080 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
7086 if (TYPE_PTR_P (from_type1)
7087 && TYPE_PTR_P (from_type2)
7088 && TYPE_PTR_P (to_type1)
7089 && TYPE_PTR_P (to_type2))
7091 deref_from_type1 = TREE_TYPE (from_type1);
7092 deref_from_type2 = TREE_TYPE (from_type2);
7093 deref_to_type1 = TREE_TYPE (to_type1);
7094 deref_to_type2 = TREE_TYPE (to_type2);
7096 /* The rules for pointers to members A::* are just like the rules
7097 for pointers A*, except opposite: if B is derived from A then
7098 A::* converts to B::*, not vice versa. For that reason, we
7099 switch the from_ and to_ variables here. */
7100 else if ((TYPE_PTRMEM_P (from_type1) && TYPE_PTRMEM_P (from_type2)
7101 && TYPE_PTRMEM_P (to_type1) && TYPE_PTRMEM_P (to_type2))
7102 || (TYPE_PTRMEMFUNC_P (from_type1)
7103 && TYPE_PTRMEMFUNC_P (from_type2)
7104 && TYPE_PTRMEMFUNC_P (to_type1)
7105 && TYPE_PTRMEMFUNC_P (to_type2)))
7107 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
7108 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
7109 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
7110 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
7113 if (deref_from_type1 != NULL_TREE
7114 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
7115 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
7117 /* This was one of the pointer or pointer-like conversions.
7121 --If class B is derived directly or indirectly from class A,
7122 conversion of B* to A* is better than conversion of B* to
7123 void*, and conversion of A* to void* is better than
7124 conversion of B* to void*. */
7125 if (TREE_CODE (deref_to_type1) == VOID_TYPE
7126 && TREE_CODE (deref_to_type2) == VOID_TYPE)
7128 if (is_properly_derived_from (deref_from_type1,
7131 else if (is_properly_derived_from (deref_from_type2,
7135 else if (TREE_CODE (deref_to_type1) == VOID_TYPE
7136 || TREE_CODE (deref_to_type2) == VOID_TYPE)
7138 if (same_type_p (deref_from_type1, deref_from_type2))
7140 if (TREE_CODE (deref_to_type2) == VOID_TYPE)
7142 if (is_properly_derived_from (deref_from_type1,
7146 /* We know that DEREF_TO_TYPE1 is `void' here. */
7147 else if (is_properly_derived_from (deref_from_type1,
7152 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
7153 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
7157 --If class B is derived directly or indirectly from class A
7158 and class C is derived directly or indirectly from B,
7160 --conversion of C* to B* is better than conversion of C* to
7163 --conversion of B* to A* is better than conversion of C* to
7165 if (same_type_p (deref_from_type1, deref_from_type2))
7167 if (is_properly_derived_from (deref_to_type1,
7170 else if (is_properly_derived_from (deref_to_type2,
7174 else if (same_type_p (deref_to_type1, deref_to_type2))
7176 if (is_properly_derived_from (deref_from_type2,
7179 else if (is_properly_derived_from (deref_from_type1,
7185 else if (CLASS_TYPE_P (non_reference (from_type1))
7186 && same_type_p (from_type1, from_type2))
7188 tree from = non_reference (from_type1);
7192 --binding of an expression of type C to a reference of type
7193 B& is better than binding an expression of type C to a
7194 reference of type A&
7196 --conversion of C to B is better than conversion of C to A, */
7197 if (is_properly_derived_from (from, to_type1)
7198 && is_properly_derived_from (from, to_type2))
7200 if (is_properly_derived_from (to_type1, to_type2))
7202 else if (is_properly_derived_from (to_type2, to_type1))
7206 else if (CLASS_TYPE_P (non_reference (to_type1))
7207 && same_type_p (to_type1, to_type2))
7209 tree to = non_reference (to_type1);
7213 --binding of an expression of type B to a reference of type
7214 A& is better than binding an expression of type C to a
7215 reference of type A&,
7217 --conversion of B to A is better than conversion of C to A */
7218 if (is_properly_derived_from (from_type1, to)
7219 && is_properly_derived_from (from_type2, to))
7221 if (is_properly_derived_from (from_type2, from_type1))
7223 else if (is_properly_derived_from (from_type1, from_type2))
7230 --S1 and S2 differ only in their qualification conversion and yield
7231 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
7232 qualification signature of type T1 is a proper subset of the cv-
7233 qualification signature of type T2 */
7234 if (ics1->kind == ck_qual
7235 && ics2->kind == ck_qual
7236 && same_type_p (from_type1, from_type2))
7238 int result = comp_cv_qual_signature (to_type1, to_type2);
7245 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
7246 to an implicit object parameter, and either S1 binds an lvalue reference
7247 to an lvalue and S2 binds an rvalue reference or S1 binds an rvalue
7248 reference to an rvalue and S2 binds an lvalue reference
7249 (C++0x draft standard, 13.3.3.2)
7251 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
7252 types to which the references refer are the same type except for
7253 top-level cv-qualifiers, and the type to which the reference
7254 initialized by S2 refers is more cv-qualified than the type to
7255 which the reference initialized by S1 refers */
7257 if (ref_conv1 && ref_conv2)
7259 if (!ref_conv1->this_p && !ref_conv2->this_p
7260 && (TYPE_REF_IS_RVALUE (ref_conv1->type)
7261 != TYPE_REF_IS_RVALUE (ref_conv2->type)))
7263 if (ref_conv1->rvaluedness_matches_p)
7265 if (ref_conv2->rvaluedness_matches_p)
7269 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
7270 return comp_cv_qualification (TREE_TYPE (ref_conv2->type),
7271 TREE_TYPE (ref_conv1->type));
7274 /* Neither conversion sequence is better than the other. */
7278 /* The source type for this standard conversion sequence. */
7281 source_type (conversion *t)
7283 for (;; t = t->u.next)
7285 if (t->kind == ck_user
7286 || t->kind == ck_ambig
7287 || t->kind == ck_identity)
7293 /* Note a warning about preferring WINNER to LOSER. We do this by storing
7294 a pointer to LOSER and re-running joust to produce the warning if WINNER
7295 is actually used. */
7298 add_warning (struct z_candidate *winner, struct z_candidate *loser)
7300 candidate_warning *cw = (candidate_warning *)
7301 conversion_obstack_alloc (sizeof (candidate_warning));
7303 cw->next = winner->warnings;
7304 winner->warnings = cw;
7307 /* Compare two candidates for overloading as described in
7308 [over.match.best]. Return values:
7310 1: cand1 is better than cand2
7311 -1: cand2 is better than cand1
7312 0: cand1 and cand2 are indistinguishable */
7315 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn)
7318 int off1 = 0, off2 = 0;
7322 /* Candidates that involve bad conversions are always worse than those
7324 if (cand1->viable > cand2->viable)
7326 if (cand1->viable < cand2->viable)
7329 /* If we have two pseudo-candidates for conversions to the same type,
7330 or two candidates for the same function, arbitrarily pick one. */
7331 if (cand1->fn == cand2->fn
7332 && (IS_TYPE_OR_DECL_P (cand1->fn)))
7335 /* a viable function F1
7336 is defined to be a better function than another viable function F2 if
7337 for all arguments i, ICSi(F1) is not a worse conversion sequence than
7338 ICSi(F2), and then */
7340 /* for some argument j, ICSj(F1) is a better conversion sequence than
7343 /* For comparing static and non-static member functions, we ignore
7344 the implicit object parameter of the non-static function. The
7345 standard says to pretend that the static function has an object
7346 parm, but that won't work with operator overloading. */
7347 len = cand1->num_convs;
7348 if (len != cand2->num_convs)
7350 int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
7351 int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
7353 gcc_assert (static_1 != static_2);
7364 for (i = 0; i < len; ++i)
7366 conversion *t1 = cand1->convs[i + off1];
7367 conversion *t2 = cand2->convs[i + off2];
7368 int comp = compare_ics (t1, t2);
7373 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
7374 == cr_std + cr_promotion)
7375 && t1->kind == ck_std
7376 && t2->kind == ck_std
7377 && TREE_CODE (t1->type) == INTEGER_TYPE
7378 && TREE_CODE (t2->type) == INTEGER_TYPE
7379 && (TYPE_PRECISION (t1->type)
7380 == TYPE_PRECISION (t2->type))
7381 && (TYPE_UNSIGNED (t1->u.next->type)
7382 || (TREE_CODE (t1->u.next->type)
7385 tree type = t1->u.next->type;
7387 struct z_candidate *w, *l;
7389 type1 = t1->type, type2 = t2->type,
7390 w = cand1, l = cand2;
7392 type1 = t2->type, type2 = t1->type,
7393 w = cand2, l = cand1;
7397 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
7398 type, type1, type2);
7399 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
7405 if (winner && comp != winner)
7414 /* warn about confusing overload resolution for user-defined conversions,
7415 either between a constructor and a conversion op, or between two
7417 if (winner && warn_conversion && cand1->second_conv
7418 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
7419 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
7421 struct z_candidate *w, *l;
7422 bool give_warning = false;
7425 w = cand1, l = cand2;
7427 w = cand2, l = cand1;
7429 /* We don't want to complain about `X::operator T1 ()'
7430 beating `X::operator T2 () const', when T2 is a no less
7431 cv-qualified version of T1. */
7432 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
7433 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
7435 tree t = TREE_TYPE (TREE_TYPE (l->fn));
7436 tree f = TREE_TYPE (TREE_TYPE (w->fn));
7438 if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
7443 if (!comp_ptr_ttypes (t, f))
7444 give_warning = true;
7447 give_warning = true;
7453 tree source = source_type (w->convs[0]);
7454 if (! DECL_CONSTRUCTOR_P (w->fn))
7455 source = TREE_TYPE (source);
7456 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
7457 && warning (OPT_Wconversion, " for conversion from %qT to %qT",
7458 source, w->second_conv->type))
7460 inform (input_location, " because conversion sequence for the argument is better");
7471 F1 is a non-template function and F2 is a template function
7474 if (!cand1->template_decl && cand2->template_decl)
7476 else if (cand1->template_decl && !cand2->template_decl)
7480 F1 and F2 are template functions and the function template for F1 is
7481 more specialized than the template for F2 according to the partial
7484 if (cand1->template_decl && cand2->template_decl)
7486 winner = more_specialized_fn
7487 (TI_TEMPLATE (cand1->template_decl),
7488 TI_TEMPLATE (cand2->template_decl),
7489 /* [temp.func.order]: The presence of unused ellipsis and default
7490 arguments has no effect on the partial ordering of function
7491 templates. add_function_candidate() will not have
7492 counted the "this" argument for constructors. */
7493 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
7499 the context is an initialization by user-defined conversion (see
7500 _dcl.init_ and _over.match.user_) and the standard conversion
7501 sequence from the return type of F1 to the destination type (i.e.,
7502 the type of the entity being initialized) is a better conversion
7503 sequence than the standard conversion sequence from the return type
7504 of F2 to the destination type. */
7506 if (cand1->second_conv)
7508 winner = compare_ics (cand1->second_conv, cand2->second_conv);
7513 /* Check whether we can discard a builtin candidate, either because we
7514 have two identical ones or matching builtin and non-builtin candidates.
7516 (Pedantically in the latter case the builtin which matched the user
7517 function should not be added to the overload set, but we spot it here.
7520 ... the builtin candidates include ...
7521 - do not have the same parameter type list as any non-template
7522 non-member candidate. */
7524 if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE
7525 || TREE_CODE (cand2->fn) == IDENTIFIER_NODE)
7527 for (i = 0; i < len; ++i)
7528 if (!same_type_p (cand1->convs[i]->type,
7529 cand2->convs[i]->type))
7531 if (i == cand1->num_convs)
7533 if (cand1->fn == cand2->fn)
7534 /* Two built-in candidates; arbitrarily pick one. */
7536 else if (TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
7537 /* cand1 is built-in; prefer cand2. */
7540 /* cand2 is built-in; prefer cand1. */
7545 /* If the two function declarations represent the same function (this can
7546 happen with declarations in multiple scopes and arg-dependent lookup),
7547 arbitrarily choose one. But first make sure the default args we're
7549 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
7550 && equal_functions (cand1->fn, cand2->fn))
7552 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
7553 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
7555 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
7557 for (i = 0; i < len; ++i)
7559 /* Don't crash if the fn is variadic. */
7562 parms1 = TREE_CHAIN (parms1);
7563 parms2 = TREE_CHAIN (parms2);
7567 parms1 = TREE_CHAIN (parms1);
7569 parms2 = TREE_CHAIN (parms2);
7573 if (!cp_tree_equal (TREE_PURPOSE (parms1),
7574 TREE_PURPOSE (parms2)))
7578 permerror (input_location, "default argument mismatch in "
7579 "overload resolution");
7580 inform (input_location,
7581 " candidate 1: %q+#F", cand1->fn);
7582 inform (input_location,
7583 " candidate 2: %q+#F", cand2->fn);
7586 add_warning (cand1, cand2);
7589 parms1 = TREE_CHAIN (parms1);
7590 parms2 = TREE_CHAIN (parms2);
7598 /* Extension: If the worst conversion for one candidate is worse than the
7599 worst conversion for the other, take the first. */
7602 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
7603 struct z_candidate *w = 0, *l = 0;
7605 for (i = 0; i < len; ++i)
7607 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
7608 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
7609 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
7610 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
7613 winner = 1, w = cand1, l = cand2;
7615 winner = -1, w = cand2, l = cand1;
7618 /* Don't choose a deleted function over ambiguity. */
7619 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
7623 pedwarn (input_location, 0,
7624 "ISO C++ says that these are ambiguous, even "
7625 "though the worst conversion for the first is better than "
7626 "the worst conversion for the second:");
7627 print_z_candidate (_("candidate 1:"), w);
7628 print_z_candidate (_("candidate 2:"), l);
7636 gcc_assert (!winner);
7640 /* Given a list of candidates for overloading, find the best one, if any.
7641 This algorithm has a worst case of O(2n) (winner is last), and a best
7642 case of O(n/2) (totally ambiguous); much better than a sorting
7645 static struct z_candidate *
7646 tourney (struct z_candidate *candidates)
7648 struct z_candidate *champ = candidates, *challenger;
7650 int champ_compared_to_predecessor = 0;
7652 /* Walk through the list once, comparing each current champ to the next
7653 candidate, knocking out a candidate or two with each comparison. */
7655 for (challenger = champ->next; challenger; )
7657 fate = joust (champ, challenger, 0);
7659 challenger = challenger->next;
7664 champ = challenger->next;
7667 champ_compared_to_predecessor = 0;
7672 champ_compared_to_predecessor = 1;
7675 challenger = champ->next;
7679 /* Make sure the champ is better than all the candidates it hasn't yet
7680 been compared to. */
7682 for (challenger = candidates;
7684 && !(champ_compared_to_predecessor && challenger->next == champ);
7685 challenger = challenger->next)
7687 fate = joust (champ, challenger, 0);
7695 /* Returns nonzero if things of type FROM can be converted to TO. */
7698 can_convert (tree to, tree from)
7700 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT);
7703 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
7706 can_convert_arg (tree to, tree from, tree arg, int flags)
7712 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7713 p = conversion_obstack_alloc (0);
7715 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
7717 ok_p = (t && !t->bad_p);
7719 /* Free all the conversions we allocated. */
7720 obstack_free (&conversion_obstack, p);
7725 /* Like can_convert_arg, but allows dubious conversions as well. */
7728 can_convert_arg_bad (tree to, tree from, tree arg, int flags)
7733 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7734 p = conversion_obstack_alloc (0);
7735 /* Try to perform the conversion. */
7736 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
7738 /* Free all the conversions we allocated. */
7739 obstack_free (&conversion_obstack, p);
7744 /* Convert EXPR to TYPE. Return the converted expression.
7746 Note that we allow bad conversions here because by the time we get to
7747 this point we are committed to doing the conversion. If we end up
7748 doing a bad conversion, convert_like will complain. */
7751 perform_implicit_conversion_flags (tree type, tree expr, tsubst_flags_t complain, int flags)
7756 if (error_operand_p (expr))
7757 return error_mark_node;
7759 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7760 p = conversion_obstack_alloc (0);
7762 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
7768 if (complain & tf_error)
7770 /* If expr has unknown type, then it is an overloaded function.
7771 Call instantiate_type to get good error messages. */
7772 if (TREE_TYPE (expr) == unknown_type_node)
7773 instantiate_type (type, expr, complain);
7774 else if (invalid_nonstatic_memfn_p (expr, complain))
7775 /* We gave an error. */;
7777 error ("could not convert %qE to %qT", expr, type);
7779 expr = error_mark_node;
7781 else if (processing_template_decl)
7783 /* In a template, we are only concerned about determining the
7784 type of non-dependent expressions, so we do not have to
7785 perform the actual conversion. */
7786 if (TREE_TYPE (expr) != type)
7787 expr = build_nop (type, expr);
7790 expr = convert_like (conv, expr, complain);
7792 /* Free all the conversions we allocated. */
7793 obstack_free (&conversion_obstack, p);
7799 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
7801 return perform_implicit_conversion_flags (type, expr, complain, LOOKUP_IMPLICIT);
7804 /* Convert EXPR to TYPE (as a direct-initialization) if that is
7805 permitted. If the conversion is valid, the converted expression is
7806 returned. Otherwise, NULL_TREE is returned, except in the case
7807 that TYPE is a class type; in that case, an error is issued. If
7808 C_CAST_P is true, then this direction initialization is taking
7809 place as part of a static_cast being attempted as part of a C-style
7813 perform_direct_initialization_if_possible (tree type,
7816 tsubst_flags_t complain)
7821 if (type == error_mark_node || error_operand_p (expr))
7822 return error_mark_node;
7825 If the destination type is a (possibly cv-qualified) class type:
7827 -- If the initialization is direct-initialization ...,
7828 constructors are considered. ... If no constructor applies, or
7829 the overload resolution is ambiguous, the initialization is
7831 if (CLASS_TYPE_P (type))
7833 VEC(tree,gc) *args = make_tree_vector_single (expr);
7834 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7835 &args, type, LOOKUP_NORMAL, complain);
7836 release_tree_vector (args);
7837 return build_cplus_new (type, expr);
7840 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7841 p = conversion_obstack_alloc (0);
7843 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
7846 if (!conv || conv->bad_p)
7849 expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
7850 /*issue_conversion_warnings=*/false,
7854 /* Free all the conversions we allocated. */
7855 obstack_free (&conversion_obstack, p);
7860 /* DECL is a VAR_DECL whose type is a REFERENCE_TYPE. The reference
7861 is being bound to a temporary. Create and return a new VAR_DECL
7862 with the indicated TYPE; this variable will store the value to
7863 which the reference is bound. */
7866 make_temporary_var_for_ref_to_temp (tree decl, tree type)
7870 /* Create the variable. */
7871 var = create_temporary_var (type);
7873 /* Register the variable. */
7874 if (TREE_STATIC (decl))
7876 /* Namespace-scope or local static; give it a mangled name. */
7879 TREE_STATIC (var) = 1;
7880 name = mangle_ref_init_variable (decl);
7881 DECL_NAME (var) = name;
7882 SET_DECL_ASSEMBLER_NAME (var, name);
7883 var = pushdecl_top_level (var);
7886 /* Create a new cleanup level if necessary. */
7887 maybe_push_cleanup_level (type);
7892 /* EXPR is the initializer for a variable DECL of reference or
7893 std::initializer_list type. Create, push and return a new VAR_DECL
7894 for the initializer so that it will live as long as DECL. Any
7895 cleanup for the new variable is returned through CLEANUP, and the
7896 code to initialize the new variable is returned through INITP. */
7899 set_up_extended_ref_temp (tree decl, tree expr, tree *cleanup, tree *initp)
7905 /* Create the temporary variable. */
7906 type = TREE_TYPE (expr);
7907 var = make_temporary_var_for_ref_to_temp (decl, type);
7908 layout_decl (var, 0);
7909 /* If the rvalue is the result of a function call it will be
7910 a TARGET_EXPR. If it is some other construct (such as a
7911 member access expression where the underlying object is
7912 itself the result of a function call), turn it into a
7913 TARGET_EXPR here. It is important that EXPR be a
7914 TARGET_EXPR below since otherwise the INIT_EXPR will
7915 attempt to make a bitwise copy of EXPR to initialize
7917 if (TREE_CODE (expr) != TARGET_EXPR)
7918 expr = get_target_expr (expr);
7920 /* If the initializer is constant, put it in DECL_INITIAL so we get
7921 static initialization and use in constant expressions. */
7922 init = maybe_constant_init (expr);
7923 if (TREE_CONSTANT (init))
7925 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
7927 /* 5.19 says that a constant expression can include an
7928 lvalue-rvalue conversion applied to "a glvalue of literal type
7929 that refers to a non-volatile temporary object initialized
7930 with a constant expression". Rather than try to communicate
7931 that this VAR_DECL is a temporary, just mark it constexpr.
7933 Currently this is only useful for initializer_list temporaries,
7934 since reference vars can't appear in constant expressions. */
7935 DECL_DECLARED_CONSTEXPR_P (var) = true;
7936 TREE_CONSTANT (var) = true;
7938 DECL_INITIAL (var) = init;
7942 /* Create the INIT_EXPR that will initialize the temporary
7944 init = build2 (INIT_EXPR, type, var, expr);
7945 if (at_function_scope_p ())
7947 add_decl_expr (var);
7949 if (TREE_STATIC (var))
7950 init = add_stmt_to_compound (init, register_dtor_fn (var));
7952 *cleanup = cxx_maybe_build_cleanup (var);
7954 /* We must be careful to destroy the temporary only
7955 after its initialization has taken place. If the
7956 initialization throws an exception, then the
7957 destructor should not be run. We cannot simply
7958 transform INIT into something like:
7960 (INIT, ({ CLEANUP_STMT; }))
7962 because emit_local_var always treats the
7963 initializer as a full-expression. Thus, the
7964 destructor would run too early; it would run at the
7965 end of initializing the reference variable, rather
7966 than at the end of the block enclosing the
7969 The solution is to pass back a cleanup expression
7970 which the caller is responsible for attaching to
7971 the statement tree. */
7975 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
7976 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7977 static_aggregates = tree_cons (NULL_TREE, var,
7985 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
7986 initializing a variable of that TYPE. If DECL is non-NULL, it is
7987 the VAR_DECL being initialized with the EXPR. (In that case, the
7988 type of DECL will be TYPE.) If DECL is non-NULL, then CLEANUP must
7989 also be non-NULL, and with *CLEANUP initialized to NULL. Upon
7990 return, if *CLEANUP is no longer NULL, it will be an expression
7991 that should be pushed as a cleanup after the returned expression
7992 is used to initialize DECL.
7994 Return the converted expression. */
7997 initialize_reference (tree type, tree expr, tree decl, tree *cleanup,
7998 tsubst_flags_t complain)
8003 if (type == error_mark_node || error_operand_p (expr))
8004 return error_mark_node;
8006 /* Get the high-water mark for the CONVERSION_OBSTACK. */
8007 p = conversion_obstack_alloc (0);
8009 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
8011 if (!conv || conv->bad_p)
8013 if (complain & tf_error)
8015 if (!CP_TYPE_CONST_P (TREE_TYPE (type))
8016 && !TYPE_REF_IS_RVALUE (type)
8017 && !real_lvalue_p (expr))
8018 error ("invalid initialization of non-const reference of "
8019 "type %qT from an rvalue of type %qT",
8020 type, TREE_TYPE (expr));
8022 error ("invalid initialization of reference of type "
8023 "%qT from expression of type %qT", type,
8026 return error_mark_node;
8029 /* If DECL is non-NULL, then this special rule applies:
8033 The temporary to which the reference is bound or the temporary
8034 that is the complete object to which the reference is bound
8035 persists for the lifetime of the reference.
8037 The temporaries created during the evaluation of the expression
8038 initializing the reference, except the temporary to which the
8039 reference is bound, are destroyed at the end of the
8040 full-expression in which they are created.
8042 In that case, we store the converted expression into a new
8043 VAR_DECL in a new scope.
8045 However, we want to be careful not to create temporaries when
8046 they are not required. For example, given:
8049 struct D : public B {};
8053 there is no need to copy the return value from "f"; we can just
8054 extend its lifetime. Similarly, given:
8057 struct T { operator S(); };
8061 we can extend the lifetime of the return value of the conversion
8063 gcc_assert (conv->kind == ck_ref_bind);
8067 tree base_conv_type;
8069 /* Skip over the REF_BIND. */
8070 conv = conv->u.next;
8071 /* If the next conversion is a BASE_CONV, skip that too -- but
8072 remember that the conversion was required. */
8073 if (conv->kind == ck_base)
8075 base_conv_type = conv->type;
8076 conv = conv->u.next;
8079 base_conv_type = NULL_TREE;
8080 /* Perform the remainder of the conversion. */
8081 expr = convert_like_real (conv, expr,
8082 /*fn=*/NULL_TREE, /*argnum=*/0,
8084 /*issue_conversion_warnings=*/true,
8086 tf_warning_or_error);
8087 if (error_operand_p (expr))
8088 expr = error_mark_node;
8091 if (!lvalue_or_rvalue_with_address_p (expr))
8094 var = set_up_extended_ref_temp (decl, expr, cleanup, &init);
8095 /* Use its address to initialize the reference variable. */
8096 expr = build_address (var);
8098 expr = convert_to_base (expr,
8099 build_pointer_type (base_conv_type),
8100 /*check_access=*/true,
8101 /*nonnull=*/true, complain);
8103 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
8106 /* Take the address of EXPR. */
8107 expr = cp_build_addr_expr (expr, tf_warning_or_error);
8108 /* If a BASE_CONV was required, perform it now. */
8110 expr = (perform_implicit_conversion
8111 (build_pointer_type (base_conv_type), expr,
8112 tf_warning_or_error));
8113 expr = build_nop (type, expr);
8117 /* Perform the conversion. */
8118 expr = convert_like (conv, expr, tf_warning_or_error);
8120 /* Free all the conversions we allocated. */
8121 obstack_free (&conversion_obstack, p);
8126 /* Returns true iff TYPE is some variant of std::initializer_list. */
8129 is_std_init_list (tree type)
8131 /* Look through typedefs. */
8134 type = TYPE_MAIN_VARIANT (type);
8135 return (CLASS_TYPE_P (type)
8136 && CP_TYPE_CONTEXT (type) == std_node
8137 && strcmp (TYPE_NAME_STRING (type), "initializer_list") == 0);
8140 /* Returns true iff DECL is a list constructor: i.e. a constructor which
8141 will accept an argument list of a single std::initializer_list<T>. */
8144 is_list_ctor (tree decl)
8146 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
8149 if (!args || args == void_list_node)
8152 arg = non_reference (TREE_VALUE (args));
8153 if (!is_std_init_list (arg))
8156 args = TREE_CHAIN (args);
8158 if (args && args != void_list_node && !TREE_PURPOSE (args))
8159 /* There are more non-defaulted parms. */
8165 #include "gt-cp-call.h"