1 /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 Copyright (C) 1987-2023 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 modified by Brendan Kehoe (brendan@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* High-level class interface. */
27 #include "coretypes.h"
31 #include "stringpool.h"
33 #include "stor-layout.h"
34 #include "trans-mem.h"
39 #include "langhooks.h"
40 #include "c-family/c-objc.h"
41 #include "internal-fn.h"
42 #include "stringpool.h"
45 #include "gcc-rich-location.h"
47 /* The various kinds of conversion. */
49 enum conversion_kind
{
64 /* When LOOKUP_SHORTCUT_BAD_CONVS is set, we may return a conversion of
65 this kind whenever we know the true conversion is either bad or outright
66 invalid, but we don't want to attempt to compute the bad conversion (for
67 sake of avoiding unnecessary instantiation). bad_p should always be set
72 /* The rank of the conversion. Order of the enumerals matters; better
73 conversions should come earlier in the list. */
75 enum conversion_rank
{
86 /* An implicit conversion sequence, in the sense of [over.best.ics].
87 The first conversion to be performed is at the end of the chain.
88 That conversion is always a cr_identity conversion. */
91 /* The kind of conversion represented by this step. */
93 /* The rank of this conversion. */
95 BOOL_BITFIELD user_conv_p
: 1;
96 BOOL_BITFIELD ellipsis_p
: 1;
97 BOOL_BITFIELD this_p
: 1;
98 /* True if this conversion would be permitted with a bending of
99 language standards, e.g. disregarding pointer qualifiers or
100 converting integers to pointers. */
101 BOOL_BITFIELD bad_p
: 1;
102 /* If KIND is ck_ref_bind or ck_base, true to indicate that a
103 temporary should be created to hold the result of the
104 conversion. If KIND is ck_ambig or ck_user, true means force
105 copy-initialization. */
106 BOOL_BITFIELD need_temporary_p
: 1;
107 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
108 from a pointer-to-derived to pointer-to-base is being performed. */
109 BOOL_BITFIELD base_p
: 1;
110 /* If KIND is ck_ref_bind, true when either an lvalue reference is
111 being bound to an lvalue expression or an rvalue reference is
112 being bound to an rvalue expression. If KIND is ck_rvalue or ck_base,
113 true when we are treating an lvalue as an rvalue (12.8p33). If
114 ck_identity, we will be binding a reference directly or decaying to
116 BOOL_BITFIELD rvaluedness_matches_p
: 1;
117 BOOL_BITFIELD check_narrowing
: 1;
118 /* Whether check_narrowing should only check TREE_CONSTANTs; used
119 in build_converted_constant_expr. */
120 BOOL_BITFIELD check_narrowing_const_only
: 1;
121 /* True if this conversion is taking place in a copy-initialization context
122 and we should only consider converting constructors. Only set in
123 ck_base and ck_rvalue. */
124 BOOL_BITFIELD copy_init_p
: 1;
125 /* The type of the expression resulting from the conversion. */
128 /* The next conversion in the chain. Since the conversions are
129 arranged from outermost to innermost, the NEXT conversion will
130 actually be performed before this conversion. This variant is
131 used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor
132 ck_list. Please use the next_conversion function instead
133 of using this field directly. */
135 /* The expression at the beginning of the conversion chain. This
136 variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig.
137 You can use conv_get_original_expr to get this expression. */
139 /* The array of conversions for an initializer_list, so this
140 variant is used only when KIN D is ck_list. */
143 /* The function candidate corresponding to this conversion
144 sequence. This field is only used if KIND is ck_user. */
145 struct z_candidate
*cand
;
148 #define CONVERSION_RANK(NODE) \
149 ((NODE)->bad_p ? cr_bad \
150 : (NODE)->ellipsis_p ? cr_ellipsis \
151 : (NODE)->user_conv_p ? cr_user \
154 #define BAD_CONVERSION_RANK(NODE) \
155 ((NODE)->ellipsis_p ? cr_ellipsis \
156 : (NODE)->user_conv_p ? cr_user \
159 static struct obstack conversion_obstack
;
160 static bool conversion_obstack_initialized
;
161 struct rejection_reason
;
163 static struct z_candidate
* tourney (struct z_candidate
*, tsubst_flags_t
);
164 static int equal_functions (tree
, tree
);
165 static int joust (struct z_candidate
*, struct z_candidate
*, bool,
167 static int compare_ics (conversion
*, conversion
*);
168 static void maybe_warn_class_memaccess (location_t
, tree
,
169 const vec
<tree
, va_gc
> *);
170 static tree
build_over_call (struct z_candidate
*, int, tsubst_flags_t
);
171 static tree
convert_like (conversion
*, tree
, tsubst_flags_t
);
172 static tree
convert_like_with_context (conversion
*, tree
, tree
, int,
174 static void op_error (const op_location_t
&, enum tree_code
, enum tree_code
,
175 tree
, tree
, tree
, bool);
176 static struct z_candidate
*build_user_type_conversion_1 (tree
, tree
, int,
178 static void print_z_candidate (location_t
, const char *, struct z_candidate
*);
179 static void print_z_candidates (location_t
, struct z_candidate
*);
180 static tree
build_this (tree
);
181 static struct z_candidate
*splice_viable (struct z_candidate
*, bool, bool *);
182 static bool any_strictly_viable (struct z_candidate
*);
183 static struct z_candidate
*add_template_candidate
184 (struct z_candidate
**, tree
, tree
, tree
, tree
, const vec
<tree
, va_gc
> *,
185 tree
, tree
, tree
, int, unification_kind_t
, bool, tsubst_flags_t
);
186 static struct z_candidate
*add_template_candidate_real
187 (struct z_candidate
**, tree
, tree
, tree
, tree
, const vec
<tree
, va_gc
> *,
188 tree
, tree
, tree
, int, tree
, unification_kind_t
, bool, tsubst_flags_t
);
189 static bool is_complete (tree
);
190 static struct z_candidate
*add_conv_candidate
191 (struct z_candidate
**, tree
, tree
, const vec
<tree
, va_gc
> *, tree
,
192 tree
, tsubst_flags_t
);
193 static struct z_candidate
*add_function_candidate
194 (struct z_candidate
**, tree
, tree
, tree
, const vec
<tree
, va_gc
> *, tree
,
195 tree
, int, conversion
**, bool, tsubst_flags_t
);
196 static conversion
*implicit_conversion (tree
, tree
, tree
, bool, int,
198 static conversion
*reference_binding (tree
, tree
, tree
, bool, int,
200 static conversion
*build_conv (conversion_kind
, tree
, conversion
*);
201 static conversion
*build_list_conv (tree
, tree
, int, tsubst_flags_t
);
202 static conversion
*next_conversion (conversion
*);
203 static bool is_subseq (conversion
*, conversion
*);
204 static conversion
*maybe_handle_ref_bind (conversion
**);
205 static void maybe_handle_implicit_object (conversion
**);
206 static struct z_candidate
*add_candidate
207 (struct z_candidate
**, tree
, tree
, const vec
<tree
, va_gc
> *, size_t,
208 conversion
**, tree
, tree
, int, struct rejection_reason
*, int);
209 static tree
source_type (conversion
*);
210 static void add_warning (struct z_candidate
*, struct z_candidate
*);
211 static conversion
*direct_reference_binding (tree
, conversion
*);
212 static bool promoted_arithmetic_type_p (tree
);
213 static conversion
*conditional_conversion (tree
, tree
, tsubst_flags_t
);
214 static char *name_as_c_string (tree
, tree
, bool *);
215 static tree
prep_operand (tree
);
216 static void add_candidates (tree
, tree
, const vec
<tree
, va_gc
> *, tree
, tree
,
217 bool, tree
, tree
, int, struct z_candidate
**,
219 static conversion
*merge_conversion_sequences (conversion
*, conversion
*);
220 static tree
build_temp (tree
, tree
, int, diagnostic_t
*, tsubst_flags_t
);
221 static conversion
*build_identity_conv (tree
, tree
);
222 static inline bool conv_binds_to_array_of_unknown_bound (conversion
*);
223 static bool conv_is_prvalue (conversion
*);
224 static tree
prevent_lifetime_extension (tree
);
226 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
227 NAME can take many forms... */
230 check_dtor_name (tree basetype
, tree name
)
232 /* Just accept something we've already complained about. */
233 if (name
== error_mark_node
)
236 if (TREE_CODE (name
) == TYPE_DECL
)
237 name
= TREE_TYPE (name
);
238 else if (TYPE_P (name
))
240 else if (identifier_p (name
))
242 if ((MAYBE_CLASS_TYPE_P (basetype
)
243 || TREE_CODE (basetype
) == ENUMERAL_TYPE
)
244 && name
== constructor_name (basetype
))
247 /* Otherwise lookup the name, it could be an unrelated typedef
248 of the correct type. */
249 name
= lookup_name (name
, LOOK_want::TYPE
);
252 name
= TREE_TYPE (name
);
253 if (name
== error_mark_node
)
260 template <class T> struct S { ~S(); };
264 NAME will be a class template. */
265 gcc_assert (DECL_CLASS_TEMPLATE_P (name
));
269 return same_type_p (TYPE_MAIN_VARIANT (basetype
), TYPE_MAIN_VARIANT (name
));
272 /* We want the address of a function or method. We avoid creating a
273 pointer-to-member function. */
276 build_addr_func (tree function
, tsubst_flags_t complain
)
278 tree type
= TREE_TYPE (function
);
280 /* We have to do these by hand to avoid real pointer to member
282 if (TREE_CODE (type
) == METHOD_TYPE
)
284 if (TREE_CODE (function
) == OFFSET_REF
)
286 tree object
= build_address (TREE_OPERAND (function
, 0));
287 return get_member_function_from_ptrfunc (&object
,
288 TREE_OPERAND (function
, 1),
291 function
= build_address (function
);
293 else if (TREE_CODE (function
) == FUNCTION_DECL
294 && DECL_IMMEDIATE_FUNCTION_P (function
))
295 function
= build_address (function
);
297 function
= decay_conversion (function
, complain
, /*reject_builtin=*/false);
302 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
303 POINTER_TYPE to those. Note, pointer to member function types
304 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
305 two variants. build_call_a is the primitive taking an array of
306 arguments, while build_call_n is a wrapper that handles varargs. */
309 build_call_n (tree function
, int n
, ...)
312 return build_call_a (function
, 0, NULL
);
315 tree
*argarray
= XALLOCAVEC (tree
, n
);
320 for (i
= 0; i
< n
; i
++)
321 argarray
[i
] = va_arg (ap
, tree
);
323 return build_call_a (function
, n
, argarray
);
327 /* Update various flags in cfun and the call itself based on what is being
328 called. Split out of build_call_a so that bot_manip can use it too. */
331 set_flags_from_callee (tree call
)
333 /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
334 tree decl
= cp_get_callee_fndecl_nofold (call
);
336 /* We check both the decl and the type; a function may be known not to
337 throw without being declared throw(). */
338 bool nothrow
= decl
&& TREE_NOTHROW (decl
);
339 tree callee
= cp_get_callee (call
);
341 nothrow
|= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee
)));
342 else if (TREE_CODE (call
) == CALL_EXPR
343 && internal_fn_flags (CALL_EXPR_IFN (call
)) & ECF_NOTHROW
)
346 if (cfun
&& cp_function_chain
&& !cp_unevaluated_operand
)
348 if (!nothrow
&& at_function_scope_p ())
349 cp_function_chain
->can_throw
= 1;
351 if (decl
&& TREE_THIS_VOLATILE (decl
))
352 current_function_returns_abnormally
= 1;
355 TREE_NOTHROW (call
) = nothrow
;
359 build_call_a (tree function
, int n
, tree
*argarray
)
366 function
= build_addr_func (function
, tf_warning_or_error
);
368 gcc_assert (TYPE_PTR_P (TREE_TYPE (function
)));
369 fntype
= TREE_TYPE (TREE_TYPE (function
));
370 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype
));
371 result_type
= TREE_TYPE (fntype
);
372 /* An rvalue has no cv-qualifiers. */
373 if (SCALAR_TYPE_P (result_type
) || VOID_TYPE_P (result_type
))
374 result_type
= cv_unqualified (result_type
);
376 function
= build_call_array_loc (input_location
,
377 result_type
, function
, n
, argarray
);
378 set_flags_from_callee (function
);
380 decl
= get_callee_fndecl (function
);
382 if (decl
&& !TREE_USED (decl
))
384 /* We invoke build_call directly for several library
385 functions. These may have been declared normally if
386 we're building libgcc, so we can't just check
388 gcc_assert (DECL_ARTIFICIAL (decl
)
389 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl
)),
394 require_complete_eh_spec_types (fntype
, decl
);
396 TREE_HAS_CONSTRUCTOR (function
) = (decl
&& DECL_CONSTRUCTOR_P (decl
));
398 /* Don't pass empty class objects by value. This is useful
399 for tags in STL, which are used to control overload resolution.
400 We don't need to handle other cases of copying empty classes. */
401 if (!decl
|| !fndecl_built_in_p (decl
))
402 for (i
= 0; i
< n
; i
++)
404 tree arg
= CALL_EXPR_ARG (function
, i
);
405 if (is_empty_class (TREE_TYPE (arg
))
406 && simple_empty_class_p (TREE_TYPE (arg
), arg
, INIT_EXPR
))
408 while (TREE_CODE (arg
) == TARGET_EXPR
)
409 /* We're disconnecting the initializer from its target,
410 don't create a temporary. */
411 arg
= TARGET_EXPR_INITIAL (arg
);
412 tree t
= build0 (EMPTY_CLASS_EXPR
, TREE_TYPE (arg
));
413 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (t
), arg
, t
);
414 CALL_EXPR_ARG (function
, i
) = arg
;
421 /* New overloading code. */
425 struct candidate_warning
{
427 candidate_warning
*next
;
430 /* Information for providing diagnostics about why overloading failed. */
432 enum rejection_reason_code
{
435 rr_explicit_conversion
,
436 rr_template_conversion
,
438 rr_bad_arg_conversion
,
439 rr_template_unification
,
442 rr_constraint_failure
445 struct conversion_info
{
446 /* The index of the argument, 0-based. */
448 /* The actual argument or its type. */
450 /* The type of the parameter. */
452 /* The location of the argument. */
456 struct rejection_reason
{
457 enum rejection_reason_code code
;
459 /* Information about an arity mismatch. */
461 /* The expected number of arguments. */
463 /* The actual number of arguments in the call. */
465 /* Whether EXPECTED should be treated as a lower bound. */
468 /* Information about an argument conversion mismatch. */
469 struct conversion_info conversion
;
470 /* Same, but for bad argument conversions. */
471 struct conversion_info bad_conversion
;
472 /* Information about template unification failures. These are the
473 parameters passed to fn_type_unification. */
481 unification_kind_t strict
;
483 } template_unification
;
484 /* Information about template instantiation failures. These are the
485 parameters passed to instantiate_template. */
489 } template_instantiation
;
494 /* The FUNCTION_DECL that will be called if this candidate is
495 selected by overload resolution. */
497 /* If not NULL_TREE, the first argument to use when calling this
500 /* The rest of the arguments to use when calling this function. If
501 there are no further arguments this may be NULL or it may be an
503 const vec
<tree
, va_gc
> *args
;
504 /* The implicit conversion sequences for each of the arguments to
507 /* The number of implicit conversion sequences. */
509 /* If FN is a user-defined conversion, the standard conversion
510 sequence from the type returned by FN to the desired destination
512 conversion
*second_conv
;
513 struct rejection_reason
*reason
;
514 /* If FN is a member function, the binfo indicating the path used to
515 qualify the name of FN at the call site. This path is used to
516 determine whether or not FN is accessible if it is selected by
517 overload resolution. The DECL_CONTEXT of FN will always be a
518 (possibly improper) base of this binfo. */
520 /* If FN is a non-static member function, the binfo indicating the
521 subobject to which the `this' pointer should be converted if FN
522 is selected by overload resolution. The type pointed to by
523 the `this' pointer must correspond to the most derived class
524 indicated by the CONVERSION_PATH. */
525 tree conversion_path
;
528 candidate_warning
*warnings
;
532 /* The flags active in add_candidate. */
535 bool rewritten () const { return (flags
& LOOKUP_REWRITTEN
); }
536 bool reversed () const { return (flags
& LOOKUP_REVERSED
); }
539 /* Returns true iff T is a null pointer constant in the sense of
543 null_ptr_cst_p (tree t
)
545 tree type
= TREE_TYPE (t
);
549 A null pointer constant is an integer literal ([lex.icon]) with value
550 zero or a prvalue of type std::nullptr_t. */
551 if (NULLPTR_TYPE_P (type
))
554 if (cxx_dialect
>= cxx11
)
556 STRIP_ANY_LOCATION_WRAPPER (t
);
558 /* Core issue 903 says only literal 0 is a null pointer constant. */
559 if (TREE_CODE (t
) == INTEGER_CST
560 && !TREE_OVERFLOW (t
)
561 && TREE_CODE (type
) == INTEGER_TYPE
563 && !char_type_p (type
))
566 else if (CP_INTEGRAL_TYPE_P (type
))
568 t
= fold_non_dependent_expr (t
, tf_none
);
570 if (integer_zerop (t
) && !TREE_OVERFLOW (t
))
577 /* Returns true iff T is a null member pointer value (4.11). */
580 null_member_pointer_value_p (tree t
)
582 tree type
= TREE_TYPE (t
);
585 else if (TYPE_PTRMEMFUNC_P (type
))
586 return (TREE_CODE (t
) == CONSTRUCTOR
587 && CONSTRUCTOR_NELTS (t
)
588 && integer_zerop (CONSTRUCTOR_ELT (t
, 0)->value
));
589 else if (TYPE_PTRDATAMEM_P (type
))
590 return integer_all_onesp (t
);
595 /* Returns nonzero if PARMLIST consists of only default parms,
596 ellipsis, and/or undeduced parameter packs. */
599 sufficient_parms_p (const_tree parmlist
)
601 for (; parmlist
&& parmlist
!= void_list_node
;
602 parmlist
= TREE_CHAIN (parmlist
))
603 if (!TREE_PURPOSE (parmlist
)
604 && !PACK_EXPANSION_P (TREE_VALUE (parmlist
)))
609 /* Allocate N bytes of memory from the conversion obstack. The memory
610 is zeroed before being returned. */
613 conversion_obstack_alloc (size_t n
)
616 if (!conversion_obstack_initialized
)
618 gcc_obstack_init (&conversion_obstack
);
619 conversion_obstack_initialized
= true;
621 p
= obstack_alloc (&conversion_obstack
, n
);
626 /* RAII class to discard anything added to conversion_obstack. */
628 struct conversion_obstack_sentinel
631 conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {}
632 ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack
, p
); }
635 /* Allocate rejection reasons. */
637 static struct rejection_reason
*
638 alloc_rejection (enum rejection_reason_code code
)
640 struct rejection_reason
*p
;
641 p
= (struct rejection_reason
*) conversion_obstack_alloc (sizeof *p
);
646 static struct rejection_reason
*
647 arity_rejection (tree first_arg
, int expected
, int actual
, bool least_p
= false)
649 struct rejection_reason
*r
= alloc_rejection (rr_arity
);
650 int adjust
= first_arg
!= NULL_TREE
;
651 r
->u
.arity
.expected
= expected
- adjust
;
652 r
->u
.arity
.actual
= actual
- adjust
;
653 r
->u
.arity
.least_p
= least_p
;
657 static struct rejection_reason
*
658 arg_conversion_rejection (tree first_arg
, int n_arg
, tree from
, tree to
,
661 struct rejection_reason
*r
= alloc_rejection (rr_arg_conversion
);
662 int adjust
= first_arg
!= NULL_TREE
;
663 r
->u
.conversion
.n_arg
= n_arg
- adjust
;
664 r
->u
.conversion
.from
= from
;
665 r
->u
.conversion
.to_type
= to
;
666 r
->u
.conversion
.loc
= loc
;
670 static struct rejection_reason
*
671 bad_arg_conversion_rejection (tree first_arg
, int n_arg
, tree from
, tree to
,
674 struct rejection_reason
*r
= alloc_rejection (rr_bad_arg_conversion
);
675 int adjust
= first_arg
!= NULL_TREE
;
676 r
->u
.bad_conversion
.n_arg
= n_arg
- adjust
;
677 r
->u
.bad_conversion
.from
= from
;
678 r
->u
.bad_conversion
.to_type
= to
;
679 r
->u
.bad_conversion
.loc
= loc
;
683 static struct rejection_reason
*
684 explicit_conversion_rejection (tree from
, tree to
)
686 struct rejection_reason
*r
= alloc_rejection (rr_explicit_conversion
);
687 r
->u
.conversion
.n_arg
= 0;
688 r
->u
.conversion
.from
= from
;
689 r
->u
.conversion
.to_type
= to
;
690 r
->u
.conversion
.loc
= UNKNOWN_LOCATION
;
694 static struct rejection_reason
*
695 template_conversion_rejection (tree from
, tree to
)
697 struct rejection_reason
*r
= alloc_rejection (rr_template_conversion
);
698 r
->u
.conversion
.n_arg
= 0;
699 r
->u
.conversion
.from
= from
;
700 r
->u
.conversion
.to_type
= to
;
701 r
->u
.conversion
.loc
= UNKNOWN_LOCATION
;
705 static struct rejection_reason
*
706 template_unification_rejection (tree tmpl
, tree explicit_targs
, tree targs
,
707 const tree
*args
, unsigned int nargs
,
708 tree return_type
, unification_kind_t strict
,
711 size_t args_n_bytes
= sizeof (*args
) * nargs
;
712 tree
*args1
= (tree
*) conversion_obstack_alloc (args_n_bytes
);
713 struct rejection_reason
*r
= alloc_rejection (rr_template_unification
);
714 r
->u
.template_unification
.tmpl
= tmpl
;
715 r
->u
.template_unification
.explicit_targs
= explicit_targs
;
716 r
->u
.template_unification
.num_targs
= TREE_VEC_LENGTH (targs
);
717 /* Copy args to our own storage. */
718 memcpy (args1
, args
, args_n_bytes
);
719 r
->u
.template_unification
.args
= args1
;
720 r
->u
.template_unification
.nargs
= nargs
;
721 r
->u
.template_unification
.return_type
= return_type
;
722 r
->u
.template_unification
.strict
= strict
;
723 r
->u
.template_unification
.flags
= flags
;
727 static struct rejection_reason
*
728 template_unification_error_rejection (void)
730 return alloc_rejection (rr_template_unification
);
733 static struct rejection_reason
*
734 invalid_copy_with_fn_template_rejection (void)
736 struct rejection_reason
*r
= alloc_rejection (rr_invalid_copy
);
740 static struct rejection_reason
*
741 inherited_ctor_rejection (void)
743 struct rejection_reason
*r
= alloc_rejection (rr_inherited_ctor
);
747 /* Build a constraint failure record. */
749 static struct rejection_reason
*
750 constraint_failure (void)
752 struct rejection_reason
*r
= alloc_rejection (rr_constraint_failure
);
756 /* Dynamically allocate a conversion. */
759 alloc_conversion (conversion_kind kind
)
762 c
= (conversion
*) conversion_obstack_alloc (sizeof (conversion
));
767 /* Make sure that all memory on the conversion obstack has been
771 validate_conversion_obstack (void)
773 if (conversion_obstack_initialized
)
774 gcc_assert ((obstack_next_free (&conversion_obstack
)
775 == obstack_base (&conversion_obstack
)));
778 /* Dynamically allocate an array of N conversions. */
781 alloc_conversions (size_t n
)
783 return (conversion
**) conversion_obstack_alloc (n
* sizeof (conversion
*));
786 /* True iff the active member of conversion::u for code CODE is NEXT. */
789 has_next (conversion_kind code
)
791 return !(code
== ck_identity
795 || code
== ck_deferred_bad
);
799 build_conv (conversion_kind code
, tree type
, conversion
*from
)
802 conversion_rank rank
= CONVERSION_RANK (from
);
804 /* Only call this function for conversions that use u.next. */
805 gcc_assert (from
== NULL
|| has_next (code
));
807 /* Note that the caller is responsible for filling in t->cand for
808 user-defined conversions. */
809 t
= alloc_conversion (code
);
833 t
->user_conv_p
= (code
== ck_user
|| from
->user_conv_p
);
834 t
->bad_p
= from
->bad_p
;
839 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
840 specialization of std::initializer_list<T>, if such a conversion is
844 build_list_conv (tree type
, tree ctor
, int flags
, tsubst_flags_t complain
)
846 tree elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type
), 0);
847 unsigned len
= CONSTRUCTOR_NELTS (ctor
);
848 conversion
**subconvs
= alloc_conversions (len
);
853 /* Within a list-initialization we can have more user-defined
855 flags
&= ~LOOKUP_NO_CONVERSION
;
856 /* But no narrowing conversions. */
857 flags
|= LOOKUP_NO_NARROWING
;
859 /* Can't make an array of these types. */
860 if (TYPE_REF_P (elttype
)
861 || TREE_CODE (elttype
) == FUNCTION_TYPE
862 || VOID_TYPE_P (elttype
))
865 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), i
, val
)
868 = implicit_conversion (elttype
, TREE_TYPE (val
), val
,
869 false, flags
, complain
);
876 t
= alloc_conversion (ck_list
);
878 t
->u
.list
= subconvs
;
881 for (i
= 0; i
< len
; ++i
)
883 conversion
*sub
= subconvs
[i
];
884 if (sub
->rank
> t
->rank
)
886 if (sub
->user_conv_p
)
887 t
->user_conv_p
= true;
895 /* Return the next conversion of the conversion chain (if applicable),
896 or NULL otherwise. Please use this function instead of directly
897 accessing fields of struct conversion. */
900 next_conversion (conversion
*conv
)
903 || !has_next (conv
->kind
))
908 /* Strip to the first ck_user, ck_ambig, ck_list, ck_aggr or ck_identity
912 strip_standard_conversion (conversion
*conv
)
915 && conv
->kind
!= ck_user
916 && has_next (conv
->kind
))
917 conv
= next_conversion (conv
);
921 /* Subroutine of build_aggr_conv: check whether FROM is a valid aggregate
922 initializer for array type ATYPE. */
925 can_convert_array (tree atype
, tree from
, int flags
, tsubst_flags_t complain
)
927 tree elttype
= TREE_TYPE (atype
);
930 if (TREE_CODE (from
) == CONSTRUCTOR
)
932 for (i
= 0; i
< CONSTRUCTOR_NELTS (from
); ++i
)
934 tree val
= CONSTRUCTOR_ELT (from
, i
)->value
;
936 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
937 ok
= can_convert_array (elttype
, val
, flags
, complain
);
939 ok
= can_convert_arg (elttype
, TREE_TYPE (val
), val
, flags
,
947 if (char_type_p (TYPE_MAIN_VARIANT (elttype
))
948 && TREE_CODE (tree_strip_any_location_wrapper (from
)) == STRING_CST
)
949 return array_string_literal_compatible_p (atype
, from
);
951 /* No other valid way to aggregate initialize an array. */
955 /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if
956 FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively
960 field_in_pset (hash_set
<tree
, true> &pset
, tree field
)
962 if (pset
.contains (field
))
964 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
965 for (field
= TYPE_FIELDS (TREE_TYPE (field
));
966 field
; field
= DECL_CHAIN (field
))
968 field
= next_aggregate_field (field
);
969 if (field
== NULL_TREE
)
971 if (field_in_pset (pset
, field
))
977 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
978 aggregate class, if such a conversion is possible. */
981 build_aggr_conv (tree type
, tree ctor
, int flags
, tsubst_flags_t complain
)
983 unsigned HOST_WIDE_INT i
= 0;
985 tree field
= next_aggregate_field (TYPE_FIELDS (type
));
986 tree empty_ctor
= NULL_TREE
;
987 hash_set
<tree
, true> pset
;
989 /* We already called reshape_init in implicit_conversion, but it might not
990 have done anything in the case of parenthesized aggr init. */
992 /* The conversions within the init-list aren't affected by the enclosing
993 context; they're always simple copy-initialization. */
994 flags
= LOOKUP_IMPLICIT
|LOOKUP_NO_NARROWING
;
996 /* For designated initializers, verify that each initializer is convertible
997 to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
998 visited. In the following loop then ignore already visited
1001 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), i
, idx
, val
)
1006 gcc_checking_assert (TREE_CODE (idx
) == FIELD_DECL
);
1008 tree ftype
= TREE_TYPE (idx
);
1011 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
1012 ok
= can_convert_array (ftype
, val
, flags
, complain
);
1014 ok
= can_convert_arg (ftype
, TREE_TYPE (val
), val
, flags
,
1020 /* For unions, there should be just one initializer. */
1021 if (TREE_CODE (type
) == UNION_TYPE
)
1030 for (; field
; field
= next_aggregate_field (DECL_CHAIN (field
)))
1032 tree ftype
= TREE_TYPE (field
);
1035 if (!pset
.is_empty () && field_in_pset (pset
, field
))
1037 if (i
< CONSTRUCTOR_NELTS (ctor
))
1039 constructor_elt
*ce
= CONSTRUCTOR_ELT (ctor
, i
);
1040 gcc_checking_assert (!ce
->index
);
1044 else if (DECL_INITIAL (field
))
1045 val
= get_nsdmi (field
, /*ctor*/false, complain
);
1046 else if (TYPE_REF_P (ftype
))
1047 /* Value-initialization of reference is ill-formed. */
1051 if (empty_ctor
== NULL_TREE
)
1052 empty_ctor
= build_constructor (init_list_type_node
, NULL
);
1056 if (TREE_CODE (ftype
) == ARRAY_TYPE
)
1057 ok
= can_convert_array (ftype
, val
, flags
, complain
);
1059 ok
= can_convert_arg (ftype
, TREE_TYPE (val
), val
, flags
,
1065 if (TREE_CODE (type
) == UNION_TYPE
)
1069 if (i
< CONSTRUCTOR_NELTS (ctor
))
1072 c
= alloc_conversion (ck_aggr
);
1075 c
->user_conv_p
= true;
1076 c
->check_narrowing
= true;
1081 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1082 array type, if such a conversion is possible. */
1085 build_array_conv (tree type
, tree ctor
, int flags
, tsubst_flags_t complain
)
1088 unsigned HOST_WIDE_INT len
= CONSTRUCTOR_NELTS (ctor
);
1089 tree elttype
= TREE_TYPE (type
);
1092 enum conversion_rank rank
= cr_exact
;
1094 /* We might need to propagate the size from the element to the array. */
1095 complete_type (type
);
1097 if (TYPE_DOMAIN (type
)
1098 && !variably_modified_type_p (TYPE_DOMAIN (type
), NULL_TREE
))
1100 unsigned HOST_WIDE_INT alen
= tree_to_uhwi (array_type_nelts_top (type
));
1105 flags
= LOOKUP_IMPLICIT
|LOOKUP_NO_NARROWING
;
1107 for (auto &e
: CONSTRUCTOR_ELTS (ctor
))
1110 = implicit_conversion (elttype
, TREE_TYPE (e
.value
), e
.value
,
1111 false, flags
, complain
);
1115 if (sub
->rank
> rank
)
1117 if (sub
->user_conv_p
)
1123 c
= alloc_conversion (ck_aggr
);
1126 c
->user_conv_p
= user
;
1132 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1133 complex type, if such a conversion is possible. */
1136 build_complex_conv (tree type
, tree ctor
, int flags
,
1137 tsubst_flags_t complain
)
1140 unsigned HOST_WIDE_INT len
= CONSTRUCTOR_NELTS (ctor
);
1141 tree elttype
= TREE_TYPE (type
);
1144 enum conversion_rank rank
= cr_exact
;
1149 flags
= LOOKUP_IMPLICIT
|LOOKUP_NO_NARROWING
;
1151 for (auto &e
: CONSTRUCTOR_ELTS (ctor
))
1154 = implicit_conversion (elttype
, TREE_TYPE (e
.value
), e
.value
,
1155 false, flags
, complain
);
1159 if (sub
->rank
> rank
)
1161 if (sub
->user_conv_p
)
1167 c
= alloc_conversion (ck_aggr
);
1170 c
->user_conv_p
= user
;
1176 /* Build a representation of the identity conversion from EXPR to
1177 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1180 build_identity_conv (tree type
, tree expr
)
1184 c
= alloc_conversion (ck_identity
);
1191 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1192 were multiple user-defined conversions to accomplish the job.
1193 Build a conversion that indicates that ambiguity. */
1196 build_ambiguous_conv (tree type
, tree expr
)
1200 c
= alloc_conversion (ck_ambig
);
1208 strip_top_quals (tree t
)
1210 if (TREE_CODE (t
) == ARRAY_TYPE
)
1212 return cp_build_qualified_type (t
, 0);
1215 /* Returns the standard conversion path (see [conv]) from type FROM to type
1216 TO, if any. For proper handling of null pointer constants, you must
1217 also pass the expression EXPR to convert from. If C_CAST_P is true,
1218 this conversion is coming from a C-style cast. */
1221 standard_conversion (tree to
, tree from
, tree expr
, bool c_cast_p
,
1222 int flags
, tsubst_flags_t complain
)
1224 enum tree_code fcode
, tcode
;
1226 bool fromref
= false;
1229 to
= non_reference (to
);
1230 if (TYPE_REF_P (from
))
1233 from
= TREE_TYPE (from
);
1236 to
= strip_top_quals (to
);
1237 from
= strip_top_quals (from
);
1239 if (expr
&& type_unknown_p (expr
))
1241 if (TYPE_PTRFN_P (to
) || TYPE_PTRMEMFUNC_P (to
))
1243 tsubst_flags_t tflags
= tf_conv
;
1244 expr
= instantiate_type (to
, expr
, tflags
);
1245 if (expr
== error_mark_node
)
1247 from
= TREE_TYPE (expr
);
1249 else if (TREE_CODE (to
) == BOOLEAN_TYPE
)
1251 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1252 expr
= resolve_nondeduced_context (expr
, complain
);
1253 from
= TREE_TYPE (expr
);
1257 fcode
= TREE_CODE (from
);
1258 tcode
= TREE_CODE (to
);
1260 conv
= build_identity_conv (from
, expr
);
1261 if (fcode
== FUNCTION_TYPE
|| fcode
== ARRAY_TYPE
)
1263 from
= type_decays_to (from
);
1264 fcode
= TREE_CODE (from
);
1265 /* Tell convert_like that we're using the address. */
1266 conv
->rvaluedness_matches_p
= true;
1267 conv
= build_conv (ck_lvalue
, from
, conv
);
1269 /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1270 obvalue_p) seems odd, since it's already a prvalue, but that's how we
1271 express the copy constructor call required by copy-initialization. */
1272 else if (fromref
|| (expr
&& obvalue_p (expr
)))
1277 bitfield_type
= is_bitfield_expr_with_lowered_type (expr
);
1280 from
= strip_top_quals (bitfield_type
);
1281 fcode
= TREE_CODE (from
);
1284 conv
= build_conv (ck_rvalue
, from
, conv
);
1285 /* If we're performing copy-initialization, remember to skip
1286 explicit constructors. */
1287 if (flags
& LOOKUP_ONLYCONVERTING
)
1288 conv
->copy_init_p
= true;
1291 /* Allow conversion between `__complex__' data types. */
1292 if (tcode
== COMPLEX_TYPE
&& fcode
== COMPLEX_TYPE
)
1294 /* The standard conversion sequence to convert FROM to TO is
1295 the standard conversion sequence to perform componentwise
1297 conversion
*part_conv
= standard_conversion
1298 (TREE_TYPE (to
), TREE_TYPE (from
), NULL_TREE
, c_cast_p
, flags
,
1303 else if (part_conv
->kind
== ck_identity
)
1304 /* Leave conv alone. */;
1307 conv
= build_conv (part_conv
->kind
, to
, conv
);
1308 conv
->rank
= part_conv
->rank
;
1314 if (same_type_p (from
, to
))
1316 if (CLASS_TYPE_P (to
) && conv
->kind
== ck_rvalue
)
1317 conv
->type
= qualified_to
;
1322 A null pointer constant can be converted to a pointer type; ... A
1323 null pointer constant of integral type can be converted to an
1324 rvalue of type std::nullptr_t. */
1325 if ((tcode
== POINTER_TYPE
|| TYPE_PTRMEM_P (to
)
1326 || NULLPTR_TYPE_P (to
))
1327 && ((expr
&& null_ptr_cst_p (expr
))
1328 || NULLPTR_TYPE_P (from
)))
1329 conv
= build_conv (ck_std
, to
, conv
);
1330 else if ((tcode
== INTEGER_TYPE
&& fcode
== POINTER_TYPE
)
1331 || (tcode
== POINTER_TYPE
&& fcode
== INTEGER_TYPE
))
1333 /* For backwards brain damage compatibility, allow interconversion of
1334 pointers and integers with a pedwarn. */
1335 conv
= build_conv (ck_std
, to
, conv
);
1338 else if (UNSCOPED_ENUM_P (to
) && fcode
== INTEGER_TYPE
)
1340 /* For backwards brain damage compatibility, allow interconversion of
1341 enums and integers with a pedwarn. */
1342 conv
= build_conv (ck_std
, to
, conv
);
1345 else if ((tcode
== POINTER_TYPE
&& fcode
== POINTER_TYPE
)
1346 || (TYPE_PTRDATAMEM_P (to
) && TYPE_PTRDATAMEM_P (from
)))
1351 if (tcode
== POINTER_TYPE
)
1353 to_pointee
= TREE_TYPE (to
);
1354 from_pointee
= TREE_TYPE (from
);
1356 /* Since this is the target of a pointer, it can't have function
1357 qualifiers, so any TYPE_QUALS must be for attributes const or
1358 noreturn. Strip them. */
1359 if (TREE_CODE (to_pointee
) == FUNCTION_TYPE
1360 && TYPE_QUALS (to_pointee
))
1361 to_pointee
= build_qualified_type (to_pointee
, TYPE_UNQUALIFIED
);
1362 if (TREE_CODE (from_pointee
) == FUNCTION_TYPE
1363 && TYPE_QUALS (from_pointee
))
1364 from_pointee
= build_qualified_type (from_pointee
, TYPE_UNQUALIFIED
);
1368 to_pointee
= TYPE_PTRMEM_POINTED_TO_TYPE (to
);
1369 from_pointee
= TYPE_PTRMEM_POINTED_TO_TYPE (from
);
1372 if (tcode
== POINTER_TYPE
1373 && same_type_ignoring_top_level_qualifiers_p (from_pointee
,
1376 else if (VOID_TYPE_P (to_pointee
)
1377 && !TYPE_PTRDATAMEM_P (from
)
1378 && TREE_CODE (from_pointee
) != FUNCTION_TYPE
)
1380 tree nfrom
= TREE_TYPE (from
);
1381 /* Don't try to apply restrict to void. */
1382 int quals
= cp_type_quals (nfrom
) & ~TYPE_QUAL_RESTRICT
;
1383 from_pointee
= cp_build_qualified_type (void_type_node
, quals
);
1384 from
= build_pointer_type (from_pointee
);
1385 conv
= build_conv (ck_ptr
, from
, conv
);
1387 else if (TYPE_PTRDATAMEM_P (from
))
1389 tree fbase
= TYPE_PTRMEM_CLASS_TYPE (from
);
1390 tree tbase
= TYPE_PTRMEM_CLASS_TYPE (to
);
1392 if (same_type_p (fbase
, tbase
))
1393 /* No base conversion needed. */;
1394 else if (DERIVED_FROM_P (fbase
, tbase
)
1395 && (same_type_ignoring_top_level_qualifiers_p
1396 (from_pointee
, to_pointee
)))
1398 from
= build_ptrmem_type (tbase
, from_pointee
);
1399 conv
= build_conv (ck_pmem
, from
, conv
);
1404 else if (CLASS_TYPE_P (from_pointee
)
1405 && CLASS_TYPE_P (to_pointee
)
1408 An rvalue of type "pointer to cv D," where D is a
1409 class type, can be converted to an rvalue of type
1410 "pointer to cv B," where B is a base class (clause
1411 _class.derived_) of D. If B is an inaccessible
1412 (clause _class.access_) or ambiguous
1413 (_class.member.lookup_) base class of D, a program
1414 that necessitates this conversion is ill-formed.
1415 Therefore, we use DERIVED_FROM_P, and do not check
1416 access or uniqueness. */
1417 && DERIVED_FROM_P (to_pointee
, from_pointee
))
1420 = cp_build_qualified_type (to_pointee
,
1421 cp_type_quals (from_pointee
));
1422 from
= build_pointer_type (from_pointee
);
1423 conv
= build_conv (ck_ptr
, from
, conv
);
1424 conv
->base_p
= true;
1427 if (same_type_p (from
, to
))
1429 else if (c_cast_p
&& comp_ptr_ttypes_const (to
, from
, bounds_either
))
1430 /* In a C-style cast, we ignore CV-qualification because we
1431 are allowed to perform a static_cast followed by a
1433 conv
= build_conv (ck_qual
, to
, conv
);
1434 else if (!c_cast_p
&& comp_ptr_ttypes (to_pointee
, from_pointee
))
1435 conv
= build_conv (ck_qual
, to
, conv
);
1436 else if (expr
&& string_conv_p (to
, expr
, 0))
1437 /* converting from string constant to char *. */
1438 conv
= build_conv (ck_qual
, to
, conv
);
1439 else if (fnptr_conv_p (to
, from
))
1440 conv
= build_conv (ck_fnptr
, to
, conv
);
1441 /* Allow conversions among compatible ObjC pointer types (base
1442 conversions have been already handled above). */
1443 else if (c_dialect_objc ()
1444 && objc_compare_types (to
, from
, -4, NULL_TREE
))
1445 conv
= build_conv (ck_ptr
, to
, conv
);
1446 else if (ptr_reasonably_similar (to_pointee
, from_pointee
))
1448 conv
= build_conv (ck_ptr
, to
, conv
);
1456 else if (TYPE_PTRMEMFUNC_P (to
) && TYPE_PTRMEMFUNC_P (from
))
1458 tree fromfn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from
));
1459 tree tofn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to
));
1460 tree fbase
= class_of_this_parm (fromfn
);
1461 tree tbase
= class_of_this_parm (tofn
);
1463 /* If FBASE and TBASE are equivalent but incomplete, DERIVED_FROM_P
1464 yields false. But a pointer to member of incomplete class is OK. */
1465 if (!same_type_p (fbase
, tbase
) && !DERIVED_FROM_P (fbase
, tbase
))
1468 tree fstat
= static_fn_type (fromfn
);
1469 tree tstat
= static_fn_type (tofn
);
1470 if (same_type_p (tstat
, fstat
)
1471 || fnptr_conv_p (tstat
, fstat
))
1476 if (!same_type_p (fbase
, tbase
))
1478 from
= build_memfn_type (fstat
,
1480 cp_type_quals (tbase
),
1481 type_memfn_rqual (tofn
));
1482 from
= build_ptrmemfunc_type (build_pointer_type (from
));
1483 conv
= build_conv (ck_pmem
, from
, conv
);
1484 conv
->base_p
= true;
1486 if (fnptr_conv_p (tstat
, fstat
))
1487 conv
= build_conv (ck_fnptr
, to
, conv
);
1489 else if (tcode
== BOOLEAN_TYPE
)
1493 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1494 to member type can be converted to a prvalue of type bool. ...
1495 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1496 std::nullptr_t can be converted to a prvalue of type bool; */
1497 if (ARITHMETIC_TYPE_P (from
)
1498 || UNSCOPED_ENUM_P (from
)
1499 || fcode
== POINTER_TYPE
1500 || TYPE_PTRMEM_P (from
)
1501 || NULLPTR_TYPE_P (from
))
1503 conv
= build_conv (ck_std
, to
, conv
);
1504 if (fcode
== POINTER_TYPE
1505 || TYPE_PTRDATAMEM_P (from
)
1506 || (TYPE_PTRMEMFUNC_P (from
)
1507 && conv
->rank
< cr_pbool
)
1508 || NULLPTR_TYPE_P (from
))
1509 conv
->rank
= cr_pbool
;
1510 if (NULLPTR_TYPE_P (from
) && (flags
& LOOKUP_ONLYCONVERTING
))
1512 if (flags
& LOOKUP_NO_NARROWING
)
1513 conv
->check_narrowing
= true;
1519 /* We don't check for ENUMERAL_TYPE here because there are no standard
1520 conversions to enum type. */
1521 /* As an extension, allow conversion to complex type. */
1522 else if (ARITHMETIC_TYPE_P (to
))
1524 if (! (INTEGRAL_CODE_P (fcode
)
1525 || (fcode
== REAL_TYPE
&& !(flags
& LOOKUP_NO_NON_INTEGRAL
)))
1526 || SCOPED_ENUM_P (from
))
1529 /* If we're parsing an enum with no fixed underlying type, we're
1530 dealing with an incomplete type, which renders the conversion
1532 if (!COMPLETE_TYPE_P (from
))
1535 conv
= build_conv (ck_std
, to
, conv
);
1537 tree underlying_type
= NULL_TREE
;
1538 if (TREE_CODE (from
) == ENUMERAL_TYPE
1539 && ENUM_FIXED_UNDERLYING_TYPE_P (from
))
1540 underlying_type
= ENUM_UNDERLYING_TYPE (from
);
1542 /* Give this a better rank if it's a promotion.
1544 To handle CWG 1601, also bump the rank if we are converting
1545 an enumeration with a fixed underlying type to the underlying
1547 if ((same_type_p (to
, type_promotes_to (from
))
1548 || (underlying_type
&& same_type_p (to
, underlying_type
)))
1549 && next_conversion (conv
)->rank
<= cr_promotion
)
1550 conv
->rank
= cr_promotion
;
1552 /* A prvalue of floating-point type can be converted to a prvalue of
1553 another floating-point type with a greater or equal conversion
1554 rank ([conv.rank]). A prvalue of standard floating-point type can
1555 be converted to a prvalue of another standard floating-point type.
1556 For backwards compatibility with handling __float128 and other
1557 non-standard floating point types, allow all implicit floating
1558 point conversions if neither type is extended floating-point
1559 type and if at least one of them is, fail if they have unordered
1560 conversion rank or from has higher conversion rank. */
1561 if (fcode
== REAL_TYPE
1562 && tcode
== REAL_TYPE
1563 && (extended_float_type_p (from
)
1564 || extended_float_type_p (to
))
1565 && cp_compare_floating_point_conversion_ranks (from
, to
) >= 2)
1568 else if (fcode
== VECTOR_TYPE
&& tcode
== VECTOR_TYPE
1569 && vector_types_convertible_p (from
, to
, false))
1570 return build_conv (ck_std
, to
, conv
);
1571 else if (MAYBE_CLASS_TYPE_P (to
) && MAYBE_CLASS_TYPE_P (from
)
1572 && is_properly_derived_from (from
, to
))
1574 if (conv
->kind
== ck_rvalue
)
1575 conv
= next_conversion (conv
);
1576 conv
= build_conv (ck_base
, to
, conv
);
1577 /* The derived-to-base conversion indicates the initialization
1578 of a parameter with base type from an object of a derived
1579 type. A temporary object is created to hold the result of
1580 the conversion unless we're binding directly to a reference. */
1581 conv
->need_temporary_p
= !(flags
& LOOKUP_NO_TEMP_BIND
);
1582 /* If we're performing copy-initialization, remember to skip
1583 explicit constructors. */
1584 if (flags
& LOOKUP_ONLYCONVERTING
)
1585 conv
->copy_init_p
= true;
1590 if (flags
& LOOKUP_NO_NARROWING
)
1591 conv
->check_narrowing
= true;
1596 /* Returns nonzero if T1 is reference-related to T2. */
1599 reference_related_p (tree t1
, tree t2
)
1601 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1604 t1
= TYPE_MAIN_VARIANT (t1
);
1605 t2
= TYPE_MAIN_VARIANT (t2
);
1609 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1610 to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1611 return (similar_type_p (t1
, t2
)
1612 || (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
1613 && DERIVED_FROM_P (t1
, t2
)));
1616 /* Returns nonzero if T1 is reference-compatible with T2. */
1619 reference_compatible_p (tree t1
, tree t2
)
1623 "cv1 T1" is reference compatible with "cv2 T2" if
1624 a prvalue of type "pointer to cv2 T2" can be converted to the type
1625 "pointer to cv1 T1" via a standard conversion sequence. */
1626 tree ptype1
= build_pointer_type (t1
);
1627 tree ptype2
= build_pointer_type (t2
);
1628 conversion
*conv
= standard_conversion (ptype1
, ptype2
, NULL_TREE
,
1629 /*c_cast_p=*/false, 0, tf_none
);
1630 if (!conv
|| conv
->bad_p
)
1635 /* Return true if converting FROM to TO would involve a qualification
1639 involves_qualification_conversion_p (tree to
, tree from
)
1641 /* If we're not convering a pointer to another one, we won't get
1642 a qualification conversion. */
1643 if (!((TYPE_PTR_P (to
) && TYPE_PTR_P (from
))
1644 || (TYPE_PTRDATAMEM_P (to
) && TYPE_PTRDATAMEM_P (from
))))
1647 conversion
*conv
= standard_conversion (to
, from
, NULL_TREE
,
1648 /*c_cast_p=*/false, 0, tf_none
);
1649 for (conversion
*t
= conv
; t
; t
= next_conversion (t
))
1650 if (t
->kind
== ck_qual
)
1656 /* A reference of the indicated TYPE is being bound directly to the
1657 expression represented by the implicit conversion sequence CONV.
1658 Return a conversion sequence for this binding. */
1661 direct_reference_binding (tree type
, conversion
*conv
)
1665 gcc_assert (TYPE_REF_P (type
));
1666 gcc_assert (!TYPE_REF_P (conv
->type
));
1668 t
= TREE_TYPE (type
);
1670 if (conv
->kind
== ck_identity
)
1671 /* Mark the identity conv as to not decay to rvalue. */
1672 conv
->rvaluedness_matches_p
= true;
1676 When a parameter of reference type binds directly
1677 (_dcl.init.ref_) to an argument expression, the implicit
1678 conversion sequence is the identity conversion, unless the
1679 argument expression has a type that is a derived class of the
1680 parameter type, in which case the implicit conversion sequence is
1681 a derived-to-base Conversion.
1683 If the parameter binds directly to the result of applying a
1684 conversion function to the argument expression, the implicit
1685 conversion sequence is a user-defined conversion sequence
1686 (_over.ics.user_), with the second standard conversion sequence
1687 either an identity conversion or, if the conversion function
1688 returns an entity of a type that is a derived class of the
1689 parameter type, a derived-to-base conversion. */
1690 if (is_properly_derived_from (conv
->type
, t
))
1692 /* Represent the derived-to-base conversion. */
1693 conv
= build_conv (ck_base
, t
, conv
);
1694 /* We will actually be binding to the base-class subobject in
1695 the derived class, so we mark this conversion appropriately.
1696 That way, convert_like knows not to generate a temporary. */
1697 conv
->need_temporary_p
= false;
1699 else if (involves_qualification_conversion_p (t
, conv
->type
))
1700 /* Represent the qualification conversion. After DR 2352
1701 #1 and #2 were indistinguishable conversion sequences:
1704 void f(const int* const &); // #2
1705 void g(int* p) { f(p); }
1707 because the types "int *" and "const int *const" are
1708 reference-related and we were binding both directly and they
1709 had the same rank. To break it up, we add a ck_qual under the
1710 ck_ref_bind so that conversion sequence ranking chooses #1.
1712 We strip_top_quals here which is also what standard_conversion
1713 does. Failure to do so would confuse comp_cv_qual_signature
1714 into thinking that in
1716 void f(const int * const &); // #1
1717 void f(const int *); // #2
1721 #2 is a better match than #1 even though they're ambiguous (97296). */
1722 conv
= build_conv (ck_qual
, strip_top_quals (t
), conv
);
1724 return build_conv (ck_ref_bind
, type
, conv
);
1727 /* Returns the conversion path from type FROM to reference type TO for
1728 purposes of reference binding. For lvalue binding, either pass a
1729 reference type to FROM or an lvalue expression to EXPR. If the
1730 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1731 the conversion returned. If C_CAST_P is true, this
1732 conversion is coming from a C-style cast. */
1735 reference_binding (tree rto
, tree rfrom
, tree expr
, bool c_cast_p
, int flags
,
1736 tsubst_flags_t complain
)
1738 conversion
*conv
= NULL
;
1739 tree to
= TREE_TYPE (rto
);
1744 cp_lvalue_kind gl_kind
;
1747 if (TREE_CODE (to
) == FUNCTION_TYPE
&& expr
&& type_unknown_p (expr
))
1749 expr
= instantiate_type (to
, expr
, tf_none
);
1750 if (expr
== error_mark_node
)
1752 from
= TREE_TYPE (expr
);
1755 bool copy_list_init
= false;
1756 if (expr
&& BRACE_ENCLOSED_INITIALIZER_P (expr
))
1758 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
1759 /* DR 1288: Otherwise, if the initializer list has a single element
1760 of type E and ... [T's] referenced type is reference-related to E,
1761 the object or reference is initialized from that element...
1763 ??? With P0388R4, we should bind 't' directly to U{}:
1766 because A[] and A[2] are reference-related. But we don't do it
1767 because grok_reference_init has deduced the array size (to 1), and
1768 A[1] and A[2] aren't reference-related. */
1769 if (CONSTRUCTOR_NELTS (expr
) == 1
1770 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr
))
1772 tree elt
= CONSTRUCTOR_ELT (expr
, 0)->value
;
1773 if (error_operand_p (elt
))
1775 tree etype
= TREE_TYPE (elt
);
1776 if (reference_related_p (to
, etype
))
1783 /* Otherwise, if T is a reference type, a prvalue temporary of the type
1784 referenced by T is copy-list-initialized, and the reference is bound
1785 to that temporary. */
1786 copy_list_init
= true;
1790 if (TYPE_REF_P (from
))
1792 from
= TREE_TYPE (from
);
1793 if (!TYPE_REF_IS_RVALUE (rfrom
)
1794 || TREE_CODE (from
) == FUNCTION_TYPE
)
1795 gl_kind
= clk_ordinary
;
1797 gl_kind
= clk_rvalueref
;
1800 gl_kind
= lvalue_kind (expr
);
1801 else if (CLASS_TYPE_P (from
)
1802 || TREE_CODE (from
) == ARRAY_TYPE
)
1803 gl_kind
= clk_class
;
1807 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1808 if ((flags
& LOOKUP_NO_TEMP_BIND
)
1809 && (gl_kind
& clk_class
))
1812 /* Same mask as real_lvalue_p. */
1813 is_lvalue
= gl_kind
&& !(gl_kind
& (clk_rvalueref
|clk_class
));
1816 if ((gl_kind
& clk_bitfield
) != 0)
1817 tfrom
= unlowered_expr_type (expr
);
1819 /* Figure out whether or not the types are reference-related and
1820 reference compatible. We have to do this after stripping
1821 references from FROM. */
1822 related_p
= reference_related_p (to
, tfrom
);
1823 /* If this is a C cast, first convert to an appropriately qualified
1824 type, so that we can later do a const_cast to the desired type. */
1825 if (related_p
&& c_cast_p
1826 && !at_least_as_qualified_p (to
, tfrom
))
1827 to
= cp_build_qualified_type (to
, cp_type_quals (tfrom
));
1828 compatible_p
= reference_compatible_p (to
, tfrom
);
1830 /* Directly bind reference when target expression's type is compatible with
1831 the reference and expression is an lvalue. In DR391, the wording in
1832 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1833 const and rvalue references to rvalues of compatible class type.
1834 We should also do direct bindings for non-class xvalues. */
1835 if ((related_p
|| compatible_p
) && gl_kind
)
1839 If the initializer expression
1841 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1842 is reference-compatible with "cv2 T2,"
1844 the reference is bound directly to the initializer expression
1848 If the initializer expression is an rvalue, with T2 a class type,
1849 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1850 is bound to the object represented by the rvalue or to a sub-object
1851 within that object. */
1853 conv
= build_identity_conv (tfrom
, expr
);
1854 conv
= direct_reference_binding (rto
, conv
);
1856 if (TYPE_REF_P (rfrom
))
1857 /* Handle rvalue reference to function properly. */
1858 conv
->rvaluedness_matches_p
1859 = (TYPE_REF_IS_RVALUE (rto
) == TYPE_REF_IS_RVALUE (rfrom
));
1861 conv
->rvaluedness_matches_p
1862 = (TYPE_REF_IS_RVALUE (rto
) == !is_lvalue
);
1864 if ((gl_kind
& clk_bitfield
) != 0
1865 || ((gl_kind
& clk_packed
) != 0 && !TYPE_PACKED (to
)))
1866 /* For the purposes of overload resolution, we ignore the fact
1867 this expression is a bitfield or packed field. (In particular,
1868 [over.ics.ref] says specifically that a function with a
1869 non-const reference parameter is viable even if the
1870 argument is a bitfield.)
1872 However, when we actually call the function we must create
1873 a temporary to which to bind the reference. If the
1874 reference is volatile, or isn't const, then we cannot make
1875 a temporary, so we just issue an error when the conversion
1877 conv
->need_temporary_p
= true;
1879 /* Don't allow binding of lvalues (other than function lvalues) to
1880 rvalue references. */
1881 if (is_lvalue
&& TYPE_REF_IS_RVALUE (rto
)
1882 && TREE_CODE (to
) != FUNCTION_TYPE
)
1885 /* Nor the reverse. */
1886 if (!is_lvalue
&& !TYPE_REF_IS_RVALUE (rto
)
1887 /* Unless it's really a C++20 lvalue being treated as an xvalue.
1888 But in C++23, such an expression is just an xvalue, not a special
1889 lvalue, so the binding is once again ill-formed. */
1890 && !(cxx_dialect
<= cxx20
1891 && (gl_kind
& clk_implicit_rval
))
1892 && (!CP_TYPE_CONST_NON_VOLATILE_P (to
)
1893 || (flags
& LOOKUP_NO_RVAL_BIND
))
1894 && TREE_CODE (to
) != FUNCTION_TYPE
)
1902 /* [class.conv.fct] A conversion function is never used to convert a
1903 (possibly cv-qualified) object to the (possibly cv-qualified) same
1904 object type (or a reference to it), to a (possibly cv-qualified) base
1905 class of that type (or a reference to it).... */
1906 else if (CLASS_TYPE_P (from
) && !related_p
1907 && !(flags
& LOOKUP_NO_CONVERSION
))
1911 If the initializer expression
1913 -- has a class type (i.e., T2 is a class type) can be
1914 implicitly converted to an lvalue of type "cv3 T3," where
1915 "cv1 T1" is reference-compatible with "cv3 T3". (this
1916 conversion is selected by enumerating the applicable
1917 conversion functions (_over.match.ref_) and choosing the
1918 best one through overload resolution. (_over.match_).
1920 the reference is bound to the lvalue result of the conversion
1921 in the second case. */
1922 z_candidate
*cand
= build_user_type_conversion_1 (rto
, expr
, flags
,
1925 return cand
->second_conv
;
1928 /* From this point on, we conceptually need temporaries, even if we
1929 elide them. Only the cases above are "direct bindings". */
1930 if (flags
& LOOKUP_NO_TEMP_BIND
)
1935 When a parameter of reference type is not bound directly to an
1936 argument expression, the conversion sequence is the one required
1937 to convert the argument expression to the underlying type of the
1938 reference according to _over.best.ics_. Conceptually, this
1939 conversion sequence corresponds to copy-initializing a temporary
1940 of the underlying type with the argument expression. Any
1941 difference in top-level cv-qualification is subsumed by the
1942 initialization itself and does not constitute a conversion. */
1944 bool maybe_valid_p
= true;
1948 Otherwise, the reference shall be an lvalue reference to a
1949 non-volatile const type, or the reference shall be an rvalue
1951 if (!CP_TYPE_CONST_NON_VOLATILE_P (to
) && !TYPE_REF_IS_RVALUE (rto
))
1952 maybe_valid_p
= false;
1956 Otherwise, a temporary of type "cv1 T1" is created and
1957 initialized from the initializer expression using the rules for a
1958 non-reference copy initialization. If T1 is reference-related to
1959 T2, cv1 must be the same cv-qualification as, or greater
1960 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1961 if (related_p
&& !at_least_as_qualified_p (to
, from
))
1962 maybe_valid_p
= false;
1964 /* We try below to treat an invalid reference binding as a bad conversion
1965 to improve diagnostics, but doing so may cause otherwise unnecessary
1966 instantiations that can lead to a hard error. So during the first pass
1967 of overload resolution wherein we shortcut bad conversions, instead just
1968 produce a special conversion indicating a second pass is necessary if
1969 there's no strictly viable candidate. */
1970 if (!maybe_valid_p
&& (flags
& LOOKUP_SHORTCUT_BAD_CONVS
))
1972 conv
= alloc_conversion (ck_deferred_bad
);
1977 /* We're generating a temporary now, but don't bind any more in the
1978 conversion (specifically, don't slice the temporary returned by a
1979 conversion operator). */
1980 flags
|= LOOKUP_NO_TEMP_BIND
;
1982 /* Core issue 899: When [copy-]initializing a temporary to be bound
1983 to the first parameter of a copy constructor (12.8) called with
1984 a single argument in the context of direct-initialization,
1985 explicit conversion functions are also considered.
1987 So don't set LOOKUP_ONLYCONVERTING in that case. */
1988 if (!(flags
& LOOKUP_COPY_PARM
))
1989 flags
|= LOOKUP_ONLYCONVERTING
;
1992 conv
= implicit_conversion (to
, from
, expr
, c_cast_p
,
1997 if (conv
->user_conv_p
)
2000 /* Remember this was copy-list-initialization. */
2001 conv
->need_temporary_p
= true;
2003 /* If initializing the temporary used a conversion function,
2004 recalculate the second conversion sequence. */
2005 for (conversion
*t
= conv
; t
; t
= next_conversion (t
))
2006 if (t
->kind
== ck_user
2007 && DECL_CONV_FN_P (t
->cand
->fn
))
2009 tree ftype
= TREE_TYPE (TREE_TYPE (t
->cand
->fn
));
2010 /* A prvalue of non-class type is cv-unqualified. */
2011 if (!TYPE_REF_P (ftype
) && !CLASS_TYPE_P (ftype
))
2012 ftype
= cv_unqualified (ftype
);
2013 int sflags
= (flags
|LOOKUP_NO_CONVERSION
)&~LOOKUP_NO_TEMP_BIND
;
2014 conversion
*new_second
2015 = reference_binding (rto
, ftype
, NULL_TREE
, c_cast_p
,
2019 conv
= merge_conversion_sequences (t
, new_second
);
2020 gcc_assert (maybe_valid_p
|| conv
->bad_p
);
2025 conv
= build_conv (ck_ref_bind
, rto
, conv
);
2026 /* This reference binding, unlike those above, requires the
2027 creation of a temporary. */
2028 conv
->need_temporary_p
= true;
2029 conv
->rvaluedness_matches_p
= TYPE_REF_IS_RVALUE (rto
);
2030 conv
->bad_p
|= !maybe_valid_p
;
2035 /* Most of the implementation of implicit_conversion, with the same
2039 implicit_conversion_1 (tree to
, tree from
, tree expr
, bool c_cast_p
,
2040 int flags
, tsubst_flags_t complain
)
2044 if (from
== error_mark_node
|| to
== error_mark_node
2045 || expr
== error_mark_node
)
2048 /* Other flags only apply to the primary function in overload
2049 resolution, or after we've chosen one. */
2050 flags
&= (LOOKUP_ONLYCONVERTING
|LOOKUP_NO_CONVERSION
|LOOKUP_COPY_PARM
2051 |LOOKUP_NO_TEMP_BIND
|LOOKUP_NO_RVAL_BIND
|LOOKUP_NO_NARROWING
2052 |LOOKUP_PROTECT
|LOOKUP_NO_NON_INTEGRAL
|LOOKUP_SHORTCUT_BAD_CONVS
);
2054 /* FIXME: actually we don't want warnings either, but we can't just
2055 have 'complain &= ~(tf_warning|tf_error)' because it would cause
2056 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
2057 We really ought not to issue that warning until we've committed
2058 to that conversion. */
2059 complain
&= ~tf_error
;
2061 /* Call reshape_init early to remove redundant braces. */
2062 if (expr
&& BRACE_ENCLOSED_INITIALIZER_P (expr
) && CLASS_TYPE_P (to
))
2064 to
= complete_type (to
);
2065 if (!COMPLETE_TYPE_P (to
))
2067 if (!CLASSTYPE_NON_AGGREGATE (to
))
2069 expr
= reshape_init (to
, expr
, complain
);
2070 if (expr
== error_mark_node
)
2072 from
= TREE_TYPE (expr
);
2076 if (TYPE_REF_P (to
))
2077 conv
= reference_binding (to
, from
, expr
, c_cast_p
, flags
, complain
);
2079 conv
= standard_conversion (to
, from
, expr
, c_cast_p
, flags
, complain
);
2084 if (expr
&& BRACE_ENCLOSED_INITIALIZER_P (expr
))
2086 if (is_std_init_list (to
) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr
))
2087 return build_list_conv (to
, expr
, flags
, complain
);
2089 /* As an extension, allow list-initialization of _Complex. */
2090 if (TREE_CODE (to
) == COMPLEX_TYPE
2091 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr
))
2093 conv
= build_complex_conv (to
, expr
, flags
, complain
);
2098 /* Allow conversion from an initializer-list with one element to a
2100 if (SCALAR_TYPE_P (to
))
2102 int nelts
= CONSTRUCTOR_NELTS (expr
);
2106 elt
= build_value_init (to
, tf_none
);
2107 else if (nelts
== 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr
))
2108 elt
= CONSTRUCTOR_ELT (expr
, 0)->value
;
2110 elt
= error_mark_node
;
2112 conv
= implicit_conversion (to
, TREE_TYPE (elt
), elt
,
2113 c_cast_p
, flags
, complain
);
2116 conv
->check_narrowing
= true;
2117 if (BRACE_ENCLOSED_INITIALIZER_P (elt
))
2118 /* Too many levels of braces, i.e. '{{1}}'. */
2123 else if (TREE_CODE (to
) == ARRAY_TYPE
)
2124 return build_array_conv (to
, expr
, flags
, complain
);
2127 if (expr
!= NULL_TREE
2128 && (MAYBE_CLASS_TYPE_P (from
)
2129 || MAYBE_CLASS_TYPE_P (to
))
2130 && (flags
& LOOKUP_NO_CONVERSION
) == 0)
2132 struct z_candidate
*cand
;
2134 if (CLASS_TYPE_P (to
)
2135 && BRACE_ENCLOSED_INITIALIZER_P (expr
)
2136 && !CLASSTYPE_NON_AGGREGATE (complete_type (to
)))
2137 return build_aggr_conv (to
, expr
, flags
, complain
);
2139 cand
= build_user_type_conversion_1 (to
, expr
, flags
, complain
);
2142 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
2143 && CONSTRUCTOR_NELTS (expr
) == 1
2144 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr
)
2145 && !is_list_ctor (cand
->fn
))
2147 /* "If C is not an initializer-list constructor and the
2148 initializer list has a single element of type cv U, where U is
2149 X or a class derived from X, the implicit conversion sequence
2150 has Exact Match rank if U is X, or Conversion rank if U is
2152 tree elt
= CONSTRUCTOR_ELT (expr
, 0)->value
;
2153 tree elttype
= TREE_TYPE (elt
);
2154 if (reference_related_p (to
, elttype
))
2155 return implicit_conversion (to
, elttype
, elt
,
2156 c_cast_p
, flags
, complain
);
2158 conv
= cand
->second_conv
;
2161 /* We used to try to bind a reference to a temporary here, but that
2162 is now handled after the recursive call to this function at the end
2163 of reference_binding. */
2170 /* Returns the implicit conversion sequence (see [over.ics]) from type
2171 FROM to type TO. The optional expression EXPR may affect the
2172 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
2173 true, this conversion is coming from a C-style cast. */
2176 implicit_conversion (tree to
, tree from
, tree expr
, bool c_cast_p
,
2177 int flags
, tsubst_flags_t complain
)
2179 conversion
*conv
= implicit_conversion_1 (to
, from
, expr
, c_cast_p
,
2181 if (!conv
|| conv
->bad_p
)
2183 if (conv_is_prvalue (conv
)
2184 && CLASS_TYPE_P (conv
->type
)
2185 && CLASSTYPE_PURE_VIRTUALS (conv
->type
))
2190 /* Like implicit_conversion, but return NULL if the conversion is bad.
2192 This is not static so that check_non_deducible_conversion can call it within
2193 add_template_candidate_real as part of overload resolution; it should not be
2194 called outside of overload resolution. */
2197 good_conversion (tree to
, tree from
, tree expr
,
2198 int flags
, tsubst_flags_t complain
)
2200 conversion
*c
= implicit_conversion (to
, from
, expr
, /*cast*/false,
2207 /* Add a new entry to the list of candidates. Used by the add_*_candidate
2208 functions. ARGS will not be changed until a single candidate is
2211 static struct z_candidate
*
2212 add_candidate (struct z_candidate
**candidates
,
2213 tree fn
, tree first_arg
, const vec
<tree
, va_gc
> *args
,
2214 size_t num_convs
, conversion
**convs
,
2215 tree access_path
, tree conversion_path
,
2216 int viable
, struct rejection_reason
*reason
,
2219 struct z_candidate
*cand
= (struct z_candidate
*)
2220 conversion_obstack_alloc (sizeof (struct z_candidate
));
2223 cand
->first_arg
= first_arg
;
2225 cand
->convs
= convs
;
2226 cand
->num_convs
= num_convs
;
2227 cand
->access_path
= access_path
;
2228 cand
->conversion_path
= conversion_path
;
2229 cand
->viable
= viable
;
2230 cand
->reason
= reason
;
2231 cand
->next
= *candidates
;
2232 cand
->flags
= flags
;
2235 if (convs
&& cand
->reversed ())
2236 /* Swap the conversions for comparison in joust; we'll swap them back
2237 before build_over_call. */
2238 std::swap (convs
[0], convs
[1]);
2243 /* Return the number of remaining arguments in the parameter list
2244 beginning with ARG. */
2247 remaining_arguments (tree arg
)
2251 for (n
= 0; arg
!= NULL_TREE
&& arg
!= void_list_node
;
2252 arg
= TREE_CHAIN (arg
))
2258 /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2259 to the first parameter of a constructor where the parameter is of type
2260 "reference to possibly cv-qualified T" and the constructor is called with a
2261 single argument in the context of direct-initialization of an object of type
2262 "cv2 T", explicit conversion functions are also considered.
2264 So set LOOKUP_COPY_PARM to let reference_binding know that
2265 it's being called in that context. */
2268 conv_flags (int i
, int nargs
, tree fn
, tree arg
, int flags
)
2272 if (i
== 0 && nargs
== 1 && DECL_CONSTRUCTOR_P (fn
)
2273 && (t
= FUNCTION_FIRST_USER_PARMTYPE (fn
))
2274 && (same_type_ignoring_top_level_qualifiers_p
2275 (non_reference (TREE_VALUE (t
)), DECL_CONTEXT (fn
))))
2277 if (!(flags
& LOOKUP_ONLYCONVERTING
))
2278 lflags
|= LOOKUP_COPY_PARM
;
2279 if ((flags
& LOOKUP_LIST_INIT_CTOR
)
2280 && BRACE_ENCLOSED_INITIALIZER_P (arg
))
2281 lflags
|= LOOKUP_NO_CONVERSION
;
2284 lflags
|= LOOKUP_ONLYCONVERTING
;
2289 /* Build an appropriate 'this' conversion for the method FN and class
2290 type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE.
2291 This function modifies PARMTYPE, ARGTYPE and ARG. */
2294 build_this_conversion (tree fn
, tree ctype
,
2295 tree
& parmtype
, tree
& argtype
, tree
& arg
,
2296 int flags
, tsubst_flags_t complain
)
2298 gcc_assert (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2299 && !DECL_CONSTRUCTOR_P (fn
));
2301 /* The type of the implicit object parameter ('this') for
2302 overload resolution is not always the same as for the
2303 function itself; conversion functions are considered to
2304 be members of the class being converted, and functions
2305 introduced by a using-declaration are considered to be
2306 members of the class that uses them.
2308 Since build_over_call ignores the ICS for the `this'
2309 parameter, we can just change the parm type. */
2310 parmtype
= cp_build_qualified_type (ctype
,
2311 cp_type_quals (TREE_TYPE (parmtype
)));
2313 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn
)))
2315 /* If the function has a ref-qualifier, the implicit
2316 object parameter has reference type. */
2317 bool rv
= FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn
));
2318 parmtype
= cp_build_reference_type (parmtype
, rv
);
2319 /* The special handling of 'this' conversions in compare_ics
2320 does not apply if there is a ref-qualifier. */
2325 parmtype
= build_pointer_type (parmtype
);
2326 /* We don't use build_this here because we don't want to
2327 capture the object argument until we've chosen a
2328 non-static member function. */
2329 arg
= build_address (arg
);
2330 argtype
= lvalue_type (arg
);
2332 flags
|= LOOKUP_ONLYCONVERTING
;
2333 conversion
*t
= implicit_conversion (parmtype
, argtype
, arg
,
2334 /*c_cast_p=*/false, flags
, complain
);
2339 /* Create an overload candidate for the function or method FN called
2340 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2341 FLAGS is passed on to implicit_conversion.
2343 This does not change ARGS.
2345 CTYPE, if non-NULL, is the type we want to pretend this function
2346 comes from for purposes of overload resolution.
2348 SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions.
2349 If true, we stop computing conversions upon seeing the first bad
2350 conversion. This is used by add_candidates to avoid computing
2351 more conversions than necessary in the presence of a strictly viable
2352 candidate, while preserving the defacto behavior of overload resolution
2353 when it turns out there are only non-strictly viable candidates. */
2355 static struct z_candidate
*
2356 add_function_candidate (struct z_candidate
**candidates
,
2357 tree fn
, tree ctype
, tree first_arg
,
2358 const vec
<tree
, va_gc
> *args
, tree access_path
,
2359 tree conversion_path
, int flags
,
2361 bool shortcut_bad_convs
,
2362 tsubst_flags_t complain
)
2364 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2367 tree orig_first_arg
= first_arg
;
2370 struct rejection_reason
*reason
= NULL
;
2372 /* The `this', `in_chrg' and VTT arguments to constructors are not
2373 considered in overload resolution. */
2374 if (DECL_CONSTRUCTOR_P (fn
))
2376 if (ctor_omit_inherited_parms (fn
))
2377 /* Bring back parameters omitted from an inherited ctor. */
2378 parmlist
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn
));
2380 parmlist
= skip_artificial_parms_for (fn
, parmlist
);
2381 skip
= num_artificial_parms_for (fn
);
2382 if (skip
> 0 && first_arg
!= NULL_TREE
)
2385 first_arg
= NULL_TREE
;
2391 len
= vec_safe_length (args
) - skip
+ (first_arg
!= NULL_TREE
? 1 : 0);
2393 convs
= alloc_conversions (len
);
2395 /* 13.3.2 - Viable functions [over.match.viable]
2396 First, to be a viable function, a candidate function shall have enough
2397 parameters to agree in number with the arguments in the list.
2399 We need to check this first; otherwise, checking the ICSes might cause
2400 us to produce an ill-formed template instantiation. */
2402 parmnode
= parmlist
;
2403 for (i
= 0; i
< len
; ++i
)
2405 if (parmnode
== NULL_TREE
|| parmnode
== void_list_node
)
2407 parmnode
= TREE_CHAIN (parmnode
);
2410 if ((i
< len
&& parmnode
)
2411 || !sufficient_parms_p (parmnode
))
2413 int remaining
= remaining_arguments (parmnode
);
2415 reason
= arity_rejection (first_arg
, i
+ remaining
, len
);
2418 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2419 parameter of type "reference to cv C" (including such a constructor
2420 instantiated from a template) is excluded from the set of candidate
2421 functions when used to construct an object of type D with an argument list
2422 containing a single argument if C is reference-related to D. */
2423 if (viable
&& len
== 1 && parmlist
&& DECL_CONSTRUCTOR_P (fn
)
2424 && flag_new_inheriting_ctors
2425 && DECL_INHERITED_CTOR (fn
))
2427 tree ptype
= non_reference (TREE_VALUE (parmlist
));
2428 tree dtype
= DECL_CONTEXT (fn
);
2429 tree btype
= DECL_INHERITED_CTOR_BASE (fn
);
2430 if (reference_related_p (ptype
, dtype
)
2431 && reference_related_p (btype
, ptype
))
2434 reason
= inherited_ctor_rejection ();
2438 /* Second, for a function to be viable, its constraints must be
2440 if (flag_concepts
&& viable
&& !constraints_satisfied_p (fn
))
2442 reason
= constraint_failure ();
2446 /* When looking for a function from a subobject from an implicit
2447 copy/move constructor/operator=, don't consider anything that takes (a
2448 reference to) an unrelated type. See c++/44909 and core 1092. */
2449 if (viable
&& parmlist
&& (flags
& LOOKUP_DEFAULTED
))
2451 if (DECL_CONSTRUCTOR_P (fn
))
2453 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
2454 && DECL_OVERLOADED_OPERATOR_IS (fn
, NOP_EXPR
))
2460 parmnode
= chain_index (i
-1, parmlist
);
2461 if (!reference_related_p (non_reference (TREE_VALUE (parmnode
)),
2466 /* This only applies at the top level. */
2467 flags
&= ~LOOKUP_DEFAULTED
;
2473 if (shortcut_bad_convs
)
2474 flags
|= LOOKUP_SHORTCUT_BAD_CONVS
;
2476 flags
&= ~LOOKUP_SHORTCUT_BAD_CONVS
;
2478 /* Third, for F to be a viable function, there shall exist for each
2479 argument an implicit conversion sequence that converts that argument
2480 to the corresponding parameter of F. */
2482 parmnode
= parmlist
;
2484 for (i
= 0; i
< len
; ++i
)
2486 tree argtype
, to_type
;
2489 if (parmnode
== void_list_node
)
2494 /* Already set during deduction. */
2495 parmnode
= TREE_CHAIN (parmnode
);
2499 if (i
== 0 && first_arg
!= NULL_TREE
)
2502 arg
= CONST_CAST_TREE (
2503 (*args
)[i
+ skip
- (first_arg
!= NULL_TREE
? 1 : 0)]);
2504 argtype
= lvalue_type (arg
);
2509 tree parmtype
= TREE_VALUE (parmnode
);
2511 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2512 && !DECL_CONSTRUCTOR_P (fn
))
2513 t
= build_this_conversion (fn
, ctype
, parmtype
, argtype
, arg
,
2517 int lflags
= conv_flags (i
, len
-skip
, fn
, arg
, flags
);
2518 t
= implicit_conversion (parmtype
, argtype
, arg
,
2519 /*c_cast_p=*/false, lflags
, complain
);
2522 parmnode
= TREE_CHAIN (parmnode
);
2526 t
= build_identity_conv (argtype
, arg
);
2527 t
->ellipsis_p
= true;
2535 reason
= arg_conversion_rejection (first_arg
, i
, argtype
, to_type
,
2536 EXPR_LOCATION (arg
));
2543 reason
= bad_arg_conversion_rejection (first_arg
, i
, arg
, to_type
,
2544 EXPR_LOCATION (arg
));
2545 if (shortcut_bad_convs
)
2551 return add_candidate (candidates
, fn
, orig_first_arg
, args
, len
, convs
,
2552 access_path
, conversion_path
, viable
, reason
, flags
);
2555 /* Create an overload candidate for the conversion function FN which will
2556 be invoked for expression OBJ, producing a pointer-to-function which
2557 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2558 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2559 passed on to implicit_conversion.
2561 Actually, we don't really care about FN; we care about the type it
2562 converts to. There may be multiple conversion functions that will
2563 convert to that type, and we rely on build_user_type_conversion_1 to
2564 choose the best one; so when we create our candidate, we record the type
2565 instead of the function. */
2567 static struct z_candidate
*
2568 add_conv_candidate (struct z_candidate
**candidates
, tree fn
, tree obj
,
2569 const vec
<tree
, va_gc
> *arglist
,
2570 tree access_path
, tree conversion_path
,
2571 tsubst_flags_t complain
)
2573 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
2574 int i
, len
, viable
, flags
;
2575 tree parmlist
, parmnode
;
2577 struct rejection_reason
*reason
;
2579 for (parmlist
= totype
; TREE_CODE (parmlist
) != FUNCTION_TYPE
; )
2580 parmlist
= TREE_TYPE (parmlist
);
2581 parmlist
= TYPE_ARG_TYPES (parmlist
);
2583 len
= vec_safe_length (arglist
) + 1;
2584 convs
= alloc_conversions (len
);
2585 parmnode
= parmlist
;
2587 flags
= LOOKUP_IMPLICIT
;
2590 /* Don't bother looking up the same type twice. */
2591 if (*candidates
&& (*candidates
)->fn
== totype
)
2594 if (!constraints_satisfied_p (fn
))
2596 reason
= constraint_failure ();
2598 return add_candidate (candidates
, fn
, obj
, arglist
, len
, convs
,
2599 access_path
, conversion_path
, viable
, reason
, flags
);
2602 for (i
= 0; i
< len
; ++i
)
2604 tree arg
, argtype
, convert_type
= NULL_TREE
;
2610 arg
= (*arglist
)[i
- 1];
2611 argtype
= lvalue_type (arg
);
2615 t
= build_identity_conv (argtype
, NULL_TREE
);
2616 t
= build_conv (ck_user
, totype
, t
);
2617 /* Leave the 'cand' field null; we'll figure out the conversion in
2618 convert_like if this candidate is chosen. */
2619 convert_type
= totype
;
2621 else if (parmnode
== void_list_node
)
2625 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
,
2626 /*c_cast_p=*/false, flags
, complain
);
2627 convert_type
= TREE_VALUE (parmnode
);
2631 t
= build_identity_conv (argtype
, arg
);
2632 t
->ellipsis_p
= true;
2633 convert_type
= argtype
;
2643 reason
= bad_arg_conversion_rejection (NULL_TREE
, i
, arg
, convert_type
,
2644 EXPR_LOCATION (arg
));
2651 parmnode
= TREE_CHAIN (parmnode
);
2655 || ! sufficient_parms_p (parmnode
))
2657 int remaining
= remaining_arguments (parmnode
);
2659 reason
= arity_rejection (NULL_TREE
, i
+ remaining
, len
);
2662 return add_candidate (candidates
, totype
, obj
, arglist
, len
, convs
,
2663 access_path
, conversion_path
, viable
, reason
, flags
);
2667 build_builtin_candidate (struct z_candidate
**candidates
, tree fnname
,
2668 tree type1
, tree type2
, const vec
<tree
,va_gc
> &args
,
2669 tree
*argtypes
, int flags
, tsubst_flags_t complain
)
2676 struct rejection_reason
*reason
= NULL
;
2681 num_convs
= args
.length ();
2682 convs
= alloc_conversions (num_convs
);
2684 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2685 conversion ops are allowed. We handle that here by just checking for
2686 boolean_type_node because other operators don't ask for it. COND_EXPR
2687 also does contextual conversion to bool for the first operand, but we
2688 handle that in build_conditional_expr, and type1 here is operand 2. */
2689 if (type1
!= boolean_type_node
)
2690 flags
|= LOOKUP_ONLYCONVERTING
;
2692 for (unsigned i
= 0; i
< 2 && i
< num_convs
; ++i
)
2694 t
= implicit_conversion (types
[i
], argtypes
[i
], args
[i
],
2695 /*c_cast_p=*/false, flags
, complain
);
2699 /* We need something for printing the candidate. */
2700 t
= build_identity_conv (types
[i
], NULL_TREE
);
2701 reason
= arg_conversion_rejection (NULL_TREE
, i
, argtypes
[i
],
2702 types
[i
], EXPR_LOCATION (args
[i
]));
2707 reason
= bad_arg_conversion_rejection (NULL_TREE
, i
, args
[i
],
2709 EXPR_LOCATION (args
[i
]));
2714 /* For COND_EXPR we rearranged the arguments; undo that now. */
2717 convs
[2] = convs
[1];
2718 convs
[1] = convs
[0];
2719 t
= implicit_conversion (boolean_type_node
, argtypes
[2], args
[2],
2720 /*c_cast_p=*/false, flags
,
2727 reason
= arg_conversion_rejection (NULL_TREE
, 0, argtypes
[2],
2729 EXPR_LOCATION (args
[2]));
2733 add_candidate (candidates
, fnname
, /*first_arg=*/NULL_TREE
, /*args=*/NULL
,
2735 /*access_path=*/NULL_TREE
,
2736 /*conversion_path=*/NULL_TREE
,
2737 viable
, reason
, flags
);
2741 is_complete (tree t
)
2743 return COMPLETE_TYPE_P (complete_type (t
));
2746 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2749 promoted_arithmetic_type_p (tree type
)
2753 In this section, the term promoted integral type is used to refer
2754 to those integral types which are preserved by integral promotion
2755 (including e.g. int and long but excluding e.g. char).
2756 Similarly, the term promoted arithmetic type refers to promoted
2757 integral types plus floating types. */
2758 return ((CP_INTEGRAL_TYPE_P (type
)
2759 && same_type_p (type_promotes_to (type
), type
))
2760 || SCALAR_FLOAT_TYPE_P (type
));
2763 /* Create any builtin operator overload candidates for the operator in
2764 question given the converted operand types TYPE1 and TYPE2. The other
2765 args are passed through from add_builtin_candidates to
2766 build_builtin_candidate.
2768 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2769 If CODE is requires candidates operands of the same type of the kind
2770 of which TYPE1 and TYPE2 are, we add both candidates
2771 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2774 add_builtin_candidate (struct z_candidate
**candidates
, enum tree_code code
,
2775 enum tree_code code2
, tree fnname
, tree type1
,
2776 tree type2
, vec
<tree
,va_gc
> &args
, tree
*argtypes
,
2777 int flags
, tsubst_flags_t complain
)
2781 case POSTINCREMENT_EXPR
:
2782 case POSTDECREMENT_EXPR
:
2783 args
[1] = integer_zero_node
;
2784 type2
= integer_type_node
;
2793 /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2794 and VQ is either volatile or empty, there exist candidate operator
2795 functions of the form
2796 VQ T& operator++(VQ T&);
2797 T operator++(VQ T&, int);
2798 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2799 and VQ is either volatile or empty, there exist candidate operator
2800 functions of the form
2801 VQ T& operator--(VQ T&);
2802 T operator--(VQ T&, int);
2803 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2804 type, and VQ is either volatile or empty, there exist candidate operator
2805 functions of the form
2806 T*VQ& operator++(T*VQ&);
2807 T*VQ& operator--(T*VQ&);
2808 T* operator++(T*VQ&, int);
2809 T* operator--(T*VQ&, int); */
2811 case POSTDECREMENT_EXPR
:
2812 case PREDECREMENT_EXPR
:
2813 if (TREE_CODE (type1
) == BOOLEAN_TYPE
)
2816 case POSTINCREMENT_EXPR
:
2817 case PREINCREMENT_EXPR
:
2818 /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2820 if (TREE_CODE (type1
) == BOOLEAN_TYPE
&& cxx_dialect
>= cxx17
)
2822 if (ARITHMETIC_TYPE_P (type1
) || TYPE_PTROB_P (type1
))
2824 type1
= build_reference_type (type1
);
2829 /* 7 For every cv-qualified or cv-unqualified object type T, there
2830 exist candidate operator functions of the form
2835 8 For every function type T that does not have cv-qualifiers or
2836 a ref-qualifier, there exist candidate operator functions of the form
2837 T& operator*(T*); */
2840 if (TYPE_PTR_P (type1
)
2841 && (TYPE_PTROB_P (type1
)
2842 || TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
))
2846 /* 9 For every type T, there exist candidate operator functions of the form
2849 10 For every floating-point or promoted integral type T, there exist
2850 candidate operator functions of the form
2854 case UNARY_PLUS_EXPR
: /* unary + */
2855 if (TYPE_PTR_P (type1
))
2859 if (ARITHMETIC_TYPE_P (type1
))
2863 /* 11 For every promoted integral type T, there exist candidate operator
2864 functions of the form
2868 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
))
2872 /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
2873 is the same type as C2 or is a derived class of C2, and T is an object
2874 type or a function type there exist candidate operator functions of the
2876 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2877 where CV12 is the union of CV1 and CV2. */
2880 if (TYPE_PTR_P (type1
) && TYPE_PTRMEM_P (type2
))
2882 tree c1
= TREE_TYPE (type1
);
2883 tree c2
= TYPE_PTRMEM_CLASS_TYPE (type2
);
2885 if (CLASS_TYPE_P (c1
) && DERIVED_FROM_P (c2
, c1
)
2886 && (TYPE_PTRMEMFUNC_P (type2
)
2887 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2
))))
2892 /* 13 For every pair of types L and R, where each of L and R is a floating-point
2893 or promoted integral type, there exist candidate operator functions of the
2899 bool operator<(L, R);
2900 bool operator>(L, R);
2901 bool operator<=(L, R);
2902 bool operator>=(L, R);
2903 bool operator==(L, R);
2904 bool operator!=(L, R);
2905 where LR is the result of the usual arithmetic conversions between
2908 14 For every integral type T there exists a candidate operator function of
2911 std::strong_ordering operator<=>(T, T);
2913 15 For every pair of floating-point types L and R, there exists a candidate
2914 operator function of the form
2916 std::partial_ordering operator<=>(L, R);
2918 16 For every cv-qualified or cv-unqualified object type T there exist
2919 candidate operator functions of the form
2920 T* operator+(T*, std::ptrdiff_t);
2921 T& operator[](T*, std::ptrdiff_t);
2922 T* operator-(T*, std::ptrdiff_t);
2923 T* operator+(std::ptrdiff_t, T*);
2924 T& operator[](std::ptrdiff_t, T*);
2926 17 For every T, where T is a pointer to object type, there exist candidate
2927 operator functions of the form
2928 std::ptrdiff_t operator-(T, T);
2930 18 For every T, where T is an enumeration type or a pointer type, there
2931 exist candidate operator functions of the form
2932 bool operator<(T, T);
2933 bool operator>(T, T);
2934 bool operator<=(T, T);
2935 bool operator>=(T, T);
2936 bool operator==(T, T);
2937 bool operator!=(T, T);
2938 R operator<=>(T, T);
2940 where R is the result type specified in [expr.spaceship].
2942 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
2943 there exist candidate operator functions of the form
2944 bool operator==(T, T);
2945 bool operator!=(T, T); */
2948 if (TYPE_PTROB_P (type1
) && TYPE_PTROB_P (type2
))
2950 if (TYPE_PTROB_P (type1
)
2951 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2953 type2
= ptrdiff_type_node
;
2958 case TRUNC_DIV_EXPR
:
2959 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2963 /* This isn't exactly what's specified above for operator<=>, but it's
2964 close enough. In particular, we don't care about the return type
2965 specified above; it doesn't participate in overload resolution and it
2966 doesn't affect the semantics of the built-in operator. */
2967 case SPACESHIP_EXPR
:
2970 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
2971 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
)))
2973 if (NULLPTR_TYPE_P (type1
) && NULLPTR_TYPE_P (type2
))
2975 if (TYPE_PTRMEM_P (type1
) && null_ptr_cst_p (args
[1]))
2980 if (TYPE_PTRMEM_P (type2
) && null_ptr_cst_p (args
[0]))
2992 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2994 if (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
2996 if (TREE_CODE (type1
) == ENUMERAL_TYPE
2997 && TREE_CODE (type2
) == ENUMERAL_TYPE
)
2999 if (TYPE_PTR_P (type1
)
3000 && null_ptr_cst_p (args
[1]))
3005 if (null_ptr_cst_p (args
[0])
3006 && TYPE_PTR_P (type2
))
3014 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
3018 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
) && TYPE_PTROB_P (type2
))
3020 type1
= ptrdiff_type_node
;
3023 if (TYPE_PTROB_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
3025 type2
= ptrdiff_type_node
;
3030 /* 18For every pair of promoted integral types L and R, there exist candi-
3031 date operator functions of the form
3038 where LR is the result of the usual arithmetic conversions between
3041 case TRUNC_MOD_EXPR
:
3047 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
3051 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3052 type, VQ is either volatile or empty, and R is a promoted arithmetic
3053 type, there exist candidate operator functions of the form
3054 VQ L& operator=(VQ L&, R);
3055 VQ L& operator*=(VQ L&, R);
3056 VQ L& operator/=(VQ L&, R);
3057 VQ L& operator+=(VQ L&, R);
3058 VQ L& operator-=(VQ L&, R);
3060 20For every pair T, VQ), where T is any type and VQ is either volatile
3061 or empty, there exist candidate operator functions of the form
3062 T*VQ& operator=(T*VQ&, T*);
3064 21For every pair T, VQ), where T is a pointer to member type and VQ is
3065 either volatile or empty, there exist candidate operator functions of
3067 VQ T& operator=(VQ T&, T);
3069 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3070 unqualified complete object type, VQ is either volatile or empty, and
3071 I is a promoted integral type, there exist candidate operator func-
3073 T*VQ& operator+=(T*VQ&, I);
3074 T*VQ& operator-=(T*VQ&, I);
3076 23For every triple L, VQ, R), where L is an integral or enumeration
3077 type, VQ is either volatile or empty, and R is a promoted integral
3078 type, there exist candidate operator functions of the form
3080 VQ L& operator%=(VQ L&, R);
3081 VQ L& operator<<=(VQ L&, R);
3082 VQ L& operator>>=(VQ L&, R);
3083 VQ L& operator&=(VQ L&, R);
3084 VQ L& operator^=(VQ L&, R);
3085 VQ L& operator|=(VQ L&, R); */
3092 if (TYPE_PTROB_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
3094 type2
= ptrdiff_type_node
;
3099 case TRUNC_DIV_EXPR
:
3100 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
3104 case TRUNC_MOD_EXPR
:
3110 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
3115 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
3117 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
3118 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
3119 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
3120 || ((TYPE_PTRMEMFUNC_P (type1
)
3121 || TYPE_PTR_P (type1
))
3122 && null_ptr_cst_p (args
[1])))
3132 type1
= build_reference_type (type1
);
3138 For every pair of promoted arithmetic types L and R, there
3139 exist candidate operator functions of the form
3141 LR operator?(bool, L, R);
3143 where LR is the result of the usual arithmetic conversions
3144 between types L and R.
3146 For every type T, where T is a pointer or pointer-to-member
3147 type, there exist candidate operator functions of the form T
3148 operator?(bool, T, T); */
3150 if (promoted_arithmetic_type_p (type1
)
3151 && promoted_arithmetic_type_p (type2
))
3155 /* Otherwise, the types should be pointers. */
3156 if (!TYPE_PTR_OR_PTRMEM_P (type1
) || !TYPE_PTR_OR_PTRMEM_P (type2
))
3159 /* We don't check that the two types are the same; the logic
3160 below will actually create two candidates; one in which both
3161 parameter types are TYPE1, and one in which both parameter
3167 if (ARITHMETIC_TYPE_P (type1
))
3175 /* Make sure we don't create builtin candidates with dependent types. */
3176 bool u1
= uses_template_parms (type1
);
3177 bool u2
= type2
? uses_template_parms (type2
) : false;
3180 /* Try to recover if one of the types is non-dependent. But if
3181 there's only one type, there's nothing we can do. */
3184 /* And we lose if both are dependent. */
3187 /* Or if they have different forms. */
3188 if (TREE_CODE (type1
) != TREE_CODE (type2
))
3197 /* If we're dealing with two pointer types or two enumeral types,
3198 we need candidates for both of them. */
3199 if (type2
&& !same_type_p (type1
, type2
)
3200 && TREE_CODE (type1
) == TREE_CODE (type2
)
3201 && (TYPE_REF_P (type1
)
3202 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
3203 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
3204 || TYPE_PTRMEMFUNC_P (type1
)
3205 || MAYBE_CLASS_TYPE_P (type1
)
3206 || TREE_CODE (type1
) == ENUMERAL_TYPE
))
3208 if (TYPE_PTR_OR_PTRMEM_P (type1
))
3210 tree cptype
= composite_pointer_type (input_location
,
3216 if (cptype
!= error_mark_node
)
3218 build_builtin_candidate
3219 (candidates
, fnname
, cptype
, cptype
, args
, argtypes
,
3225 build_builtin_candidate
3226 (candidates
, fnname
, type1
, type1
, args
, argtypes
, flags
, complain
);
3227 build_builtin_candidate
3228 (candidates
, fnname
, type2
, type2
, args
, argtypes
, flags
, complain
);
3232 build_builtin_candidate
3233 (candidates
, fnname
, type1
, type2
, args
, argtypes
, flags
, complain
);
3237 type_decays_to (tree type
)
3239 if (TREE_CODE (type
) == ARRAY_TYPE
)
3240 return build_pointer_type (TREE_TYPE (type
));
3241 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3242 return build_pointer_type (type
);
3246 /* There are three conditions of builtin candidates:
3248 1) bool-taking candidates. These are the same regardless of the input.
3249 2) pointer-pair taking candidates. These are generated for each type
3250 one of the input types converts to.
3251 3) arithmetic candidates. According to the standard, we should generate
3252 all of these, but I'm trying not to...
3254 Here we generate a superset of the possible candidates for this particular
3255 case. That is a subset of the full set the standard defines, plus some
3256 other cases which the standard disallows. add_builtin_candidate will
3257 filter out the invalid set. */
3260 add_builtin_candidates (struct z_candidate
**candidates
, enum tree_code code
,
3261 enum tree_code code2
, tree fnname
,
3262 vec
<tree
, va_gc
> *argv
,
3263 int flags
, tsubst_flags_t complain
)
3267 tree type
, argtypes
[3], t
;
3268 /* TYPES[i] is the set of possible builtin-operator parameter types
3269 we will consider for the Ith argument. */
3270 vec
<tree
, va_gc
> *types
[2];
3272 vec
<tree
, va_gc
> &args
= *argv
;
3273 unsigned len
= args
.length ();
3275 for (unsigned i
= 0; i
< len
; ++i
)
3278 argtypes
[i
] = unlowered_expr_type (args
[i
]);
3280 argtypes
[i
] = NULL_TREE
;
3285 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3286 and VQ is either volatile or empty, there exist candidate operator
3287 functions of the form
3288 VQ T& operator++(VQ T&); */
3290 case POSTINCREMENT_EXPR
:
3291 case PREINCREMENT_EXPR
:
3292 case POSTDECREMENT_EXPR
:
3293 case PREDECREMENT_EXPR
:
3298 /* 24There also exist candidate operator functions of the form
3299 bool operator!(bool);
3300 bool operator&&(bool, bool);
3301 bool operator||(bool, bool); */
3303 case TRUTH_NOT_EXPR
:
3304 build_builtin_candidate
3305 (candidates
, fnname
, boolean_type_node
,
3306 NULL_TREE
, args
, argtypes
, flags
, complain
);
3309 case TRUTH_ORIF_EXPR
:
3310 case TRUTH_ANDIF_EXPR
:
3311 build_builtin_candidate
3312 (candidates
, fnname
, boolean_type_node
,
3313 boolean_type_node
, args
, argtypes
, flags
, complain
);
3329 case SPACESHIP_EXPR
:
3337 types
[0] = make_tree_vector ();
3338 types
[1] = make_tree_vector ();
3342 for (unsigned i
= 0; i
< len
; ++i
)
3344 if (MAYBE_CLASS_TYPE_P (argtypes
[i
]))
3348 if (i
== 0 && code
== MODIFY_EXPR
&& code2
== NOP_EXPR
)
3351 convs
= lookup_conversions (argtypes
[i
]);
3353 if (code
== COND_EXPR
)
3355 if (lvalue_p (args
[i
]))
3356 vec_safe_push (types
[i
], build_reference_type (argtypes
[i
]));
3358 vec_safe_push (types
[i
], TYPE_MAIN_VARIANT (argtypes
[i
]));
3364 for (; convs
; convs
= TREE_CHAIN (convs
))
3366 type
= TREE_TYPE (convs
);
3369 && (!TYPE_REF_P (type
)
3370 || CP_TYPE_CONST_P (TREE_TYPE (type
))))
3373 if (code
== COND_EXPR
&& TYPE_REF_P (type
))
3374 vec_safe_push (types
[i
], type
);
3376 type
= non_reference (type
);
3377 if (i
!= 0 || ! ref1
)
3379 type
= cv_unqualified (type_decays_to (type
));
3380 if (enum_p
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
3381 vec_safe_push (types
[i
], type
);
3382 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
3383 type
= type_promotes_to (type
);
3386 if (! vec_member (type
, types
[i
]))
3387 vec_safe_push (types
[i
], type
);
3392 if (code
== COND_EXPR
&& lvalue_p (args
[i
]))
3393 vec_safe_push (types
[i
], build_reference_type (argtypes
[i
]));
3394 type
= non_reference (argtypes
[i
]);
3395 if (i
!= 0 || ! ref1
)
3397 type
= cv_unqualified (type_decays_to (type
));
3398 if (enum_p
&& UNSCOPED_ENUM_P (type
))
3399 vec_safe_push (types
[i
], type
);
3400 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
3401 type
= type_promotes_to (type
);
3403 vec_safe_push (types
[i
], type
);
3407 /* Run through the possible parameter types of both arguments,
3408 creating candidates with those parameter types. */
3409 FOR_EACH_VEC_ELT_REVERSE (*(types
[0]), ix
, t
)
3414 if (!types
[1]->is_empty ())
3415 FOR_EACH_VEC_ELT_REVERSE (*(types
[1]), jx
, u
)
3416 add_builtin_candidate
3417 (candidates
, code
, code2
, fnname
, t
,
3418 u
, args
, argtypes
, flags
, complain
);
3420 add_builtin_candidate
3421 (candidates
, code
, code2
, fnname
, t
,
3422 NULL_TREE
, args
, argtypes
, flags
, complain
);
3425 release_tree_vector (types
[0]);
3426 release_tree_vector (types
[1]);
3430 /* If TMPL can be successfully instantiated as indicated by
3431 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3433 TMPL is the template. EXPLICIT_TARGS are any explicit template
3434 arguments. ARGLIST is the arguments provided at the call-site.
3435 This does not change ARGLIST. The RETURN_TYPE is the desired type
3436 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3437 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3438 CTYPE are ignored, and OBJ is as for add_conv_candidate.
3440 SHORTCUT_BAD_CONVS is as in add_function_candidate. */
3442 static struct z_candidate
*
3443 add_template_candidate_real (struct z_candidate
**candidates
, tree tmpl
,
3444 tree ctype
, tree explicit_targs
, tree first_arg
,
3445 const vec
<tree
, va_gc
> *arglist
, tree return_type
,
3446 tree access_path
, tree conversion_path
,
3447 int flags
, tree obj
, unification_kind_t strict
,
3448 bool shortcut_bad_convs
, tsubst_flags_t complain
)
3450 int ntparms
= DECL_NTPARMS (tmpl
);
3451 tree targs
= make_tree_vec (ntparms
);
3452 unsigned int len
= vec_safe_length (arglist
);
3453 unsigned int nargs
= (first_arg
== NULL_TREE
? 0 : 1) + len
;
3454 unsigned int skip_without_in_chrg
= 0;
3455 tree first_arg_without_in_chrg
= first_arg
;
3456 tree
*args_without_in_chrg
;
3457 unsigned int nargs_without_in_chrg
;
3458 unsigned int ia
, ix
;
3460 struct z_candidate
*cand
;
3462 struct rejection_reason
*reason
= NULL
;
3464 conversion
**convs
= NULL
;
3466 /* We don't do deduction on the in-charge parameter, the VTT
3467 parameter or 'this'. */
3468 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl
))
3470 if (first_arg_without_in_chrg
!= NULL_TREE
)
3471 first_arg_without_in_chrg
= NULL_TREE
;
3472 else if (return_type
&& strict
== DEDUCE_CALL
)
3473 /* We're deducing for a call to the result of a template conversion
3474 function, so the args don't contain 'this'; leave them alone. */;
3476 ++skip_without_in_chrg
;
3479 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl
)
3480 || DECL_BASE_CONSTRUCTOR_P (tmpl
))
3481 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl
)))
3483 if (first_arg_without_in_chrg
!= NULL_TREE
)
3484 first_arg_without_in_chrg
= NULL_TREE
;
3486 ++skip_without_in_chrg
;
3489 if (len
< skip_without_in_chrg
)
3492 if (DECL_CONSTRUCTOR_P (tmpl
) && nargs
== 2
3493 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg
),
3494 TREE_TYPE ((*arglist
)[0])))
3496 /* 12.8/6 says, "A declaration of a constructor for a class X is
3497 ill-formed if its first parameter is of type (optionally cv-qualified)
3498 X and either there are no other parameters or else all other
3499 parameters have default arguments. A member function template is never
3500 instantiated to produce such a constructor signature."
3502 So if we're trying to copy an object of the containing class, don't
3503 consider a template constructor that has a first parameter type that
3504 is just a template parameter, as we would deduce a signature that we
3505 would then reject in the code below. */
3506 if (tree firstparm
= FUNCTION_FIRST_USER_PARMTYPE (tmpl
))
3508 firstparm
= TREE_VALUE (firstparm
);
3509 if (PACK_EXPANSION_P (firstparm
))
3510 firstparm
= PACK_EXPANSION_PATTERN (firstparm
);
3511 if (TREE_CODE (firstparm
) == TEMPLATE_TYPE_PARM
)
3513 gcc_assert (!explicit_targs
);
3514 reason
= invalid_copy_with_fn_template_rejection ();
3520 nargs_without_in_chrg
= ((first_arg_without_in_chrg
!= NULL_TREE
? 1 : 0)
3521 + (len
- skip_without_in_chrg
));
3522 args_without_in_chrg
= XALLOCAVEC (tree
, nargs_without_in_chrg
);
3524 if (first_arg_without_in_chrg
!= NULL_TREE
)
3526 args_without_in_chrg
[ia
] = first_arg_without_in_chrg
;
3529 for (ix
= skip_without_in_chrg
;
3530 vec_safe_iterate (arglist
, ix
, &arg
);
3533 args_without_in_chrg
[ia
] = arg
;
3536 gcc_assert (ia
== nargs_without_in_chrg
);
3540 /* Check that there's no obvious arity mismatch before proceeding with
3541 deduction. This avoids substituting explicit template arguments
3542 into the template or e.g. derived-to-base parm/arg unification
3543 (which could result in an error outside the immediate context) when
3544 the resulting candidate would be unviable anyway. */
3545 int min_arity
= 0, max_arity
= 0;
3546 tree parms
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
3547 parms
= skip_artificial_parms_for (tmpl
, parms
);
3548 for (; parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
3550 if (!parms
|| PACK_EXPANSION_P (TREE_VALUE (parms
)))
3555 if (TREE_PURPOSE (parms
))
3556 /* A parameter with a default argument. */
3559 ++min_arity
, ++max_arity
;
3561 if (ia
< (unsigned)min_arity
)
3563 /* Too few arguments. */
3564 reason
= arity_rejection (NULL_TREE
, min_arity
, ia
,
3565 /*least_p=*/(max_arity
== -1));
3568 else if (max_arity
!= -1 && ia
> (unsigned)max_arity
)
3570 /* Too many arguments. */
3571 reason
= arity_rejection (NULL_TREE
, max_arity
, ia
);
3575 convs
= alloc_conversions (nargs
);
3577 if (shortcut_bad_convs
3578 && DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl
)
3579 && !DECL_CONSTRUCTOR_P (tmpl
))
3581 /* Check the 'this' conversion before proceeding with deduction.
3582 This is effectively an extension of the DR 1391 resolution
3583 that we perform in check_non_deducible_conversions, though it's
3584 convenient to do this extra check here instead of there. */
3585 tree parmtype
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl
)));
3586 tree argtype
= lvalue_type (first_arg
);
3587 tree arg
= first_arg
;
3588 conversion
*t
= build_this_conversion (tmpl
, ctype
,
3589 parmtype
, argtype
, arg
,
3594 reason
= bad_arg_conversion_rejection (first_arg
, 0,
3596 EXPR_LOCATION (arg
));
3602 errs
= errorcount
+sorrycount
;
3603 fn
= fn_type_unification (tmpl
, explicit_targs
, targs
,
3604 args_without_in_chrg
,
3605 nargs_without_in_chrg
,
3606 return_type
, strict
, flags
, convs
,
3607 false, complain
& tf_decltype
);
3609 if (fn
== error_mark_node
)
3611 /* Don't repeat unification later if it already resulted in errors. */
3612 if (errorcount
+sorrycount
== errs
)
3613 reason
= template_unification_rejection (tmpl
, explicit_targs
,
3614 targs
, args_without_in_chrg
,
3615 nargs_without_in_chrg
,
3616 return_type
, strict
, flags
);
3618 reason
= template_unification_error_rejection ();
3622 /* Now the explicit specifier might have been deduced; check if this
3623 declaration is explicit. If it is and we're ignoring non-converting
3624 constructors, don't add this function to the set of candidates. */
3625 if (((flags
& (LOOKUP_ONLYCONVERTING
|LOOKUP_LIST_INIT_CTOR
))
3626 == LOOKUP_ONLYCONVERTING
)
3627 && DECL_NONCONVERTING_P (fn
))
3630 if (DECL_CONSTRUCTOR_P (fn
) && nargs
== 2)
3632 tree arg_types
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
3633 if (arg_types
&& same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types
)),
3636 /* We're trying to produce a constructor with a prohibited signature,
3637 as discussed above; handle here any cases we didn't catch then,
3639 reason
= invalid_copy_with_fn_template_rejection ();
3644 if (obj
!= NULL_TREE
)
3645 /* Aha, this is a conversion function. */
3646 cand
= add_conv_candidate (candidates
, fn
, obj
, arglist
,
3647 access_path
, conversion_path
, complain
);
3649 cand
= add_function_candidate (candidates
, fn
, ctype
,
3650 first_arg
, arglist
, access_path
,
3651 conversion_path
, flags
, convs
,
3652 shortcut_bad_convs
, complain
);
3653 if (DECL_TI_TEMPLATE (fn
) != tmpl
)
3654 /* This situation can occur if a member template of a template
3655 class is specialized. Then, instantiate_template might return
3656 an instantiation of the specialization, in which case the
3657 DECL_TI_TEMPLATE field will point at the original
3658 specialization. For example:
3660 template <class T> struct S { template <class U> void f(U);
3661 template <> void f(int) {}; };
3665 Here, TMPL will be template <class U> S<double>::f(U).
3666 And, instantiate template will give us the specialization
3667 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3668 for this will point at template <class T> template <> S<T>::f(int),
3669 so that we can find the definition. For the purposes of
3670 overload resolution, however, we want the original TMPL. */
3671 cand
->template_decl
= build_template_info (tmpl
, targs
);
3673 cand
->template_decl
= DECL_TEMPLATE_INFO (fn
);
3674 cand
->explicit_targs
= explicit_targs
;
3678 int viable
= (reason
->code
== rr_bad_arg_conversion
? -1 : 0);
3679 return add_candidate (candidates
, tmpl
, first_arg
, arglist
, nargs
, convs
,
3680 access_path
, conversion_path
, viable
, reason
, flags
);
3684 static struct z_candidate
*
3685 add_template_candidate (struct z_candidate
**candidates
, tree tmpl
, tree ctype
,
3686 tree explicit_targs
, tree first_arg
,
3687 const vec
<tree
, va_gc
> *arglist
, tree return_type
,
3688 tree access_path
, tree conversion_path
, int flags
,
3689 unification_kind_t strict
, bool shortcut_bad_convs
,
3690 tsubst_flags_t complain
)
3693 add_template_candidate_real (candidates
, tmpl
, ctype
,
3694 explicit_targs
, first_arg
, arglist
,
3695 return_type
, access_path
, conversion_path
,
3696 flags
, NULL_TREE
, strict
, shortcut_bad_convs
,
3700 /* Create an overload candidate for the conversion function template TMPL,
3701 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3702 pointer-to-function which will in turn be called with the argument list
3703 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3704 passed on to implicit_conversion. */
3706 static struct z_candidate
*
3707 add_template_conv_candidate (struct z_candidate
**candidates
, tree tmpl
,
3709 const vec
<tree
, va_gc
> *arglist
,
3710 tree return_type
, tree access_path
,
3711 tree conversion_path
, tsubst_flags_t complain
)
3714 add_template_candidate_real (candidates
, tmpl
, NULL_TREE
, NULL_TREE
,
3715 NULL_TREE
, arglist
, return_type
, access_path
,
3716 conversion_path
, 0, obj
, DEDUCE_CALL
,
3717 /*shortcut_bad_convs=*/false, complain
);
3720 /* The CANDS are the set of candidates that were considered for
3721 overload resolution. Return the set of viable candidates, or CANDS
3722 if none are viable. If any of the candidates were viable, set
3723 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3724 considered viable only if it is strictly viable. */
3726 static struct z_candidate
*
3727 splice_viable (struct z_candidate
*cands
,
3731 struct z_candidate
*viable
;
3732 struct z_candidate
**last_viable
;
3733 struct z_candidate
**cand
;
3734 bool found_strictly_viable
= false;
3736 /* Be strict inside templates, since build_over_call won't actually
3737 do the conversions to get pedwarns. */
3738 if (processing_template_decl
)
3742 last_viable
= &viable
;
3743 *any_viable_p
= false;
3748 struct z_candidate
*c
= *cand
;
3750 && (c
->viable
== 1 || TREE_CODE (c
->fn
) == TEMPLATE_DECL
))
3752 /* Be strict in the presence of a viable candidate. Also if
3753 there are template candidates, so that we get deduction errors
3754 for them instead of silently preferring a bad conversion. */
3756 if (viable
&& !found_strictly_viable
)
3758 /* Put any spliced near matches back onto the main list so
3759 that we see them if there is no strict match. */
3760 *any_viable_p
= false;
3761 *last_viable
= cands
;
3764 last_viable
= &viable
;
3768 if (strict_p
? c
->viable
== 1 : c
->viable
)
3773 last_viable
= &c
->next
;
3774 *any_viable_p
= true;
3776 found_strictly_viable
= true;
3782 return viable
? viable
: cands
;
3786 any_strictly_viable (struct z_candidate
*cands
)
3788 for (; cands
; cands
= cands
->next
)
3789 if (cands
->viable
== 1)
3794 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3795 words, it is about to become the "this" pointer for a member
3796 function call. Take the address of the object. */
3799 build_this (tree obj
)
3801 /* In a template, we are only concerned about the type of the
3802 expression, so we can take a shortcut. */
3803 if (processing_template_decl
)
3804 return build_address (obj
);
3806 return cp_build_addr_expr (obj
, tf_warning_or_error
);
3809 /* Returns true iff functions are equivalent. Equivalent functions are
3810 not '==' only if one is a function-local extern function or if
3811 both are extern "C". */
3814 equal_functions (tree fn1
, tree fn2
)
3816 if (TREE_CODE (fn1
) != TREE_CODE (fn2
))
3818 if (TREE_CODE (fn1
) == TEMPLATE_DECL
)
3820 if (DECL_LOCAL_DECL_P (fn1
) || DECL_LOCAL_DECL_P (fn2
)
3821 || DECL_EXTERN_C_FUNCTION_P (fn1
))
3822 return decls_match (fn1
, fn2
);
3826 /* Print information about a candidate FN being rejected due to INFO. */
3829 print_conversion_rejection (location_t loc
, struct conversion_info
*info
,
3832 tree from
= info
->from
;
3834 from
= lvalue_type (from
);
3835 if (info
->n_arg
== -1)
3837 /* Conversion of implicit `this' argument failed. */
3838 if (!TYPE_P (info
->from
))
3839 /* A bad conversion for 'this' must be discarding cv-quals. */
3840 inform (loc
, " passing %qT as %<this%> "
3841 "argument discards qualifiers",
3844 inform (loc
, " no known conversion for implicit "
3845 "%<this%> parameter from %qH to %qI",
3846 from
, info
->to_type
);
3848 else if (!TYPE_P (info
->from
))
3850 if (info
->n_arg
>= 0)
3851 inform (loc
, " conversion of argument %d would be ill-formed:",
3853 iloc_sentinel ils
= loc
;
3854 perform_implicit_conversion (info
->to_type
, info
->from
,
3855 tf_warning_or_error
);
3857 else if (info
->n_arg
== -2)
3858 /* Conversion of conversion function return value failed. */
3859 inform (loc
, " no known conversion from %qH to %qI",
3860 from
, info
->to_type
);
3863 if (TREE_CODE (fn
) == FUNCTION_DECL
)
3864 loc
= get_fndecl_argument_location (fn
, info
->n_arg
);
3865 inform (loc
, " no known conversion for argument %d from %qH to %qI",
3866 info
->n_arg
+ 1, from
, info
->to_type
);
3870 /* Print information about a candidate with WANT parameters and we found
3874 print_arity_information (location_t loc
, unsigned int have
, unsigned int want
,
3878 inform_n (loc
, want
,
3879 " candidate expects at least %d argument, %d provided",
3880 " candidate expects at least %d arguments, %d provided",
3883 inform_n (loc
, want
,
3884 " candidate expects %d argument, %d provided",
3885 " candidate expects %d arguments, %d provided",
3889 /* Print information about one overload candidate CANDIDATE. MSGSTR
3890 is the text to print before the candidate itself.
3892 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3893 to have been run through gettext by the caller. This wart makes
3894 life simpler in print_z_candidates and for the translators. */
3897 print_z_candidate (location_t loc
, const char *msgstr
,
3898 struct z_candidate
*candidate
)
3900 const char *msg
= (msgstr
== NULL
3902 : ACONCAT ((_(msgstr
), " ", NULL
)));
3903 tree fn
= candidate
->fn
;
3904 if (flag_new_inheriting_ctors
)
3905 fn
= strip_inheriting_ctors (fn
);
3906 location_t cloc
= location_of (fn
);
3908 if (identifier_p (fn
))
3911 if (candidate
->num_convs
== 3)
3912 inform (cloc
, "%s%<%D(%T, %T, %T)%> (built-in)", msg
, fn
,
3913 candidate
->convs
[0]->type
,
3914 candidate
->convs
[1]->type
,
3915 candidate
->convs
[2]->type
);
3916 else if (candidate
->num_convs
== 2)
3917 inform (cloc
, "%s%<%D(%T, %T)%> (built-in)", msg
, fn
,
3918 candidate
->convs
[0]->type
,
3919 candidate
->convs
[1]->type
);
3921 inform (cloc
, "%s%<%D(%T)%> (built-in)", msg
, fn
,
3922 candidate
->convs
[0]->type
);
3924 else if (TYPE_P (fn
))
3925 inform (cloc
, "%s%qT (conversion)", msg
, fn
);
3926 else if (candidate
->viable
== -1)
3927 inform (cloc
, "%s%#qD (near match)", msg
, fn
);
3928 else if (DECL_DELETED_FN (fn
))
3929 inform (cloc
, "%s%#qD (deleted)", msg
, fn
);
3930 else if (candidate
->reversed ())
3931 inform (cloc
, "%s%#qD (reversed)", msg
, fn
);
3932 else if (candidate
->rewritten ())
3933 inform (cloc
, "%s%#qD (rewritten)", msg
, fn
);
3935 inform (cloc
, "%s%#qD", msg
, fn
);
3936 if (fn
!= candidate
->fn
)
3938 cloc
= location_of (candidate
->fn
);
3939 inform (cloc
, " inherited here");
3941 /* Give the user some information about why this candidate failed. */
3942 if (candidate
->reason
!= NULL
)
3944 struct rejection_reason
*r
= candidate
->reason
;
3949 print_arity_information (cloc
, r
->u
.arity
.actual
,
3950 r
->u
.arity
.expected
,
3951 r
->u
.arity
.least_p
);
3953 case rr_arg_conversion
:
3954 print_conversion_rejection (cloc
, &r
->u
.conversion
, fn
);
3956 case rr_bad_arg_conversion
:
3957 print_conversion_rejection (cloc
, &r
->u
.bad_conversion
, fn
);
3959 case rr_explicit_conversion
:
3960 inform (cloc
, " return type %qT of explicit conversion function "
3961 "cannot be converted to %qT with a qualification "
3962 "conversion", r
->u
.conversion
.from
,
3963 r
->u
.conversion
.to_type
);
3965 case rr_template_conversion
:
3966 inform (cloc
, " conversion from return type %qT of template "
3967 "conversion function specialization to %qT is not an "
3968 "exact match", r
->u
.conversion
.from
,
3969 r
->u
.conversion
.to_type
);
3971 case rr_template_unification
:
3972 /* We use template_unification_error_rejection if unification caused
3973 actual non-SFINAE errors, in which case we don't need to repeat
3975 if (r
->u
.template_unification
.tmpl
== NULL_TREE
)
3977 inform (cloc
, " substitution of deduced template arguments "
3978 "resulted in errors seen above");
3981 /* Re-run template unification with diagnostics. */
3982 inform (cloc
, " template argument deduction/substitution failed:");
3983 fn_type_unification (r
->u
.template_unification
.tmpl
,
3984 r
->u
.template_unification
.explicit_targs
,
3986 (r
->u
.template_unification
.num_targs
)),
3987 r
->u
.template_unification
.args
,
3988 r
->u
.template_unification
.nargs
,
3989 r
->u
.template_unification
.return_type
,
3990 r
->u
.template_unification
.strict
,
3991 r
->u
.template_unification
.flags
,
3994 case rr_invalid_copy
:
3996 " a constructor taking a single argument of its own "
3997 "class type is invalid");
3999 case rr_constraint_failure
:
4000 diagnose_constraints (cloc
, fn
, NULL_TREE
);
4002 case rr_inherited_ctor
:
4003 inform (cloc
, " an inherited constructor is not a candidate for "
4004 "initialization from an expression of the same or derived "
4009 /* This candidate didn't have any issues or we failed to
4010 handle a particular code. Either way... */
4017 print_z_candidates (location_t loc
, struct z_candidate
*candidates
)
4019 struct z_candidate
*cand1
;
4020 struct z_candidate
**cand2
;
4025 /* Remove non-viable deleted candidates. */
4027 for (cand2
= &cand1
; *cand2
; )
4029 if (TREE_CODE ((*cand2
)->fn
) == FUNCTION_DECL
4030 && !(*cand2
)->viable
4031 && DECL_DELETED_FN ((*cand2
)->fn
))
4032 *cand2
= (*cand2
)->next
;
4034 cand2
= &(*cand2
)->next
;
4036 /* ...if there are any non-deleted ones. */
4040 /* There may be duplicates in the set of candidates. We put off
4041 checking this condition as long as possible, since we have no way
4042 to eliminate duplicates from a set of functions in less than n^2
4043 time. Now we are about to emit an error message, so it is more
4044 permissible to go slowly. */
4045 for (cand1
= candidates
; cand1
; cand1
= cand1
->next
)
4047 tree fn
= cand1
->fn
;
4048 /* Skip builtin candidates and conversion functions. */
4051 cand2
= &cand1
->next
;
4054 if (DECL_P ((*cand2
)->fn
)
4055 && equal_functions (fn
, (*cand2
)->fn
))
4056 *cand2
= (*cand2
)->next
;
4058 cand2
= &(*cand2
)->next
;
4062 for (; candidates
; candidates
= candidates
->next
)
4063 print_z_candidate (loc
, N_("candidate:"), candidates
);
4066 /* USER_SEQ is a user-defined conversion sequence, beginning with a
4067 USER_CONV. STD_SEQ is the standard conversion sequence applied to
4068 the result of the conversion function to convert it to the final
4069 desired type. Merge the two sequences into a single sequence,
4070 and return the merged sequence. */
4073 merge_conversion_sequences (conversion
*user_seq
, conversion
*std_seq
)
4076 bool bad
= user_seq
->bad_p
;
4078 gcc_assert (user_seq
->kind
== ck_user
);
4080 /* Find the end of the second conversion sequence. */
4081 for (t
= &std_seq
; (*t
)->kind
!= ck_identity
; t
= &((*t
)->u
.next
))
4083 /* The entire sequence is a user-conversion sequence. */
4084 (*t
)->user_conv_p
= true;
4089 if ((*t
)->rvaluedness_matches_p
)
4090 /* We're binding a reference directly to the result of the conversion.
4091 build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4092 type, but we want it back. */
4093 user_seq
->type
= TREE_TYPE (TREE_TYPE (user_seq
->cand
->fn
));
4095 /* Replace the identity conversion with the user conversion
4102 /* Handle overload resolution for initializing an object of class type from
4103 an initializer list. First we look for a suitable constructor that
4104 takes a std::initializer_list; if we don't find one, we then look for a
4105 non-list constructor.
4107 Parameters are as for add_candidates, except that the arguments are in
4108 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4109 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4112 add_list_candidates (tree fns
, tree first_arg
,
4113 const vec
<tree
, va_gc
> *args
, tree totype
,
4114 tree explicit_targs
, bool template_only
,
4115 tree conversion_path
, tree access_path
,
4117 struct z_candidate
**candidates
,
4118 tsubst_flags_t complain
)
4120 gcc_assert (*candidates
== NULL
);
4122 /* We're looking for a ctor for list-initialization. */
4123 flags
|= LOOKUP_LIST_INIT_CTOR
;
4124 /* And we don't allow narrowing conversions. We also use this flag to
4125 avoid the copy constructor call for copy-list-initialization. */
4126 flags
|= LOOKUP_NO_NARROWING
;
4128 unsigned nart
= num_artificial_parms_for (OVL_FIRST (fns
)) - 1;
4129 tree init_list
= (*args
)[nart
];
4131 /* Always use the default constructor if the list is empty (DR 990). */
4132 if (CONSTRUCTOR_NELTS (init_list
) == 0
4133 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype
))
4135 else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list
)
4136 && !CP_AGGREGATE_TYPE_P (totype
))
4138 if (complain
& tf_error
)
4139 error ("designated initializers cannot be used with a "
4140 "non-aggregate type %qT", totype
);
4143 /* If the class has a list ctor, try passing the list as a single
4144 argument first, but only consider list ctors. */
4145 else if (TYPE_HAS_LIST_CTOR (totype
))
4147 flags
|= LOOKUP_LIST_ONLY
;
4148 add_candidates (fns
, first_arg
, args
, NULL_TREE
,
4149 explicit_targs
, template_only
, conversion_path
,
4150 access_path
, flags
, candidates
, complain
);
4151 if (any_strictly_viable (*candidates
))
4155 /* Expand the CONSTRUCTOR into a new argument vec. */
4156 vec
<tree
, va_gc
> *new_args
;
4157 vec_alloc (new_args
, nart
+ CONSTRUCTOR_NELTS (init_list
));
4158 for (unsigned i
= 0; i
< nart
; ++i
)
4159 new_args
->quick_push ((*args
)[i
]);
4160 for (unsigned i
= 0; i
< CONSTRUCTOR_NELTS (init_list
); ++i
)
4161 new_args
->quick_push (CONSTRUCTOR_ELT (init_list
, i
)->value
);
4163 /* We aren't looking for list-ctors anymore. */
4164 flags
&= ~LOOKUP_LIST_ONLY
;
4165 /* We allow more user-defined conversions within an init-list. */
4166 flags
&= ~LOOKUP_NO_CONVERSION
;
4168 add_candidates (fns
, first_arg
, new_args
, NULL_TREE
,
4169 explicit_targs
, template_only
, conversion_path
,
4170 access_path
, flags
, candidates
, complain
);
4173 /* Given C(std::initializer_list<A>), return A. */
4176 list_ctor_element_type (tree fn
)
4178 gcc_checking_assert (is_list_ctor (fn
));
4180 tree parm
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
4181 parm
= non_reference (TREE_VALUE (parm
));
4182 return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
4185 /* If EXPR is a braced-init-list where the elements all decay to the same type,
4186 return that type. */
4189 braced_init_element_type (tree expr
)
4191 if (TREE_CODE (expr
) == CONSTRUCTOR
4192 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
4193 return TREE_TYPE (TREE_TYPE (expr
));
4194 if (!BRACE_ENCLOSED_INITIALIZER_P (expr
))
4197 tree elttype
= NULL_TREE
;
4198 for (constructor_elt
&e
: CONSTRUCTOR_ELTS (expr
))
4200 tree type
= TREE_TYPE (e
.value
);
4201 type
= type_decays_to (type
);
4204 else if (!same_type_p (type
, elttype
))
4210 /* True iff EXPR contains any temporaries with non-trivial destruction.
4212 ??? Also ignore classes with non-trivial but no-op destruction other than
4216 has_non_trivial_temporaries (tree expr
)
4218 auto_vec
<tree
*> temps
;
4219 cp_walk_tree_without_duplicates (&expr
, find_temps_r
, &temps
);
4220 for (tree
*p
: temps
)
4222 tree t
= TREE_TYPE (*p
);
4223 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t
)
4224 && !is_std_allocator (t
))
4230 /* We're initializing an array of ELTTYPE from INIT. If it seems useful,
4231 return INIT as an array (of its own type) so the caller can initialize the
4232 target array in a loop. */
4235 maybe_init_list_as_array (tree elttype
, tree init
)
4237 /* Only do this if the array can go in rodata but not once converted. */
4238 if (!TYPE_NON_AGGREGATE_CLASS (elttype
))
4240 tree init_elttype
= braced_init_element_type (init
);
4241 if (!init_elttype
|| !SCALAR_TYPE_P (init_elttype
) || !TREE_CONSTANT (init
))
4244 /* Check with a stub expression to weed out special cases, and check whether
4245 we call the same function for direct-init as copy-list-init. */
4246 conversion_obstack_sentinel cos
;
4247 tree arg
= build_stub_object (init_elttype
);
4248 conversion
*c
= implicit_conversion (elttype
, init_elttype
, arg
, false,
4249 LOOKUP_NORMAL
, tf_none
);
4250 if (c
&& c
->kind
== ck_rvalue
)
4251 c
= next_conversion (c
);
4252 if (!c
|| c
->kind
!= ck_user
)
4255 tree first
= CONSTRUCTOR_ELT (init
, 0)->value
;
4256 conversion
*fc
= implicit_conversion (elttype
, init_elttype
, first
, false,
4257 LOOKUP_IMPLICIT
|LOOKUP_NO_NARROWING
,
4259 if (fc
&& fc
->kind
== ck_rvalue
)
4260 fc
= next_conversion (fc
);
4261 if (!fc
|| fc
->kind
!= ck_user
|| fc
->cand
->fn
!= c
->cand
->fn
)
4263 first
= convert_like (fc
, first
, tf_none
);
4264 if (first
== error_mark_node
)
4265 /* Let the normal code give the error. */
4268 /* Don't do this if the conversion would be constant. */
4269 first
= maybe_constant_init (first
);
4270 if (TREE_CONSTANT (first
))
4273 /* We can't do this if the conversion creates temporaries that need
4274 to live until the whole array is initialized. */
4275 if (has_non_trivial_temporaries (first
))
4278 /* We can't do this if copying from the initializer_list would be
4280 tree copy_argtypes
= make_tree_vec (1);
4281 TREE_VEC_ELT (copy_argtypes
, 0)
4282 = cp_build_qualified_type (elttype
, TYPE_QUAL_CONST
);
4283 if (!is_xible (INIT_EXPR
, elttype
, copy_argtypes
))
4286 init_elttype
= cp_build_qualified_type (init_elttype
, TYPE_QUAL_CONST
);
4287 tree arr
= build_array_of_n_type (init_elttype
, CONSTRUCTOR_NELTS (init
));
4288 arr
= finish_compound_literal (arr
, init
, tf_none
);
4289 DECL_MERGEABLE (TARGET_EXPR_SLOT (arr
)) = true;
4293 /* If we were going to call e.g. vector(initializer_list<string>) starting
4294 with a list of string-literals (which is inefficient, see PR105838),
4295 instead build an array of const char* and pass it to the range constructor.
4296 But only do this for standard library types, where we can assume the
4297 transformation makes sense.
4299 Really the container classes should have initializer_list<U> constructors to
4300 get the same effect more simply; this is working around that lack. */
4303 maybe_init_list_as_range (tree fn
, tree expr
)
4305 if (!processing_template_decl
4306 && BRACE_ENCLOSED_INITIALIZER_P (expr
)
4307 && is_list_ctor (fn
)
4308 && decl_in_std_namespace_p (fn
))
4310 tree to
= list_ctor_element_type (fn
);
4311 if (tree init
= maybe_init_list_as_array (to
, expr
))
4313 tree begin
= decay_conversion (TARGET_EXPR_SLOT (init
), tf_none
);
4314 tree nelts
= array_type_nelts_top (TREE_TYPE (init
));
4315 tree end
= cp_build_binary_op (input_location
, PLUS_EXPR
, begin
,
4317 begin
= cp_build_compound_expr (init
, begin
, tf_none
);
4318 return build_constructor_va (init_list_type_node
, 2,
4319 NULL_TREE
, begin
, NULL_TREE
, end
);
4326 /* Returns the best overload candidate to perform the requested
4327 conversion. This function is used for three the overloading situations
4328 described in [over.match.copy], [over.match.conv], and [over.match.ref].
4329 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4330 per [dcl.init.ref], so we ignore temporary bindings. */
4332 static struct z_candidate
*
4333 build_user_type_conversion_1 (tree totype
, tree expr
, int flags
,
4334 tsubst_flags_t complain
)
4336 struct z_candidate
*candidates
, *cand
;
4338 tree ctors
= NULL_TREE
;
4339 tree conv_fns
= NULL_TREE
;
4340 conversion
*conv
= NULL
;
4341 tree first_arg
= NULL_TREE
;
4342 vec
<tree
, va_gc
> *args
= NULL
;
4349 fromtype
= TREE_TYPE (expr
);
4351 /* We represent conversion within a hierarchy using RVALUE_CONV and
4352 BASE_CONV, as specified by [over.best.ics]; these become plain
4353 constructor calls, as specified in [dcl.init]. */
4354 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype
) || !MAYBE_CLASS_TYPE_P (totype
)
4355 || !DERIVED_FROM_P (totype
, fromtype
));
4357 if (CLASS_TYPE_P (totype
))
4358 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4359 creating a garbage BASELINK; constructors can't be inherited. */
4360 ctors
= get_class_binding (totype
, complete_ctor_identifier
);
4362 tree to_nonref
= non_reference (totype
);
4363 if (MAYBE_CLASS_TYPE_P (fromtype
))
4365 if (same_type_ignoring_top_level_qualifiers_p (to_nonref
, fromtype
) ||
4366 (CLASS_TYPE_P (to_nonref
) && CLASS_TYPE_P (fromtype
)
4367 && DERIVED_FROM_P (to_nonref
, fromtype
)))
4369 /* [class.conv.fct] A conversion function is never used to
4370 convert a (possibly cv-qualified) object to the (possibly
4371 cv-qualified) same object type (or a reference to it), to a
4372 (possibly cv-qualified) base class of that type (or a
4373 reference to it)... */
4376 conv_fns
= lookup_conversions (fromtype
);
4380 flags
|= LOOKUP_NO_CONVERSION
;
4381 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
4382 flags
|= LOOKUP_NO_NARROWING
;
4383 /* Prevent add_candidates from treating a non-strictly viable candidate
4385 complain
|= tf_conv
;
4387 /* It's OK to bind a temporary for converting constructor arguments, but
4388 not in converting the return value of a conversion operator. */
4389 convflags
= ((flags
& LOOKUP_NO_TEMP_BIND
) | LOOKUP_NO_CONVERSION
4390 | (flags
& LOOKUP_NO_NARROWING
));
4391 flags
&= ~LOOKUP_NO_TEMP_BIND
;
4395 int ctorflags
= flags
;
4397 first_arg
= build_dummy_object (totype
);
4399 /* We should never try to call the abstract or base constructor
4401 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors
))
4402 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors
)));
4404 args
= make_tree_vector_single (expr
);
4405 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
4407 /* List-initialization. */
4408 add_list_candidates (ctors
, first_arg
, args
, totype
, NULL_TREE
,
4409 false, TYPE_BINFO (totype
), TYPE_BINFO (totype
),
4410 ctorflags
, &candidates
, complain
);
4414 add_candidates (ctors
, first_arg
, args
, NULL_TREE
, NULL_TREE
, false,
4415 TYPE_BINFO (totype
), TYPE_BINFO (totype
),
4416 ctorflags
, &candidates
, complain
);
4419 for (cand
= candidates
; cand
; cand
= cand
->next
)
4421 cand
->second_conv
= build_identity_conv (totype
, NULL_TREE
);
4423 /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4424 set, then this is copy-initialization. In that case, "The
4425 result of the call is then used to direct-initialize the
4426 object that is the destination of the copy-initialization."
4429 We represent this in the conversion sequence with an
4430 rvalue conversion, which means a constructor call. */
4431 if (!TYPE_REF_P (totype
)
4432 && cxx_dialect
< cxx17
4433 && (flags
& LOOKUP_ONLYCONVERTING
)
4434 && !(convflags
& LOOKUP_NO_TEMP_BIND
))
4436 = build_conv (ck_rvalue
, totype
, cand
->second_conv
);
4442 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
4443 first_arg
= CONSTRUCTOR_ELT (expr
, 0)->value
;
4448 for (; conv_fns
; conv_fns
= TREE_CHAIN (conv_fns
))
4450 tree conversion_path
= TREE_PURPOSE (conv_fns
);
4451 struct z_candidate
*old_candidates
;
4453 /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4454 would need an addional user-defined conversion, i.e. if the return
4455 type differs in class-ness from the desired type. So we avoid
4456 considering operator bool when calling a copy constructor.
4458 This optimization avoids the failure in PR97600, and is allowed by
4459 [temp.inst]/9: "If the function selected by overload resolution can be
4460 determined without instantiating a class template definition, it is
4461 unspecified whether that instantiation actually takes place." */
4462 tree convtype
= non_reference (TREE_TYPE (conv_fns
));
4463 if ((flags
& LOOKUP_NO_CONVERSION
)
4464 && !WILDCARD_TYPE_P (convtype
)
4465 && (CLASS_TYPE_P (to_nonref
)
4466 != CLASS_TYPE_P (convtype
)))
4469 /* If we are called to convert to a reference type, we are trying to
4470 find a direct binding, so don't even consider temporaries. If
4471 we don't find a direct binding, the caller will try again to
4472 look for a temporary binding. */
4473 if (TYPE_REF_P (totype
))
4474 convflags
|= LOOKUP_NO_TEMP_BIND
;
4476 old_candidates
= candidates
;
4477 add_candidates (TREE_VALUE (conv_fns
), first_arg
, NULL
, totype
,
4479 conversion_path
, TYPE_BINFO (fromtype
),
4480 flags
, &candidates
, complain
);
4482 for (cand
= candidates
; cand
!= old_candidates
; cand
= cand
->next
)
4484 if (cand
->viable
== 0)
4485 /* Already rejected, don't change to -1. */
4488 tree rettype
= TREE_TYPE (TREE_TYPE (cand
->fn
));
4490 = implicit_conversion (totype
,
4493 /*c_cast_p=*/false, convflags
,
4496 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4497 copy-initialization. In that case, "The result of the
4498 call is then used to direct-initialize the object that is
4499 the destination of the copy-initialization." [dcl.init]
4501 We represent this in the conversion sequence with an
4502 rvalue conversion, which means a constructor call. But
4503 don't add a second rvalue conversion if there's already
4504 one there. Which there really shouldn't be, but it's
4505 harmless since we'd add it here anyway. */
4506 if (ics
&& MAYBE_CLASS_TYPE_P (totype
) && ics
->kind
!= ck_rvalue
4507 && !(convflags
& LOOKUP_NO_TEMP_BIND
))
4508 ics
= build_conv (ck_rvalue
, totype
, ics
);
4510 cand
->second_conv
= ics
;
4515 cand
->reason
= arg_conversion_rejection (NULL_TREE
, -2,
4517 EXPR_LOCATION (expr
));
4519 else if (TYPE_REF_P (totype
) && !ics
->rvaluedness_matches_p
4520 /* Limit this to non-templates for now (PR90546). */
4521 && !cand
->template_decl
4522 && TREE_CODE (TREE_TYPE (totype
)) != FUNCTION_TYPE
)
4524 /* If we are called to convert to a reference type, we are trying
4525 to find a direct binding per [over.match.ref], so rvaluedness
4526 must match for non-functions. */
4529 else if (DECL_NONCONVERTING_P (cand
->fn
)
4530 && ics
->rank
> cr_exact
)
4532 /* 13.3.1.5: For direct-initialization, those explicit
4533 conversion functions that are not hidden within S and
4534 yield type T or a type that can be converted to type T
4535 with a qualification conversion (4.4) are also candidate
4537 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4538 I've raised this issue with the committee. --jason 9/2011 */
4540 cand
->reason
= explicit_conversion_rejection (rettype
, totype
);
4542 else if (cand
->viable
== 1 && ics
->bad_p
)
4546 = bad_arg_conversion_rejection (NULL_TREE
, -2,
4548 EXPR_LOCATION (expr
));
4550 else if (primary_template_specialization_p (cand
->fn
)
4551 && ics
->rank
> cr_exact
)
4553 /* 13.3.3.1.2: If the user-defined conversion is specified by
4554 a specialization of a conversion function template, the
4555 second standard conversion sequence shall have exact match
4558 cand
->reason
= template_conversion_rejection (rettype
, totype
);
4563 candidates
= splice_viable (candidates
, false, &any_viable_p
);
4567 release_tree_vector (args
);
4571 cand
= tourney (candidates
, complain
);
4574 if (complain
& tf_error
)
4576 auto_diagnostic_group d
;
4577 error_at (cp_expr_loc_or_input_loc (expr
),
4578 "conversion from %qH to %qI is ambiguous",
4580 print_z_candidates (location_of (expr
), candidates
);
4583 cand
= candidates
; /* any one will do */
4584 cand
->second_conv
= build_ambiguous_conv (totype
, expr
);
4585 cand
->second_conv
->user_conv_p
= true;
4586 if (!any_strictly_viable (candidates
))
4587 cand
->second_conv
->bad_p
= true;
4588 if (flags
& LOOKUP_ONLYCONVERTING
)
4589 cand
->second_conv
->need_temporary_p
= true;
4590 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4591 ambiguous conversion is no worse than another user-defined
4597 /* Maybe pass { } as iterators instead of an initializer_list. */
4598 if (tree iters
= maybe_init_list_as_range (cand
->fn
, expr
))
4599 if (z_candidate
*cand2
4600 = build_user_type_conversion_1 (totype
, iters
, flags
, tf_none
))
4601 if (cand2
->viable
== 1 && !is_list_ctor (cand2
->fn
))
4608 if (!DECL_CONSTRUCTOR_P (cand
->fn
))
4609 convtype
= non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
)));
4610 else if (cand
->second_conv
->kind
== ck_rvalue
)
4611 /* DR 5: [in the first step of copy-initialization]...if the function
4612 is a constructor, the call initializes a temporary of the
4613 cv-unqualified version of the destination type. */
4614 convtype
= cv_unqualified (totype
);
4617 /* Build the user conversion sequence. */
4621 build_identity_conv (TREE_TYPE (expr
), expr
));
4623 if (cand
->viable
== -1)
4626 /* Remember that this was a list-initialization. */
4627 if (flags
& LOOKUP_NO_NARROWING
)
4628 conv
->check_narrowing
= true;
4630 /* Combine it with the second conversion sequence. */
4631 cand
->second_conv
= merge_conversion_sequences (conv
,
4637 /* Wrapper for above. */
4640 build_user_type_conversion (tree totype
, tree expr
, int flags
,
4641 tsubst_flags_t complain
)
4643 struct z_candidate
*cand
;
4646 auto_cond_timevar
tv (TV_OVERLOAD
);
4648 conversion_obstack_sentinel cos
;
4650 cand
= build_user_type_conversion_1 (totype
, expr
, flags
, complain
);
4654 if (cand
->second_conv
->kind
== ck_ambig
)
4655 ret
= error_mark_node
;
4658 expr
= convert_like (cand
->second_conv
, expr
, complain
);
4659 ret
= convert_from_reference (expr
);
4668 /* Give a helpful diagnostic when implicit_conversion fails. */
4671 implicit_conversion_error (location_t loc
, tree type
, tree expr
)
4673 tsubst_flags_t complain
= tf_warning_or_error
;
4675 /* If expr has unknown type, then it is an overloaded function.
4676 Call instantiate_type to get good error messages. */
4677 if (TREE_TYPE (expr
) == unknown_type_node
)
4678 instantiate_type (type
, expr
, complain
);
4679 else if (invalid_nonstatic_memfn_p (loc
, expr
, complain
))
4680 /* We gave an error. */;
4681 else if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
4682 && CONSTRUCTOR_IS_DESIGNATED_INIT (expr
)
4683 && !CP_AGGREGATE_TYPE_P (type
))
4684 error_at (loc
, "designated initializers cannot be used with a "
4685 "non-aggregate type %qT", type
);
4688 range_label_for_type_mismatch
label (TREE_TYPE (expr
), type
);
4689 gcc_rich_location
rich_loc (loc
, &label
);
4690 error_at (&rich_loc
, "could not convert %qE from %qH to %qI",
4691 expr
, TREE_TYPE (expr
), type
);
4695 /* Worker for build_converted_constant_expr. */
4698 build_converted_constant_expr_internal (tree type
, tree expr
,
4699 int flags
, tsubst_flags_t complain
)
4703 location_t loc
= cp_expr_loc_or_input_loc (expr
);
4705 if (error_operand_p (expr
))
4706 return error_mark_node
;
4708 conversion_obstack_sentinel cos
;
4710 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
4711 /*c_cast_p=*/false, flags
, complain
);
4713 /* A converted constant expression of type T is an expression, implicitly
4714 converted to type T, where the converted expression is a constant
4715 expression and the implicit conversion sequence contains only
4717 * user-defined conversions,
4718 * lvalue-to-rvalue conversions (7.1),
4719 * array-to-pointer conversions (7.2),
4720 * function-to-pointer conversions (7.3),
4721 * qualification conversions (7.5),
4722 * integral promotions (7.6),
4723 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4724 * null pointer conversions (7.11) from std::nullptr_t,
4725 * null member pointer conversions (7.12) from std::nullptr_t, and
4726 * function pointer conversions (7.13),
4728 and where the reference binding (if any) binds directly. */
4730 for (conversion
*c
= conv
;
4731 c
&& c
->kind
!= ck_identity
;
4732 c
= next_conversion (c
))
4736 /* A conversion function is OK. If it isn't constexpr, we'll
4737 complain later that the argument isn't constant. */
4739 /* List-initialization is OK. */
4741 /* The lvalue-to-rvalue conversion is OK. */
4743 /* Array-to-pointer and function-to-pointer. */
4745 /* Function pointer conversions. */
4747 /* Qualification conversions. */
4752 if (c
->need_temporary_p
)
4754 if (complain
& tf_error
)
4755 error_at (loc
, "initializing %qH with %qI in converted "
4756 "constant expression does not bind directly",
4757 type
, next_conversion (c
)->type
);
4766 t
= next_conversion (c
)->type
;
4767 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t
)
4768 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4769 /* Integral promotion or conversion. */
4771 if (NULLPTR_TYPE_P (t
))
4772 /* Conversion from nullptr to pointer or pointer-to-member. */
4775 if (complain
& tf_error
)
4776 error_at (loc
, "conversion from %qH to %qI in a "
4777 "converted constant expression", t
, type
);
4786 /* Avoid confusing convert_nontype_argument by introducing
4787 a redundant conversion to the same reference type. */
4788 if (conv
&& conv
->kind
== ck_ref_bind
4789 && REFERENCE_REF_P (expr
))
4791 tree ref
= TREE_OPERAND (expr
, 0);
4792 if (same_type_p (type
, TREE_TYPE (ref
)))
4798 /* Don't copy a class in a template. */
4799 if (CLASS_TYPE_P (type
) && conv
->kind
== ck_rvalue
4800 && processing_template_decl
)
4801 conv
= next_conversion (conv
);
4803 /* Issuing conversion warnings for value-dependent expressions is
4804 likely too noisy. */
4805 warning_sentinel
w (warn_conversion
);
4806 conv
->check_narrowing
= true;
4807 conv
->check_narrowing_const_only
= true;
4808 expr
= convert_like (conv
, expr
, complain
);
4812 if (complain
& tf_error
)
4813 implicit_conversion_error (loc
, type
, expr
);
4814 expr
= error_mark_node
;
4820 /* Subroutine of convert_nontype_argument.
4822 EXPR is an expression used in a context that requires a converted
4823 constant-expression, such as a template non-type parameter. Do any
4824 necessary conversions (that are permitted for converted
4825 constant-expressions) to convert it to the desired type.
4827 This function doesn't consider explicit conversion functions. If
4828 you mean to use "a contextually converted constant expression of type
4829 bool", use build_converted_constant_bool_expr.
4831 If conversion is successful, returns the converted expression;
4832 otherwise, returns error_mark_node. */
4835 build_converted_constant_expr (tree type
, tree expr
, tsubst_flags_t complain
)
4837 return build_converted_constant_expr_internal (type
, expr
, LOOKUP_IMPLICIT
,
4841 /* Used to create "a contextually converted constant expression of type
4842 bool". This differs from build_converted_constant_expr in that it
4843 also considers explicit conversion functions. */
4846 build_converted_constant_bool_expr (tree expr
, tsubst_flags_t complain
)
4848 return build_converted_constant_expr_internal (boolean_type_node
, expr
,
4849 LOOKUP_NORMAL
, complain
);
4852 /* Do any initial processing on the arguments to a function call. */
4855 resolve_args (vec
<tree
, va_gc
> *args
, tsubst_flags_t complain
)
4860 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
4862 if (error_operand_p (arg
))
4864 else if (VOID_TYPE_P (TREE_TYPE (arg
)))
4866 if (complain
& tf_error
)
4867 error_at (cp_expr_loc_or_input_loc (arg
),
4868 "invalid use of void expression");
4871 else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg
), arg
, complain
))
4874 /* Force auto deduction now. Omit tf_warning to avoid redundant
4875 deprecated warning on deprecated-14.C. */
4876 if (!mark_single_function (arg
, complain
& ~tf_warning
))
4882 /* Perform overload resolution on FN, which is called with the ARGS.
4884 Return the candidate function selected by overload resolution, or
4885 NULL if the event that overload resolution failed. In the case
4886 that overload resolution fails, *CANDIDATES will be the set of
4887 candidates considered, and ANY_VIABLE_P will be set to true or
4888 false to indicate whether or not any of the candidates were
4891 The ARGS should already have gone through RESOLVE_ARGS before this
4892 function is called. */
4894 static struct z_candidate
*
4895 perform_overload_resolution (tree fn
,
4896 const vec
<tree
, va_gc
> *args
,
4897 struct z_candidate
**candidates
,
4898 bool *any_viable_p
, tsubst_flags_t complain
)
4900 struct z_candidate
*cand
;
4901 tree explicit_targs
;
4904 auto_cond_timevar
tv (TV_OVERLOAD
);
4906 explicit_targs
= NULL_TREE
;
4910 *any_viable_p
= true;
4913 gcc_assert (OVL_P (fn
) || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
);
4915 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4917 explicit_targs
= TREE_OPERAND (fn
, 1);
4918 fn
= TREE_OPERAND (fn
, 0);
4922 /* Add the various candidate functions. */
4923 add_candidates (fn
, NULL_TREE
, args
, NULL_TREE
,
4924 explicit_targs
, template_only
,
4925 /*conversion_path=*/NULL_TREE
,
4926 /*access_path=*/NULL_TREE
,
4928 candidates
, complain
);
4930 *candidates
= splice_viable (*candidates
, false, any_viable_p
);
4932 cand
= tourney (*candidates
, complain
);
4939 /* Print an error message about being unable to build a call to FN with
4940 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4941 be located; CANDIDATES is a possibly empty list of such
4945 print_error_for_call_failure (tree fn
, const vec
<tree
, va_gc
> *args
,
4946 struct z_candidate
*candidates
)
4948 tree targs
= NULL_TREE
;
4949 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4951 targs
= TREE_OPERAND (fn
, 1);
4952 fn
= TREE_OPERAND (fn
, 0);
4954 tree name
= OVL_NAME (fn
);
4955 location_t loc
= location_of (name
);
4957 name
= lookup_template_function (name
, targs
);
4959 auto_diagnostic_group d
;
4960 if (!any_strictly_viable (candidates
))
4961 error_at (loc
, "no matching function for call to %<%D(%A)%>",
4962 name
, build_tree_list_vec (args
));
4964 error_at (loc
, "call of overloaded %<%D(%A)%> is ambiguous",
4965 name
, build_tree_list_vec (args
));
4967 print_z_candidates (loc
, candidates
);
4970 /* Perform overload resolution on the set of deduction guides DGUIDES
4971 using ARGS. Returns the selected deduction guide, or error_mark_node
4972 if overload resolution fails. */
4975 perform_dguide_overload_resolution (tree dguides
, const vec
<tree
, va_gc
> *args
,
4976 tsubst_flags_t complain
)
4978 z_candidate
*candidates
;
4982 gcc_assert (deduction_guide_p (OVL_FIRST (dguides
)));
4984 conversion_obstack_sentinel cos
;
4986 z_candidate
*cand
= perform_overload_resolution (dguides
, args
, &candidates
,
4987 &any_viable_p
, complain
);
4990 if (complain
& tf_error
)
4991 print_error_for_call_failure (dguides
, args
, candidates
);
4992 result
= error_mark_node
;
5000 /* Return an expression for a call to FN (a namespace-scope function,
5001 or a static member function) with the ARGS. This may change
5005 build_new_function_call (tree fn
, vec
<tree
, va_gc
> **args
,
5006 tsubst_flags_t complain
)
5008 struct z_candidate
*candidates
, *cand
;
5012 if (args
!= NULL
&& *args
!= NULL
)
5014 *args
= resolve_args (*args
, complain
);
5016 return error_mark_node
;
5020 tm_malloc_replacement (fn
);
5022 conversion_obstack_sentinel cos
;
5024 cand
= perform_overload_resolution (fn
, *args
, &candidates
, &any_viable_p
,
5029 if (complain
& tf_error
)
5031 // If there is a single (non-viable) function candidate,
5032 // let the error be diagnosed by cp_build_function_call_vec.
5033 if (!any_viable_p
&& candidates
&& ! candidates
->next
5034 && (TREE_CODE (candidates
->fn
) == FUNCTION_DECL
))
5035 return cp_build_function_call_vec (candidates
->fn
, args
, complain
);
5037 // Otherwise, emit notes for non-viable candidates.
5038 print_error_for_call_failure (fn
, *args
, candidates
);
5040 result
= error_mark_node
;
5044 result
= build_over_call (cand
, LOOKUP_NORMAL
, complain
);
5049 && TREE_CODE (result
) == CALL_EXPR
5050 && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result
), 0))
5052 result
= coro_validate_builtin_call (result
);
5057 /* Build a call to a global operator new. FNNAME is the name of the
5058 operator (either "operator new" or "operator new[]") and ARGS are
5059 the arguments provided. This may change ARGS. *SIZE points to the
5060 total number of bytes required by the allocation, and is updated if
5061 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
5062 be used. If this function determines that no cookie should be
5063 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
5064 is not NULL_TREE, it is evaluated before calculating the final
5065 array size, and if it fails, the array size is replaced with
5066 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
5067 is non-NULL, it will be set, upon return, to the allocation
5071 build_operator_new_call (tree fnname
, vec
<tree
, va_gc
> **args
,
5072 tree
*size
, tree
*cookie_size
,
5073 tree align_arg
, tree size_check
,
5074 tree
*fn
, tsubst_flags_t complain
)
5076 tree original_size
= *size
;
5078 struct z_candidate
*candidates
;
5079 struct z_candidate
*cand
= NULL
;
5084 /* Set to (size_t)-1 if the size check fails. */
5085 if (size_check
!= NULL_TREE
)
5087 tree errval
= TYPE_MAX_VALUE (sizetype
);
5088 if (cxx_dialect
>= cxx11
&& flag_exceptions
)
5089 errval
= throw_bad_array_new_length ();
5090 *size
= fold_build3 (COND_EXPR
, sizetype
, size_check
,
5091 original_size
, errval
);
5093 vec_safe_insert (*args
, 0, *size
);
5094 *args
= resolve_args (*args
, complain
);
5096 return error_mark_node
;
5098 conversion_obstack_sentinel cos
;
5104 If this lookup fails to find the name, or if the allocated type
5105 is not a class type, the allocation function's name is looked
5106 up in the global scope.
5108 we disregard block-scope declarations of "operator new". */
5109 fns
= lookup_qualified_name (global_namespace
, fnname
);
5113 vec
<tree
, va_gc
>* align_args
5114 = vec_copy_and_insert (*args
, align_arg
, 1);
5115 cand
= perform_overload_resolution (fns
, align_args
, &candidates
,
5116 &any_viable_p
, tf_none
);
5119 /* If no aligned allocation function matches, try again without the
5123 /* Figure out what function is being called. */
5125 cand
= perform_overload_resolution (fns
, *args
, &candidates
, &any_viable_p
,
5128 /* If no suitable function could be found, issue an error message
5132 if (complain
& tf_error
)
5133 print_error_for_call_failure (fns
, *args
, candidates
);
5134 return error_mark_node
;
5137 /* If a cookie is required, add some extra space. Whether
5138 or not a cookie is required cannot be determined until
5139 after we know which function was called. */
5142 bool use_cookie
= true;
5145 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (cand
->fn
));
5146 /* Skip the size_t parameter. */
5147 arg_types
= TREE_CHAIN (arg_types
);
5148 /* Check the remaining parameters (if any). */
5150 && TREE_CHAIN (arg_types
) == void_list_node
5151 && same_type_p (TREE_VALUE (arg_types
),
5154 /* If we need a cookie, adjust the number of bytes allocated. */
5157 /* Update the total size. */
5158 *size
= size_binop (PLUS_EXPR
, original_size
, *cookie_size
);
5161 /* Set to (size_t)-1 if the size check fails. */
5162 gcc_assert (size_check
!= NULL_TREE
);
5163 *size
= fold_build3 (COND_EXPR
, sizetype
, size_check
,
5164 *size
, TYPE_MAX_VALUE (sizetype
));
5166 /* Update the argument list to reflect the adjusted size. */
5167 (**args
)[0] = *size
;
5170 *cookie_size
= NULL_TREE
;
5173 /* Tell our caller which function we decided to call. */
5177 /* Build the CALL_EXPR. */
5178 tree ret
= build_over_call (cand
, LOOKUP_NORMAL
, complain
);
5180 /* Set this flag for all callers of this function. In addition to
5181 new-expressions, this is called for allocating coroutine state; treat
5182 that as an implicit new-expression. */
5183 tree call
= extract_call_expr (ret
);
5184 if (TREE_CODE (call
) == CALL_EXPR
)
5185 CALL_FROM_NEW_OR_DELETE_P (call
) = 1;
5190 /* Evaluate side-effects from OBJ before evaluating call
5191 to FN in RESULT expression.
5192 This is for expressions of the form `obj->fn(...)'
5193 where `fn' turns out to be a static member function and
5194 `obj' needs to be evaluated. `fn' could be also static operator[]
5195 or static operator(), in which cases the source expression
5196 would be `obj[...]' or `obj(...)'. */
5199 keep_unused_object_arg (tree result
, tree obj
, tree fn
)
5201 if (result
== NULL_TREE
5202 || result
== error_mark_node
5203 || TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
5204 || !TREE_SIDE_EFFECTS (obj
))
5207 /* But avoid the implicit lvalue-rvalue conversion when `obj' is
5210 if (TREE_THIS_VOLATILE (a
))
5212 if (TREE_SIDE_EFFECTS (a
))
5213 return cp_build_compound_expr (a
, result
, tf_error
);
5217 /* Build a new call to operator(). This may change ARGS. */
5220 build_op_call (tree obj
, vec
<tree
, va_gc
> **args
, tsubst_flags_t complain
)
5222 struct z_candidate
*candidates
= 0, *cand
;
5223 tree fns
, convs
, first_mem_arg
= NULL_TREE
;
5225 tree result
= NULL_TREE
;
5227 auto_cond_timevar
tv (TV_OVERLOAD
);
5229 obj
= mark_lvalue_use (obj
);
5231 if (error_operand_p (obj
))
5232 return error_mark_node
;
5234 tree type
= TREE_TYPE (obj
);
5236 obj
= prep_operand (obj
);
5238 if (TYPE_PTRMEMFUNC_P (type
))
5240 if (complain
& tf_error
)
5241 /* It's no good looking for an overloaded operator() on a
5242 pointer-to-member-function. */
5243 error ("pointer-to-member function %qE cannot be called without "
5244 "an object; consider using %<.*%> or %<->*%>", obj
);
5245 return error_mark_node
;
5248 if (TYPE_BINFO (type
))
5250 fns
= lookup_fnfields (TYPE_BINFO (type
), call_op_identifier
, 1, complain
);
5251 if (fns
== error_mark_node
)
5252 return error_mark_node
;
5257 if (args
!= NULL
&& *args
!= NULL
)
5259 *args
= resolve_args (*args
, complain
);
5261 return error_mark_node
;
5264 conversion_obstack_sentinel cos
;
5268 first_mem_arg
= obj
;
5270 add_candidates (BASELINK_FUNCTIONS (fns
),
5271 first_mem_arg
, *args
, NULL_TREE
,
5273 BASELINK_BINFO (fns
), BASELINK_ACCESS_BINFO (fns
),
5274 LOOKUP_NORMAL
, &candidates
, complain
);
5277 bool any_call_ops
= candidates
!= nullptr;
5279 convs
= lookup_conversions (type
);
5281 for (; convs
; convs
= TREE_CHAIN (convs
))
5283 tree totype
= TREE_TYPE (convs
);
5285 if (TYPE_PTRFN_P (totype
)
5286 || TYPE_REFFN_P (totype
)
5287 || (TYPE_REF_P (totype
)
5288 && TYPE_PTRFN_P (TREE_TYPE (totype
))))
5289 for (tree fn
: ovl_range (TREE_VALUE (convs
)))
5291 if (DECL_NONCONVERTING_P (fn
))
5294 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
5296 /* Making this work broke PR 71117 and 85118, so until the
5297 committee resolves core issue 2189, let's disable this
5298 candidate if there are any call operators. */
5302 add_template_conv_candidate
5303 (&candidates
, fn
, obj
, *args
, totype
,
5304 /*access_path=*/NULL_TREE
,
5305 /*conversion_path=*/NULL_TREE
, complain
);
5308 add_conv_candidate (&candidates
, fn
, obj
,
5309 *args
, /*conversion_path=*/NULL_TREE
,
5310 /*access_path=*/NULL_TREE
, complain
);
5314 /* Be strict here because if we choose a bad conversion candidate, the
5315 errors we get won't mention the call context. */
5316 candidates
= splice_viable (candidates
, true, &any_viable_p
);
5319 if (complain
& tf_error
)
5321 auto_diagnostic_group d
;
5322 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj
),
5323 build_tree_list_vec (*args
));
5324 print_z_candidates (location_of (TREE_TYPE (obj
)), candidates
);
5326 result
= error_mark_node
;
5330 cand
= tourney (candidates
, complain
);
5333 if (complain
& tf_error
)
5335 auto_diagnostic_group d
;
5336 error ("call of %<(%T) (%A)%> is ambiguous",
5337 TREE_TYPE (obj
), build_tree_list_vec (*args
));
5338 print_z_candidates (location_of (TREE_TYPE (obj
)), candidates
);
5340 result
= error_mark_node
;
5342 else if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
5343 && DECL_OVERLOADED_OPERATOR_P (cand
->fn
)
5344 && DECL_OVERLOADED_OPERATOR_IS (cand
->fn
, CALL_EXPR
))
5346 result
= build_over_call (cand
, LOOKUP_NORMAL
, complain
);
5347 /* In an expression of the form `a()' where cand->fn
5348 which is operator() turns out to be a static member function,
5349 `a' is none-the-less evaluated. */
5350 result
= keep_unused_object_arg (result
, obj
, cand
->fn
);
5354 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
5355 obj
= convert_like_with_context (cand
->convs
[0], obj
, cand
->fn
,
5359 gcc_checking_assert (TYPE_P (cand
->fn
));
5360 obj
= convert_like (cand
->convs
[0], obj
, complain
);
5362 obj
= convert_from_reference (obj
);
5363 result
= cp_build_function_call_vec (obj
, args
, complain
);
5370 /* Called by op_error to prepare format strings suitable for the error
5371 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5372 and a suffix (controlled by NTYPES). */
5375 op_error_string (const char *errmsg
, int ntypes
, bool match
)
5379 const char *msgp
= concat (match
? G_("ambiguous overload for ")
5380 : G_("no match for "), errmsg
, NULL
);
5383 msg
= concat (msgp
, G_(" (operand types are %qT, %qT, and %qT)"), NULL
);
5384 else if (ntypes
== 2)
5385 msg
= concat (msgp
, G_(" (operand types are %qT and %qT)"), NULL
);
5387 msg
= concat (msgp
, G_(" (operand type is %qT)"), NULL
);
5393 op_error (const op_location_t
&loc
,
5394 enum tree_code code
, enum tree_code code2
,
5395 tree arg1
, tree arg2
, tree arg3
, bool match
)
5397 bool assop
= code
== MODIFY_EXPR
;
5398 const char *opname
= OVL_OP_INFO (assop
, assop
? code2
: code
)->name
;
5403 if (flag_diagnostics_show_caret
)
5404 error_at (loc
, op_error_string (G_("ternary %<operator?:%>"),
5406 TREE_TYPE (arg1
), TREE_TYPE (arg2
), TREE_TYPE (arg3
));
5408 error_at (loc
, op_error_string (G_("ternary %<operator?:%> "
5409 "in %<%E ? %E : %E%>"), 3, match
),
5411 TREE_TYPE (arg1
), TREE_TYPE (arg2
), TREE_TYPE (arg3
));
5414 case POSTINCREMENT_EXPR
:
5415 case POSTDECREMENT_EXPR
:
5416 if (flag_diagnostics_show_caret
)
5417 error_at (loc
, op_error_string (G_("%<operator%s%>"), 1, match
),
5418 opname
, TREE_TYPE (arg1
));
5420 error_at (loc
, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5422 opname
, arg1
, opname
, TREE_TYPE (arg1
));
5426 if (flag_diagnostics_show_caret
)
5427 error_at (loc
, op_error_string (G_("%<operator[]%>"), 2, match
),
5428 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
5430 error_at (loc
, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5432 arg1
, arg2
, TREE_TYPE (arg1
), TREE_TYPE (arg2
));
5437 if (flag_diagnostics_show_caret
)
5438 error_at (loc
, op_error_string (G_("%qs"), 1, match
),
5439 opname
, TREE_TYPE (arg1
));
5441 error_at (loc
, op_error_string (G_("%qs in %<%s %E%>"), 1, match
),
5442 opname
, opname
, arg1
, TREE_TYPE (arg1
));
5446 if (flag_diagnostics_show_caret
)
5447 error_at (loc
, op_error_string (G_("%<operator %s%>"), 1, match
),
5448 opname
, TREE_TYPE (arg1
));
5450 error_at (loc
, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5452 opname
, opname
, arg1
, TREE_TYPE (arg1
));
5457 if (flag_diagnostics_show_caret
)
5459 binary_op_rich_location
richloc (loc
, arg1
, arg2
, true);
5461 op_error_string (G_("%<operator%s%>"), 2, match
),
5462 opname
, TREE_TYPE (arg1
), TREE_TYPE (arg2
));
5465 error_at (loc
, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5467 opname
, arg1
, opname
, arg2
,
5468 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
5470 if (flag_diagnostics_show_caret
)
5471 error_at (loc
, op_error_string (G_("%<operator%s%>"), 1, match
),
5472 opname
, TREE_TYPE (arg1
));
5474 error_at (loc
, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5476 opname
, opname
, arg1
, TREE_TYPE (arg1
));
5481 /* Return the implicit conversion sequence that could be used to
5482 convert E1 to E2 in [expr.cond]. */
5485 conditional_conversion (tree e1
, tree e2
, tsubst_flags_t complain
)
5487 tree t1
= non_reference (TREE_TYPE (e1
));
5488 tree t2
= non_reference (TREE_TYPE (e2
));
5494 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5495 implicitly converted (clause _conv_) to the type "lvalue reference to
5496 T2", subject to the constraint that in the conversion the
5497 reference must bind directly (_dcl.init.ref_) to an lvalue.
5499 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5500 implicitly converted to the type "rvalue reference to T2", subject to
5501 the constraint that the reference must bind directly. */
5504 tree rtype
= cp_build_reference_type (t2
, !lvalue_p (e2
));
5505 conv
= implicit_conversion (rtype
,
5509 LOOKUP_NO_TEMP_BIND
|LOOKUP_NO_RVAL_BIND
5510 |LOOKUP_ONLYCONVERTING
,
5512 if (conv
&& !conv
->bad_p
)
5516 /* If E2 is a prvalue or if neither of the conversions above can be done
5517 and at least one of the operands has (possibly cv-qualified) class
5519 if (!CLASS_TYPE_P (t1
) && !CLASS_TYPE_P (t2
))
5524 If E1 and E2 have class type, and the underlying class types are
5525 the same or one is a base class of the other: E1 can be converted
5526 to match E2 if the class of T2 is the same type as, or a base
5527 class of, the class of T1, and the cv-qualification of T2 is the
5528 same cv-qualification as, or a greater cv-qualification than, the
5529 cv-qualification of T1. If the conversion is applied, E1 is
5530 changed to an rvalue of type T2 that still refers to the original
5531 source class object (or the appropriate subobject thereof). */
5532 if (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
5533 && ((good_base
= DERIVED_FROM_P (t2
, t1
)) || DERIVED_FROM_P (t1
, t2
)))
5535 if (good_base
&& at_least_as_qualified_p (t2
, t1
))
5537 conv
= build_identity_conv (t1
, e1
);
5538 if (!same_type_p (TYPE_MAIN_VARIANT (t1
),
5539 TYPE_MAIN_VARIANT (t2
)))
5540 conv
= build_conv (ck_base
, t2
, conv
);
5542 conv
= build_conv (ck_rvalue
, t2
, conv
);
5551 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5552 converted to the type that expression E2 would have if E2 were
5553 converted to an rvalue (or the type it has, if E2 is an rvalue). */
5554 return implicit_conversion (t2
, t1
, e1
, /*c_cast_p=*/false,
5555 LOOKUP_IMPLICIT
, complain
);
5558 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5559 arguments to the conditional expression. */
5562 build_conditional_expr (const op_location_t
&loc
,
5563 tree arg1
, tree arg2
, tree arg3
,
5564 tsubst_flags_t complain
)
5568 tree result
= NULL_TREE
;
5569 tree result_type
= NULL_TREE
;
5570 tree semantic_result_type
= NULL_TREE
;
5571 bool is_glvalue
= true;
5572 struct z_candidate
*candidates
= 0;
5573 struct z_candidate
*cand
;
5574 tree orig_arg2
, orig_arg3
;
5576 auto_cond_timevar
tv (TV_OVERLOAD
);
5578 /* As a G++ extension, the second argument to the conditional can be
5579 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5580 c'.) If the second operand is omitted, make sure it is
5581 calculated only once. */
5584 if (complain
& tf_error
)
5585 pedwarn (loc
, OPT_Wpedantic
,
5586 "ISO C++ forbids omitting the middle term of "
5587 "a %<?:%> expression");
5589 if ((complain
& tf_warning
) && !truth_value_p (TREE_CODE (arg1
)))
5590 warn_for_omitted_condop (loc
, arg1
);
5592 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5593 if (glvalue_p (arg1
))
5595 arg1
= cp_stabilize_reference (arg1
);
5596 arg2
= arg1
= prevent_lifetime_extension (arg1
);
5598 else if (TREE_CODE (arg1
) == TARGET_EXPR
)
5599 /* arg1 can't be a prvalue result of the conditional
5600 expression, since it needs to be materialized for the
5601 conversion to bool, so treat it as an xvalue in arg2. */
5602 arg2
= move (TARGET_EXPR_SLOT (arg1
));
5603 else if (TREE_CODE (arg1
) == EXCESS_PRECISION_EXPR
)
5604 arg2
= arg1
= build1 (EXCESS_PRECISION_EXPR
, TREE_TYPE (arg1
),
5605 cp_save_expr (TREE_OPERAND (arg1
, 0)));
5607 arg2
= arg1
= cp_save_expr (arg1
);
5610 /* If something has already gone wrong, just pass that fact up the
5612 if (error_operand_p (arg1
)
5613 || error_operand_p (arg2
)
5614 || error_operand_p (arg3
))
5615 return error_mark_node
;
5617 conversion_obstack_sentinel cos
;
5622 if (gnu_vector_type_p (TREE_TYPE (arg1
))
5623 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1
)))
5625 tree arg1_type
= TREE_TYPE (arg1
);
5627 /* If arg1 is another cond_expr choosing between -1 and 0,
5628 then we can use its comparison. It may help to avoid
5629 additional comparison, produce more accurate diagnostics
5630 and enables folding. */
5631 if (TREE_CODE (arg1
) == VEC_COND_EXPR
5632 && integer_minus_onep (TREE_OPERAND (arg1
, 1))
5633 && integer_zerop (TREE_OPERAND (arg1
, 2)))
5634 arg1
= TREE_OPERAND (arg1
, 0);
5636 arg1
= force_rvalue (arg1
, complain
);
5637 arg2
= force_rvalue (arg2
, complain
);
5638 arg3
= force_rvalue (arg3
, complain
);
5640 /* force_rvalue can return error_mark on valid arguments. */
5641 if (error_operand_p (arg1
)
5642 || error_operand_p (arg2
)
5643 || error_operand_p (arg3
))
5644 return error_mark_node
;
5646 arg2_type
= TREE_TYPE (arg2
);
5647 arg3_type
= TREE_TYPE (arg3
);
5649 if (!VECTOR_TYPE_P (arg2_type
)
5650 && !VECTOR_TYPE_P (arg3_type
))
5652 /* Rely on the error messages of the scalar version. */
5653 tree scal
= build_conditional_expr (loc
, integer_one_node
,
5654 orig_arg2
, orig_arg3
, complain
);
5655 if (scal
== error_mark_node
)
5656 return error_mark_node
;
5657 tree stype
= TREE_TYPE (scal
);
5658 tree ctype
= TREE_TYPE (arg1_type
);
5659 if (TYPE_SIZE (stype
) != TYPE_SIZE (ctype
)
5660 || (!INTEGRAL_TYPE_P (stype
) && !SCALAR_FLOAT_TYPE_P (stype
)))
5662 if (complain
& tf_error
)
5663 error_at (loc
, "inferred scalar type %qT is not an integer or "
5664 "floating-point type of the same size as %qT", stype
,
5665 COMPARISON_CLASS_P (arg1
)
5666 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
5668 return error_mark_node
;
5671 tree vtype
= build_opaque_vector_type (stype
,
5672 TYPE_VECTOR_SUBPARTS (arg1_type
));
5673 /* We could pass complain & tf_warning to unsafe_conversion_p,
5674 but the warnings (like Wsign-conversion) have already been
5675 given by the scalar build_conditional_expr_1. We still check
5676 unsafe_conversion_p to forbid truncating long long -> float. */
5677 if (unsafe_conversion_p (stype
, arg2
, NULL_TREE
, false))
5679 if (complain
& tf_error
)
5680 error_at (loc
, "conversion of scalar %qH to vector %qI "
5681 "involves truncation", arg2_type
, vtype
);
5682 return error_mark_node
;
5684 if (unsafe_conversion_p (stype
, arg3
, NULL_TREE
, false))
5686 if (complain
& tf_error
)
5687 error_at (loc
, "conversion of scalar %qH to vector %qI "
5688 "involves truncation", arg3_type
, vtype
);
5689 return error_mark_node
;
5692 arg2
= cp_convert (stype
, arg2
, complain
);
5693 arg2
= save_expr (arg2
);
5694 arg2
= build_vector_from_val (vtype
, arg2
);
5696 arg3
= cp_convert (stype
, arg3
, complain
);
5697 arg3
= save_expr (arg3
);
5698 arg3
= build_vector_from_val (vtype
, arg3
);
5702 if ((gnu_vector_type_p (arg2_type
) && !VECTOR_TYPE_P (arg3_type
))
5703 || (gnu_vector_type_p (arg3_type
) && !VECTOR_TYPE_P (arg2_type
)))
5705 enum stv_conv convert_flag
=
5706 scalar_to_vector (loc
, VEC_COND_EXPR
, arg2
, arg3
,
5707 complain
& tf_error
);
5709 switch (convert_flag
)
5712 return error_mark_node
;
5715 arg2
= save_expr (arg2
);
5716 arg2
= convert (TREE_TYPE (arg3_type
), arg2
);
5717 arg2
= build_vector_from_val (arg3_type
, arg2
);
5718 arg2_type
= TREE_TYPE (arg2
);
5723 arg3
= save_expr (arg3
);
5724 arg3
= convert (TREE_TYPE (arg2_type
), arg3
);
5725 arg3
= build_vector_from_val (arg2_type
, arg3
);
5726 arg3_type
= TREE_TYPE (arg3
);
5734 if (!gnu_vector_type_p (arg2_type
)
5735 || !gnu_vector_type_p (arg3_type
)
5736 || !same_type_p (arg2_type
, arg3_type
)
5737 || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type
),
5738 TYPE_VECTOR_SUBPARTS (arg2_type
))
5739 || TYPE_SIZE (arg1_type
) != TYPE_SIZE (arg2_type
))
5741 if (complain
& tf_error
)
5743 "incompatible vector types in conditional expression: "
5744 "%qT, %qT and %qT", TREE_TYPE (arg1
),
5745 TREE_TYPE (orig_arg2
), TREE_TYPE (orig_arg3
));
5746 return error_mark_node
;
5749 if (!COMPARISON_CLASS_P (arg1
))
5751 tree cmp_type
= truth_type_for (arg1_type
);
5752 arg1
= build2 (NE_EXPR
, cmp_type
, arg1
, build_zero_cst (arg1_type
));
5754 return build3_loc (loc
, VEC_COND_EXPR
, arg2_type
, arg1
, arg2
, arg3
);
5759 The first expression is implicitly converted to bool (clause
5761 arg1
= perform_implicit_conversion_flags (boolean_type_node
, arg1
, complain
,
5763 if (error_operand_p (arg1
))
5764 return error_mark_node
;
5766 arg2_type
= unlowered_expr_type (arg2
);
5767 arg3_type
= unlowered_expr_type (arg3
);
5769 if ((TREE_CODE (arg2
) == EXCESS_PRECISION_EXPR
5770 || TREE_CODE (arg3
) == EXCESS_PRECISION_EXPR
)
5771 && (TREE_CODE (arg2_type
) == INTEGER_TYPE
5772 || SCALAR_FLOAT_TYPE_P (arg2_type
)
5773 || TREE_CODE (arg2_type
) == COMPLEX_TYPE
)
5774 && (TREE_CODE (arg3_type
) == INTEGER_TYPE
5775 || SCALAR_FLOAT_TYPE_P (arg3_type
)
5776 || TREE_CODE (arg3_type
) == COMPLEX_TYPE
))
5778 semantic_result_type
5779 = type_after_usual_arithmetic_conversions (arg2_type
, arg3_type
);
5780 if (semantic_result_type
== error_mark_node
)
5782 tree t1
= arg2_type
;
5783 tree t2
= arg3_type
;
5784 if (TREE_CODE (t1
) == COMPLEX_TYPE
)
5785 t1
= TREE_TYPE (t1
);
5786 if (TREE_CODE (t2
) == COMPLEX_TYPE
)
5787 t2
= TREE_TYPE (t2
);
5788 gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1
)
5789 && SCALAR_FLOAT_TYPE_P (t2
)
5790 && (extended_float_type_p (t1
)
5791 || extended_float_type_p (t2
))
5792 && cp_compare_floating_point_conversion_ranks
5794 if (complain
& tf_error
)
5795 error_at (loc
, "operands to %<?:%> of types %qT and %qT "
5796 "have unordered conversion rank",
5797 arg2_type
, arg3_type
);
5798 return error_mark_node
;
5800 if (TREE_CODE (arg2
) == EXCESS_PRECISION_EXPR
)
5802 arg2
= TREE_OPERAND (arg2
, 0);
5803 arg2_type
= TREE_TYPE (arg2
);
5805 if (TREE_CODE (arg3
) == EXCESS_PRECISION_EXPR
)
5807 arg3
= TREE_OPERAND (arg3
, 0);
5808 arg3_type
= TREE_TYPE (arg3
);
5814 If either the second or the third operand has type (possibly
5815 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
5816 array-to-pointer (_conv.array_), and function-to-pointer
5817 (_conv.func_) standard conversions are performed on the second
5818 and third operands. */
5819 if (VOID_TYPE_P (arg2_type
) || VOID_TYPE_P (arg3_type
))
5821 /* 'void' won't help in resolving an overloaded expression on the
5822 other side, so require it to resolve by itself. */
5823 if (arg2_type
== unknown_type_node
)
5825 arg2
= resolve_nondeduced_context_or_error (arg2
, complain
);
5826 arg2_type
= TREE_TYPE (arg2
);
5828 if (arg3_type
== unknown_type_node
)
5830 arg3
= resolve_nondeduced_context_or_error (arg3
, complain
);
5831 arg3_type
= TREE_TYPE (arg3
);
5836 One of the following shall hold:
5838 --The second or the third operand (but not both) is a
5839 throw-expression (_except.throw_); the result is of the type
5840 and value category of the other.
5842 --Both the second and the third operands have type void; the
5843 result is of type void and is a prvalue. */
5844 if (TREE_CODE (arg2
) == THROW_EXPR
5845 && TREE_CODE (arg3
) != THROW_EXPR
)
5847 result_type
= arg3_type
;
5848 is_glvalue
= glvalue_p (arg3
);
5850 else if (TREE_CODE (arg2
) != THROW_EXPR
5851 && TREE_CODE (arg3
) == THROW_EXPR
)
5853 result_type
= arg2_type
;
5854 is_glvalue
= glvalue_p (arg2
);
5856 else if (VOID_TYPE_P (arg2_type
) && VOID_TYPE_P (arg3_type
))
5858 result_type
= void_type_node
;
5863 if (complain
& tf_error
)
5865 if (VOID_TYPE_P (arg2_type
))
5866 error_at (cp_expr_loc_or_loc (arg3
, loc
),
5867 "second operand to the conditional operator "
5868 "is of type %<void%>, but the third operand is "
5869 "neither a throw-expression nor of type %<void%>");
5871 error_at (cp_expr_loc_or_loc (arg2
, loc
),
5872 "third operand to the conditional operator "
5873 "is of type %<void%>, but the second operand is "
5874 "neither a throw-expression nor of type %<void%>");
5876 return error_mark_node
;
5879 goto valid_operands
;
5883 Otherwise, if the second and third operand have different types,
5884 and either has (possibly cv-qualified) class type, or if both are
5885 glvalues of the same value category and the same type except for
5886 cv-qualification, an attempt is made to convert each of those operands
5887 to the type of the other. */
5888 else if (!same_type_p (arg2_type
, arg3_type
)
5889 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)
5890 || (same_type_ignoring_top_level_qualifiers_p (arg2_type
,
5892 && glvalue_p (arg2
) && glvalue_p (arg3
)
5893 && lvalue_p (arg2
) == lvalue_p (arg3
))))
5897 bool converted
= false;
5899 conv2
= conditional_conversion (arg2
, arg3
, complain
);
5900 conv3
= conditional_conversion (arg3
, arg2
, complain
);
5904 If both can be converted, or one can be converted but the
5905 conversion is ambiguous, the program is ill-formed. If
5906 neither can be converted, the operands are left unchanged and
5907 further checking is performed as described below. If exactly
5908 one conversion is possible, that conversion is applied to the
5909 chosen operand and the converted operand is used in place of
5910 the original operand for the remainder of this section. */
5911 if ((conv2
&& !conv2
->bad_p
5912 && conv3
&& !conv3
->bad_p
)
5913 || (conv2
&& conv2
->kind
== ck_ambig
)
5914 || (conv3
&& conv3
->kind
== ck_ambig
))
5916 if (complain
& tf_error
)
5918 error_at (loc
, "operands to %<?:%> have different types "
5920 arg2_type
, arg3_type
);
5921 if (conv2
&& !conv2
->bad_p
&& conv3
&& !conv3
->bad_p
)
5922 inform (loc
, " and each type can be converted to the other");
5923 else if (conv2
&& conv2
->kind
== ck_ambig
)
5924 convert_like (conv2
, arg2
, complain
);
5926 convert_like (conv3
, arg3
, complain
);
5928 result
= error_mark_node
;
5930 else if (conv2
&& !conv2
->bad_p
)
5932 arg2
= convert_like (conv2
, arg2
, complain
);
5933 arg2
= convert_from_reference (arg2
);
5934 arg2_type
= TREE_TYPE (arg2
);
5935 /* Even if CONV2 is a valid conversion, the result of the
5936 conversion may be invalid. For example, if ARG3 has type
5937 "volatile X", and X does not have a copy constructor
5938 accepting a "volatile X&", then even if ARG2 can be
5939 converted to X, the conversion will fail. */
5940 if (error_operand_p (arg2
))
5941 result
= error_mark_node
;
5944 else if (conv3
&& !conv3
->bad_p
)
5946 arg3
= convert_like (conv3
, arg3
, complain
);
5947 arg3
= convert_from_reference (arg3
);
5948 arg3_type
= TREE_TYPE (arg3
);
5949 if (error_operand_p (arg3
))
5950 result
= error_mark_node
;
5957 /* If, after the conversion, both operands have class type,
5958 treat the cv-qualification of both operands as if it were the
5959 union of the cv-qualification of the operands.
5961 The standard is not clear about what to do in this
5962 circumstance. For example, if the first operand has type
5963 "const X" and the second operand has a user-defined
5964 conversion to "volatile X", what is the type of the second
5965 operand after this step? Making it be "const X" (matching
5966 the first operand) seems wrong, as that discards the
5967 qualification without actually performing a copy. Leaving it
5968 as "volatile X" seems wrong as that will result in the
5969 conditional expression failing altogether, even though,
5970 according to this step, the one operand could be converted to
5971 the type of the other. */
5973 && CLASS_TYPE_P (arg2_type
)
5974 && cp_type_quals (arg2_type
) != cp_type_quals (arg3_type
))
5975 arg2_type
= arg3_type
=
5976 cp_build_qualified_type (arg2_type
,
5977 cp_type_quals (arg2_type
)
5978 | cp_type_quals (arg3_type
));
5983 If the second and third operands are glvalues of the same value
5984 category and have the same type, the result is of that type and
5986 if (((lvalue_p (arg2
) && lvalue_p (arg3
))
5987 || (xvalue_p (arg2
) && xvalue_p (arg3
)))
5988 && same_type_p (arg2_type
, arg3_type
))
5990 result_type
= arg2_type
;
5991 goto valid_operands
;
5996 Otherwise, the result is an rvalue. If the second and third
5997 operand do not have the same type, and either has (possibly
5998 cv-qualified) class type, overload resolution is used to
5999 determine the conversions (if any) to be applied to the operands
6000 (_over.match.oper_, _over.built_). */
6002 if (!same_type_p (arg2_type
, arg3_type
)
6003 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
6009 /* Rearrange the arguments so that add_builtin_candidate only has
6010 to know about two args. In build_builtin_candidate, the
6011 arguments are unscrambled. */
6012 args
->quick_push (arg2
);
6013 args
->quick_push (arg3
);
6014 args
->quick_push (arg1
);
6015 add_builtin_candidates (&candidates
,
6018 ovl_op_identifier (false, COND_EXPR
),
6020 LOOKUP_NORMAL
, complain
);
6024 If the overload resolution fails, the program is
6026 candidates
= splice_viable (candidates
, false, &any_viable_p
);
6029 if (complain
& tf_error
)
6030 error_at (loc
, "operands to %<?:%> have different types %qT and %qT",
6031 arg2_type
, arg3_type
);
6032 return error_mark_node
;
6034 cand
= tourney (candidates
, complain
);
6037 if (complain
& tf_error
)
6039 auto_diagnostic_group d
;
6040 op_error (loc
, COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, false);
6041 print_z_candidates (loc
, candidates
);
6043 return error_mark_node
;
6048 Otherwise, the conversions thus determined are applied, and
6049 the converted operands are used in place of the original
6050 operands for the remainder of this section. */
6051 conv
= cand
->convs
[0];
6052 arg1
= convert_like (conv
, arg1
, complain
);
6053 conv
= cand
->convs
[1];
6054 arg2
= convert_like (conv
, arg2
, complain
);
6055 arg2_type
= TREE_TYPE (arg2
);
6056 conv
= cand
->convs
[2];
6057 arg3
= convert_like (conv
, arg3
, complain
);
6058 arg3_type
= TREE_TYPE (arg3
);
6063 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
6064 and function-to-pointer (_conv.func_) standard conversions are
6065 performed on the second and third operands.
6067 We need to force the lvalue-to-rvalue conversion here for class types,
6068 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
6069 that isn't wrapped with a TARGET_EXPR plays havoc with exception
6072 arg2
= force_rvalue (arg2
, complain
);
6073 if (!CLASS_TYPE_P (arg2_type
))
6074 arg2_type
= TREE_TYPE (arg2
);
6076 arg3
= force_rvalue (arg3
, complain
);
6077 if (!CLASS_TYPE_P (arg3_type
))
6078 arg3_type
= TREE_TYPE (arg3
);
6080 if (arg2
== error_mark_node
|| arg3
== error_mark_node
)
6081 return error_mark_node
;
6085 After those conversions, one of the following shall hold:
6087 --The second and third operands have the same type; the result is of
6089 if (same_type_p (arg2_type
, arg3_type
))
6090 result_type
= arg2_type
;
6093 --The second and third operands have arithmetic or enumeration
6094 type; the usual arithmetic conversions are performed to bring
6095 them to a common type, and the result is of that type. */
6096 else if ((ARITHMETIC_TYPE_P (arg2_type
)
6097 || UNSCOPED_ENUM_P (arg2_type
))
6098 && (ARITHMETIC_TYPE_P (arg3_type
)
6099 || UNSCOPED_ENUM_P (arg3_type
)))
6101 /* A conditional expression between a floating-point
6102 type and an integer type should convert the integer type to
6103 the evaluation format of the floating-point type, with
6104 possible excess precision. */
6105 tree eptype2
= arg2_type
;
6106 tree eptype3
= arg3_type
;
6108 if (ANY_INTEGRAL_TYPE_P (arg2_type
)
6109 && (eptype
= excess_precision_type (arg3_type
)) != NULL_TREE
)
6112 if (!semantic_result_type
)
6113 semantic_result_type
6114 = type_after_usual_arithmetic_conversions (arg2_type
, arg3_type
);
6116 else if (ANY_INTEGRAL_TYPE_P (arg3_type
)
6117 && (eptype
= excess_precision_type (arg2_type
)) != NULL_TREE
)
6120 if (!semantic_result_type
)
6121 semantic_result_type
6122 = type_after_usual_arithmetic_conversions (arg2_type
, arg3_type
);
6124 result_type
= type_after_usual_arithmetic_conversions (eptype2
,
6126 if (result_type
== error_mark_node
)
6130 if (TREE_CODE (t1
) == COMPLEX_TYPE
)
6131 t1
= TREE_TYPE (t1
);
6132 if (TREE_CODE (t2
) == COMPLEX_TYPE
)
6133 t2
= TREE_TYPE (t2
);
6134 gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1
)
6135 && SCALAR_FLOAT_TYPE_P (t2
)
6136 && (extended_float_type_p (t1
)
6137 || extended_float_type_p (t2
))
6138 && cp_compare_floating_point_conversion_ranks
6140 if (complain
& tf_error
)
6141 error_at (loc
, "operands to %<?:%> of types %qT and %qT "
6142 "have unordered conversion rank",
6144 return error_mark_node
;
6146 if (semantic_result_type
== error_mark_node
)
6148 tree t1
= arg2_type
;
6149 tree t2
= arg3_type
;
6150 if (TREE_CODE (t1
) == COMPLEX_TYPE
)
6151 t1
= TREE_TYPE (t1
);
6152 if (TREE_CODE (t2
) == COMPLEX_TYPE
)
6153 t2
= TREE_TYPE (t2
);
6154 gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1
)
6155 && SCALAR_FLOAT_TYPE_P (t2
)
6156 && (extended_float_type_p (t1
)
6157 || extended_float_type_p (t2
))
6158 && cp_compare_floating_point_conversion_ranks
6160 if (complain
& tf_error
)
6161 error_at (loc
, "operands to %<?:%> of types %qT and %qT "
6162 "have unordered conversion rank",
6163 arg2_type
, arg3_type
);
6164 return error_mark_node
;
6167 if (complain
& tf_warning
)
6168 do_warn_double_promotion (result_type
, arg2_type
, arg3_type
,
6169 "implicit conversion from %qH to %qI to "
6170 "match other result of conditional",
6173 if (TREE_CODE (arg2_type
) == ENUMERAL_TYPE
6174 && TREE_CODE (arg3_type
) == ENUMERAL_TYPE
)
6176 tree stripped_orig_arg2
= tree_strip_any_location_wrapper (orig_arg2
);
6177 tree stripped_orig_arg3
= tree_strip_any_location_wrapper (orig_arg3
);
6178 if (TREE_CODE (stripped_orig_arg2
) == CONST_DECL
6179 && TREE_CODE (stripped_orig_arg3
) == CONST_DECL
6180 && (DECL_CONTEXT (stripped_orig_arg2
)
6181 == DECL_CONTEXT (stripped_orig_arg3
)))
6182 /* Two enumerators from the same enumeration can have different
6183 types when the enumeration is still being defined. */;
6184 else if (complain
& tf_warning
)
6185 warning_at (loc
, OPT_Wenum_compare
, "enumerated mismatch "
6186 "in conditional expression: %qT vs %qT",
6187 arg2_type
, arg3_type
);
6189 else if ((complain
& tf_warning
)
6190 && warn_deprecated_enum_float_conv
6191 && ((TREE_CODE (arg2_type
) == ENUMERAL_TYPE
6192 && SCALAR_FLOAT_TYPE_P (arg3_type
))
6193 || (SCALAR_FLOAT_TYPE_P (arg2_type
)
6194 && TREE_CODE (arg3_type
) == ENUMERAL_TYPE
)))
6196 if (TREE_CODE (arg2_type
) == ENUMERAL_TYPE
)
6197 warning_at (loc
, OPT_Wdeprecated_enum_float_conversion
,
6198 "conditional expression between enumeration type "
6199 "%qT and floating-point type %qT is deprecated",
6200 arg2_type
, arg3_type
);
6202 warning_at (loc
, OPT_Wdeprecated_enum_float_conversion
,
6203 "conditional expression between floating-point "
6204 "type %qT and enumeration type %qT is deprecated",
6205 arg2_type
, arg3_type
);
6207 else if ((extra_warnings
|| warn_enum_conversion
)
6208 && ((TREE_CODE (arg2_type
) == ENUMERAL_TYPE
6209 && !same_type_p (arg3_type
, type_promotes_to (arg2_type
)))
6210 || (TREE_CODE (arg3_type
) == ENUMERAL_TYPE
6211 && !same_type_p (arg2_type
,
6212 type_promotes_to (arg3_type
)))))
6214 if (complain
& tf_warning
)
6216 enum opt_code opt
= (warn_enum_conversion
6217 ? OPT_Wenum_conversion
6219 warning_at (loc
, opt
, "enumerated and "
6220 "non-enumerated type in conditional expression");
6224 arg2
= perform_implicit_conversion (result_type
, arg2
, complain
);
6225 arg3
= perform_implicit_conversion (result_type
, arg3
, complain
);
6229 --The second and third operands have pointer type, or one has
6230 pointer type and the other is a null pointer constant; pointer
6231 conversions (_conv.ptr_) and qualification conversions
6232 (_conv.qual_) are performed to bring them to their composite
6233 pointer type (_expr.rel_). The result is of the composite
6236 --The second and third operands have pointer to member type, or
6237 one has pointer to member type and the other is a null pointer
6238 constant; pointer to member conversions (_conv.mem_) and
6239 qualification conversions (_conv.qual_) are performed to bring
6240 them to a common type, whose cv-qualification shall match the
6241 cv-qualification of either the second or the third operand.
6242 The result is of the common type. */
6243 else if ((null_ptr_cst_p (arg2
)
6244 && TYPE_PTR_OR_PTRMEM_P (arg3_type
))
6245 || (null_ptr_cst_p (arg3
)
6246 && TYPE_PTR_OR_PTRMEM_P (arg2_type
))
6247 || (TYPE_PTR_P (arg2_type
) && TYPE_PTR_P (arg3_type
))
6248 || (TYPE_PTRDATAMEM_P (arg2_type
) && TYPE_PTRDATAMEM_P (arg3_type
))
6249 || (TYPE_PTRMEMFUNC_P (arg2_type
) && TYPE_PTRMEMFUNC_P (arg3_type
)))
6251 result_type
= composite_pointer_type (loc
,
6252 arg2_type
, arg3_type
, arg2
,
6253 arg3
, CPO_CONDITIONAL_EXPR
,
6255 if (result_type
== error_mark_node
)
6256 return error_mark_node
;
6257 arg2
= perform_implicit_conversion (result_type
, arg2
, complain
);
6258 arg3
= perform_implicit_conversion (result_type
, arg3
, complain
);
6263 if (complain
& tf_error
)
6264 error_at (loc
, "operands to %<?:%> have different types %qT and %qT",
6265 arg2_type
, arg3_type
);
6266 return error_mark_node
;
6269 if (arg2
== error_mark_node
|| arg3
== error_mark_node
)
6270 return error_mark_node
;
6273 if (processing_template_decl
&& is_glvalue
)
6275 /* Let lvalue_kind know this was a glvalue. */
6276 tree arg
= (result_type
== arg2_type
? arg2
: arg3
);
6277 result_type
= cp_build_reference_type (result_type
, xvalue_p (arg
));
6280 result
= build3_loc (loc
, COND_EXPR
, result_type
, arg1
, arg2
, arg3
);
6282 /* If the ARG2 and ARG3 are the same and don't have side-effects,
6283 warn here, because the COND_EXPR will be turned into ARG2. */
6284 if (warn_duplicated_branches
6285 && (complain
& tf_warning
)
6286 && (arg2
== arg3
|| operand_equal_p (arg2
, arg3
,
6287 OEP_ADDRESS_OF_SAME_FIELD
)))
6288 warning_at (EXPR_LOCATION (result
), OPT_Wduplicated_branches
,
6289 "this condition has identical branches");
6291 /* We can't use result_type below, as fold might have returned a
6296 /* Expand both sides into the same slot, hopefully the target of
6297 the ?: expression. We used to check for TARGET_EXPRs here,
6298 but now we sometimes wrap them in NOP_EXPRs so the test would
6300 if (CLASS_TYPE_P (TREE_TYPE (result
)))
6302 result
= get_target_expr (result
, complain
);
6303 /* Tell gimplify_modify_expr_rhs not to strip this in
6304 assignment context: we want both arms to initialize
6305 the same temporary. */
6306 TARGET_EXPR_NO_ELIDE (result
) = true;
6308 /* If this expression is an rvalue, but might be mistaken for an
6309 lvalue, we must add a NON_LVALUE_EXPR. */
6310 result
= rvalue (result
);
6311 if (semantic_result_type
)
6312 result
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
,
6317 result
= force_paren_expr (result
);
6318 gcc_assert (semantic_result_type
== NULL_TREE
);
6324 /* OPERAND is an operand to an expression. Perform necessary steps
6325 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6329 prep_operand (tree operand
)
6333 if (CLASS_TYPE_P (TREE_TYPE (operand
))
6334 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand
)))
6335 /* Make sure the template type is instantiated now. */
6336 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand
)));
6342 /* True iff CONV represents a conversion sequence which no other can be better
6343 than under [over.ics.rank]: in other words, a "conversion" to the exact same
6344 type (including binding to a reference to the same type). This is stronger
6345 than the standard's "identity" category, which also includes reference
6346 bindings that add cv-qualifiers or change rvalueness. */
6349 perfect_conversion_p (conversion
*conv
)
6351 if (CONVERSION_RANK (conv
) != cr_identity
)
6353 if (conv
->kind
== ck_ref_bind
)
6355 if (!conv
->rvaluedness_matches_p
)
6357 if (!same_type_p (TREE_TYPE (conv
->type
),
6358 next_conversion (conv
)->type
))
6361 if (conv
->check_narrowing
)
6362 /* Brace elision is imperfect. */
6367 /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6368 other candidate can be a better match. Since the template/non-template
6369 tiebreaker comes immediately after the conversion comparison in
6370 [over.match.best], a perfect non-template candidate is better than all
6374 perfect_candidate_p (z_candidate
*cand
)
6376 if (cand
->viable
< 1)
6378 /* CWG1402 makes an implicitly deleted move op worse than other
6380 if (DECL_DELETED_FN (cand
->fn
) && DECL_DEFAULTED_FN (cand
->fn
)
6381 && move_fn_p (cand
->fn
))
6383 int len
= cand
->num_convs
;
6384 for (int i
= 0; i
< len
; ++i
)
6385 if (!perfect_conversion_p (cand
->convs
[i
]))
6387 if (conversion
*conv
= cand
->second_conv
)
6388 if (!perfect_conversion_p (conv
))
6393 /* True iff one of CAND's argument conversions is missing. */
6396 missing_conversion_p (const z_candidate
*cand
)
6398 for (unsigned i
= 0; i
< cand
->num_convs
; ++i
)
6400 conversion
*conv
= cand
->convs
[i
];
6403 if (conv
->kind
== ck_deferred_bad
)
6405 /* We don't know whether this conversion is outright invalid or
6406 just bad, so conservatively assume it's missing. */
6407 gcc_checking_assert (conv
->bad_p
);
6414 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6415 OVERLOAD) to the CANDIDATES, returning an updated list of
6416 CANDIDATES. The ARGS are the arguments provided to the call;
6417 if FIRST_ARG is non-null it is the implicit object argument,
6418 otherwise the first element of ARGS is used if needed. The
6419 EXPLICIT_TARGS are explicit template arguments provided.
6420 TEMPLATE_ONLY is true if only template functions should be
6421 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6422 add_function_candidate. */
6425 add_candidates (tree fns
, tree first_arg
, const vec
<tree
, va_gc
> *args
,
6427 tree explicit_targs
, bool template_only
,
6428 tree conversion_path
, tree access_path
,
6430 struct z_candidate
**candidates
,
6431 tsubst_flags_t complain
)
6434 const vec
<tree
, va_gc
> *non_static_args
;
6435 bool check_list_ctor
= false;
6436 bool check_converting
= false;
6437 unification_kind_t strict
;
6438 tree ne_fns
= NULL_TREE
;
6443 /* Precalculate special handling of constructors and conversion ops. */
6444 tree fn
= OVL_FIRST (fns
);
6445 if (DECL_CONV_FN_P (fn
))
6447 check_list_ctor
= false;
6448 check_converting
= (flags
& LOOKUP_ONLYCONVERTING
) != 0;
6449 if (flags
& LOOKUP_NO_CONVERSION
)
6450 /* We're doing return_type(x). */
6451 strict
= DEDUCE_CONV
;
6453 /* We're doing x.operator return_type(). */
6454 strict
= DEDUCE_EXACT
;
6455 /* [over.match.funcs] For conversion functions, the function
6456 is considered to be a member of the class of the implicit
6457 object argument for the purpose of defining the type of
6458 the implicit object parameter. */
6459 ctype
= TYPE_MAIN_VARIANT (TREE_TYPE (first_arg
));
6463 if (DECL_CONSTRUCTOR_P (fn
))
6465 check_list_ctor
= (flags
& LOOKUP_LIST_ONLY
) != 0;
6466 /* For list-initialization we consider explicit constructors
6467 and complain if one is chosen. */
6469 = ((flags
& (LOOKUP_ONLYCONVERTING
|LOOKUP_LIST_INIT_CTOR
))
6470 == LOOKUP_ONLYCONVERTING
);
6472 strict
= DEDUCE_CALL
;
6473 ctype
= conversion_path
? BINFO_TYPE (conversion_path
) : NULL_TREE
;
6476 /* P2468: Check if operator== is a rewrite target with first operand
6477 (*args)[0]; for now just do the lookups. */
6478 if ((flags
& (LOOKUP_REWRITTEN
| LOOKUP_REVERSED
))
6479 && DECL_OVERLOADED_OPERATOR_IS (fn
, EQ_EXPR
))
6481 tree ne_name
= ovl_op_identifier (false, NE_EXPR
);
6482 if (DECL_CLASS_SCOPE_P (fn
))
6484 ne_fns
= lookup_fnfields (TREE_TYPE ((*args
)[0]), ne_name
,
6486 if (ne_fns
== error_mark_node
|| ne_fns
== NULL_TREE
)
6489 ne_fns
= BASELINK_FUNCTIONS (ne_fns
);
6493 tree context
= decl_namespace_context (fn
);
6494 ne_fns
= lookup_qualified_name (context
, ne_name
, LOOK_want::NORMAL
,
6496 if (ne_fns
== error_mark_node
6497 || !is_overloaded_fn (ne_fns
))
6503 non_static_args
= args
;
6505 /* Delay creating the implicit this parameter until it is needed. */
6506 non_static_args
= NULL
;
6508 bool seen_strictly_viable
= any_strictly_viable (*candidates
);
6509 /* If there's a non-template perfect match, we don't need to consider
6510 templates. So check non-templates first. This optimization is only
6511 really needed for the defaulted copy constructor of tuple and the like
6512 (96926), but it seems like we might as well enable it more generally. */
6513 bool seen_perfect
= false;
6514 enum { templates
, non_templates
, either
} which
= either
;
6517 else /*if (flags & LOOKUP_DEFAULTED)*/
6518 which
= non_templates
;
6520 /* During overload resolution, we first consider each function under the
6521 assumption that we'll eventually find a strictly viable candidate.
6522 This allows us to circumvent our defacto behavior when checking
6523 argument conversions and shortcut consideration of the candidate
6524 upon encountering the first bad conversion. If this assumption
6525 turns out to be false, and all candidates end up being non-strictly
6526 viable, then we reconsider such candidates under the defacto behavior.
6527 This trick is important for pruning member function overloads according
6528 to their const/ref-qualifiers (since all 'this' conversions are at
6529 worst bad) without breaking -fpermissive. */
6530 tree bad_fns
= NULL_TREE
;
6531 bool shortcut_bad_convs
= true;
6534 for (tree fn
: lkp_range (fns
))
6536 if (check_converting
&& DECL_NONCONVERTING_P (fn
))
6538 if (check_list_ctor
&& !is_list_ctor (fn
))
6540 if (which
== templates
&& TREE_CODE (fn
) != TEMPLATE_DECL
)
6542 if (which
== non_templates
&& TREE_CODE (fn
) == TEMPLATE_DECL
)
6545 tree fn_first_arg
= NULL_TREE
;
6546 const vec
<tree
, va_gc
> *fn_args
= args
;
6548 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
6550 /* Figure out where the object arg comes from. If this
6551 function is a non-static member and we didn't get an
6552 implicit object argument, move it out of args. */
6553 if (first_arg
== NULL_TREE
)
6557 vec
<tree
, va_gc
> *tempvec
;
6558 vec_alloc (tempvec
, args
->length () - 1);
6559 for (ix
= 1; args
->iterate (ix
, &arg
); ++ix
)
6560 tempvec
->quick_push (arg
);
6561 non_static_args
= tempvec
;
6562 first_arg
= (*args
)[0];
6565 fn_first_arg
= first_arg
;
6566 fn_args
= non_static_args
;
6569 /* Don't bother reversing an operator with two identical parameters. */
6570 else if (vec_safe_length (args
) == 2 && (flags
& LOOKUP_REVERSED
))
6572 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
6573 if (same_type_p (TREE_VALUE (parmlist
),
6574 TREE_VALUE (TREE_CHAIN (parmlist
))))
6578 /* When considering reversed operator==, if there's a corresponding
6579 operator!= in the same scope, it's not a rewrite target. */
6583 for (lkp_iterator
ne (ne_fns
); !found
&& ne
; ++ne
)
6584 if (0 && !ne
.using_p ()
6585 && DECL_NAMESPACE_SCOPE_P (fn
)
6586 && DECL_CONTEXT (*ne
) != DECL_CONTEXT (fn
))
6587 /* ??? This kludge excludes inline namespace members for the H
6588 test in spaceship-eq15.C, but I don't see why we would want
6589 that behavior. Asked Core 2022-11-04. Disabling for now. */;
6590 else if (fns_correspond (fn
, *ne
))
6599 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
6601 if (!add_template_candidate (candidates
,
6618 add_function_candidate (candidates
,
6629 if (perfect_candidate_p (*candidates
))
6630 seen_perfect
= true;
6633 z_candidate
*cand
= *candidates
;
6634 if (cand
->viable
== 1)
6635 seen_strictly_viable
= true;
6637 if (cand
->viable
== -1
6638 && shortcut_bad_convs
6639 && missing_conversion_p (cand
))
6641 /* This candidate has been tentatively marked non-strictly viable,
6642 and we didn't compute all argument conversions for it (having
6643 stopped at the first bad conversion). Add the function to BAD_FNS
6644 to fully reconsider later if we don't find any strictly viable
6646 if (complain
& (tf_error
| tf_conv
))
6648 bad_fns
= lookup_add (fn
, bad_fns
);
6649 *candidates
= (*candidates
)->next
;
6652 /* But if we're in a SFINAE context, just mark this candidate as
6653 unviable outright and avoid potentially reconsidering it.
6654 This is safe to do because in a SFINAE context, performing a bad
6655 conversion is always an error (even with -fpermissive), so a
6656 non-strictly viable candidate is effectively unviable anyway. */
6660 if (which
== non_templates
&& !seen_perfect
)
6665 else if (which
== templates
6666 && !seen_strictly_viable
6667 && shortcut_bad_convs
6670 /* None of the candidates are strictly viable, so consider again those
6671 functions in BAD_FNS, this time without shortcutting bad conversions
6672 so that all their argument conversions are computed. */
6675 shortcut_bad_convs
= false;
6680 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
6681 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
6684 op_is_ordered (tree_code code
)
6690 return (flag_strong_eval_order
> 1 ? -1 : 0);
6694 return (flag_strong_eval_order
> 1 ? 1 : 0);
6697 // Not overloadable (yet).
6699 // Only one argument.
6707 // Predates P0145R3.
6708 case TRUTH_ANDIF_EXPR
:
6710 // Predates P0145R3.
6711 case TRUTH_ORIF_EXPR
:
6713 // Predates P0145R3.
6715 return (flag_strong_eval_order
? 1 : 0);
6722 /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
6723 operator indicated by CODE/CODE2. This function calls itself recursively to
6724 handle C++20 rewritten comparison operator candidates.
6726 LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
6727 overloads to consider. This parameter is used when instantiating a
6728 dependent operator expression and has the same structure as
6729 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
6732 add_operator_candidates (z_candidate
**candidates
,
6733 tree_code code
, tree_code code2
,
6734 vec
<tree
, va_gc
> *arglist
, tree lookups
,
6735 int flags
, tsubst_flags_t complain
)
6737 z_candidate
*start_candidates
= *candidates
;
6738 bool ismodop
= code2
!= ERROR_MARK
;
6739 tree fnname
= ovl_op_identifier (ismodop
, ismodop
? code2
: code
);
6741 /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
6742 rewrite from, and also when we're looking for the e.g. < operator to use
6743 on the result of <=>. In the latter case, we don't want the flag set in
6744 the candidate, we just want to suppress looking for rewrites. */
6745 bool rewritten
= (flags
& LOOKUP_REWRITTEN
);
6746 if (rewritten
&& code
!= EQ_EXPR
&& code
!= SPACESHIP_EXPR
)
6747 flags
&= ~LOOKUP_REWRITTEN
;
6749 bool memonly
= false;
6752 /* =, ->, [], () must be non-static member functions. */
6754 if (code2
!= NOP_EXPR
)
6766 /* Add namespace-scope operators to the list of functions to
6772 fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
);
6773 /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
6774 expression, and LOOKUPS is the result of stage 1 name lookup. */
6775 else if (tree found
= purpose_member (fnname
, lookups
))
6776 fns
= TREE_VALUE (found
);
6779 fns
= lookup_arg_dependent (fnname
, fns
, arglist
);
6780 add_candidates (fns
, NULL_TREE
, arglist
, NULL_TREE
,
6781 NULL_TREE
, false, NULL_TREE
, NULL_TREE
,
6782 flags
, candidates
, complain
);
6785 /* Add class-member operators to the candidate set. */
6786 tree arg1_type
= TREE_TYPE ((*arglist
)[0]);
6787 unsigned nargs
= arglist
->length () > 1 ? 2 : 1;
6788 tree arg2_type
= nargs
> 1 ? TREE_TYPE ((*arglist
)[1]) : NULL_TREE
;
6789 if (CLASS_TYPE_P (arg1_type
))
6791 tree fns
= lookup_fnfields (arg1_type
, fnname
, 1, complain
);
6792 if (fns
== error_mark_node
)
6793 return error_mark_node
;
6796 if (code
== ARRAY_REF
)
6798 vec
<tree
,va_gc
> *restlist
= make_tree_vector ();
6799 for (unsigned i
= 1; i
< nargs
; ++i
)
6800 vec_safe_push (restlist
, (*arglist
)[i
]);
6801 z_candidate
*save_cand
= *candidates
;
6802 add_candidates (BASELINK_FUNCTIONS (fns
),
6803 (*arglist
)[0], restlist
, NULL_TREE
,
6805 BASELINK_BINFO (fns
),
6806 BASELINK_ACCESS_BINFO (fns
),
6807 flags
, candidates
, complain
);
6808 /* Release the vec if we didn't add a candidate that uses it. */
6809 for (z_candidate
*c
= *candidates
; c
!= save_cand
; c
= c
->next
)
6810 if (c
->args
== restlist
)
6815 release_tree_vector (restlist
);
6818 add_candidates (BASELINK_FUNCTIONS (fns
),
6819 NULL_TREE
, arglist
, NULL_TREE
,
6821 BASELINK_BINFO (fns
),
6822 BASELINK_ACCESS_BINFO (fns
),
6823 flags
, candidates
, complain
);
6826 /* Per [over.match.oper]3.2, if no operand has a class type, then
6827 only non-member functions that have type T1 or reference to
6828 cv-qualified-opt T1 for the first argument, if the first argument
6829 has an enumeration type, or T2 or reference to cv-qualified-opt
6830 T2 for the second argument, if the second argument has an
6831 enumeration type. Filter out those that don't match. */
6832 else if (! arg2_type
|| ! CLASS_TYPE_P (arg2_type
))
6834 struct z_candidate
**candp
, **next
;
6836 for (candp
= candidates
; *candp
!= start_candidates
; candp
= next
)
6839 z_candidate
*cand
= *candp
;
6842 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (cand
->fn
));
6844 for (i
= 0; i
< nargs
; ++i
)
6846 tree parmtype
= TREE_VALUE (parmlist
);
6847 tree argtype
= unlowered_expr_type ((*arglist
)[i
]);
6849 if (TYPE_REF_P (parmtype
))
6850 parmtype
= TREE_TYPE (parmtype
);
6851 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
6852 && (same_type_ignoring_top_level_qualifiers_p
6853 (argtype
, parmtype
)))
6856 parmlist
= TREE_CHAIN (parmlist
);
6859 /* No argument has an appropriate type, so remove this
6860 candidate function from the list. */
6863 *candp
= cand
->next
;
6871 /* The standard says to rewrite built-in candidates, too,
6872 but there's no point. */
6873 add_builtin_candidates (candidates
, code
, code2
, fnname
, arglist
,
6876 /* Maybe add C++20 rewritten comparison candidates. */
6877 tree_code rewrite_code
= ERROR_MARK
;
6878 if (cxx_dialect
>= cxx20
6880 && (OVERLOAD_TYPE_P (arg1_type
) || OVERLOAD_TYPE_P (arg2_type
)))
6887 case SPACESHIP_EXPR
:
6888 rewrite_code
= SPACESHIP_EXPR
;
6893 rewrite_code
= EQ_EXPR
;
6901 flags
|= LOOKUP_REWRITTEN
;
6902 if (rewrite_code
!= code
)
6903 /* Add rewritten candidates in same order. */
6904 add_operator_candidates (candidates
, rewrite_code
, ERROR_MARK
,
6905 arglist
, lookups
, flags
, complain
);
6907 z_candidate
*save_cand
= *candidates
;
6909 /* Add rewritten candidates in reverse order. */
6910 flags
|= LOOKUP_REVERSED
;
6911 vec
<tree
,va_gc
> *revlist
= make_tree_vector ();
6912 revlist
->quick_push ((*arglist
)[1]);
6913 revlist
->quick_push ((*arglist
)[0]);
6914 add_operator_candidates (candidates
, rewrite_code
, ERROR_MARK
,
6915 revlist
, lookups
, flags
, complain
);
6917 /* Release the vec if we didn't add a candidate that uses it. */
6918 for (z_candidate
*c
= *candidates
; c
!= save_cand
; c
= c
->next
)
6919 if (c
->args
== revlist
)
6924 release_tree_vector (revlist
);
6932 build_new_op (const op_location_t
&loc
, enum tree_code code
, int flags
,
6933 tree arg1
, tree arg2
, tree arg3
, tree lookups
,
6934 tree
*overload
, tsubst_flags_t complain
)
6936 struct z_candidate
*candidates
= 0, *cand
;
6937 releasing_vec arglist
;
6938 tree result
= NULL_TREE
;
6939 bool result_valid_p
= false;
6940 enum tree_code code2
= ERROR_MARK
;
6941 enum tree_code code_orig_arg1
= ERROR_MARK
;
6942 enum tree_code code_orig_arg2
= ERROR_MARK
;
6946 auto_cond_timevar
tv (TV_OVERLOAD
);
6948 if (error_operand_p (arg1
)
6949 || error_operand_p (arg2
)
6950 || error_operand_p (arg3
))
6951 return error_mark_node
;
6953 conversion_obstack_sentinel cos
;
6955 bool ismodop
= code
== MODIFY_EXPR
;
6958 code2
= TREE_CODE (arg3
);
6962 tree arg1_type
= unlowered_expr_type (arg1
);
6963 tree arg2_type
= arg2
? unlowered_expr_type (arg2
) : NULL_TREE
;
6965 arg1
= prep_operand (arg1
);
6971 case VEC_DELETE_EXPR
:
6973 /* Use build_operator_new_call and build_op_delete_call instead. */
6977 /* Use build_op_call instead. */
6980 case TRUTH_ORIF_EXPR
:
6981 case TRUTH_ANDIF_EXPR
:
6982 case TRUTH_AND_EXPR
:
6984 /* These are saved for the sake of warn_logical_operator. */
6985 code_orig_arg1
= TREE_CODE (arg1
);
6986 code_orig_arg2
= TREE_CODE (arg2
);
6994 /* These are saved for the sake of maybe_warn_bool_compare. */
6995 code_orig_arg1
= TREE_CODE (arg1_type
);
6996 code_orig_arg2
= TREE_CODE (arg2_type
);
7003 arg2
= prep_operand (arg2
);
7004 arg3
= prep_operand (arg3
);
7006 if (code
== COND_EXPR
)
7007 /* Use build_conditional_expr instead. */
7009 else if (! OVERLOAD_TYPE_P (arg1_type
)
7010 && (! arg2
|| ! OVERLOAD_TYPE_P (arg2_type
)))
7013 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
7015 arg2
= integer_zero_node
;
7016 arg2_type
= integer_type_node
;
7019 arglist
->quick_push (arg1
);
7020 if (arg2
!= NULL_TREE
)
7021 arglist
->quick_push (arg2
);
7022 if (arg3
!= NULL_TREE
)
7023 arglist
->quick_push (arg3
);
7025 result
= add_operator_candidates (&candidates
, code
, code2
, arglist
,
7026 lookups
, flags
, complain
);
7027 if (result
== error_mark_node
)
7028 return error_mark_node
;
7034 /* For these, the built-in candidates set is empty
7035 [over.match.oper]/3. We don't want non-strict matches
7036 because exact matches are always possible with built-in
7037 operators. The built-in candidate set for COMPONENT_REF
7038 would be empty too, but since there are no such built-in
7039 operators, we accept non-strict matches for them. */
7048 candidates
= splice_viable (candidates
, strict_p
, &any_viable_p
);
7053 case POSTINCREMENT_EXPR
:
7054 case POSTDECREMENT_EXPR
:
7055 /* Don't try anything fancy if we're not allowed to produce
7057 if (!(complain
& tf_error
))
7058 return error_mark_node
;
7060 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
7061 distinguish between prefix and postfix ++ and
7062 operator++() was used for both, so we allow this with
7066 tree fnname
= ovl_op_identifier (ismodop
, ismodop
? code2
: code
);
7067 const char *msg
= (flag_permissive
)
7068 ? G_("no %<%D(int)%> declared for postfix %qs,"
7069 " trying prefix operator instead")
7070 : G_("no %<%D(int)%> declared for postfix %qs");
7071 permerror (loc
, msg
, fnname
, OVL_OP_INFO (false, code
)->name
);
7074 if (!flag_permissive
)
7075 return error_mark_node
;
7077 if (code
== POSTINCREMENT_EXPR
)
7078 code
= PREINCREMENT_EXPR
;
7080 code
= PREDECREMENT_EXPR
;
7081 result
= build_new_op (loc
, code
, flags
, arg1
, NULL_TREE
,
7082 NULL_TREE
, lookups
, overload
, complain
);
7085 /* The caller will deal with these. */
7091 result_valid_p
= true;
7095 if (complain
& tf_error
)
7097 /* If one of the arguments of the operator represents
7098 an invalid use of member function pointer, try to report
7099 a meaningful error ... */
7100 if (invalid_nonstatic_memfn_p (loc
, arg1
, tf_error
)
7101 || invalid_nonstatic_memfn_p (loc
, arg2
, tf_error
)
7102 || invalid_nonstatic_memfn_p (loc
, arg3
, tf_error
))
7103 /* We displayed the error message. */;
7106 /* ... Otherwise, report the more generic
7107 "no matching operator found" error */
7108 auto_diagnostic_group d
;
7109 op_error (loc
, code
, code2
, arg1
, arg2
, arg3
, false);
7110 print_z_candidates (loc
, candidates
);
7113 result
= error_mark_node
;
7119 cand
= tourney (candidates
, complain
);
7122 if (complain
& tf_error
)
7124 auto_diagnostic_group d
;
7125 op_error (loc
, code
, code2
, arg1
, arg2
, arg3
, true);
7126 print_z_candidates (loc
, candidates
);
7128 result
= error_mark_node
;
7130 *overload
= error_mark_node
;
7132 else if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
7135 *overload
= cand
->fn
;
7137 if (resolve_args (arglist
, complain
) == NULL
)
7138 result
= error_mark_node
;
7141 tsubst_flags_t ocomplain
= complain
;
7142 if (cand
->rewritten ())
7143 /* We'll wrap this call in another one. */
7144 ocomplain
&= ~tf_decltype
;
7145 if (cand
->reversed ())
7147 /* We swapped these in add_candidate, swap them back now. */
7148 std::swap (cand
->convs
[0], cand
->convs
[1]);
7149 if (cand
->fn
== current_function_decl
)
7150 warning_at (loc
, 0, "in C++20 this comparison calls the "
7151 "current function recursively with reversed "
7154 result
= build_over_call (cand
, LOOKUP_NORMAL
, ocomplain
);
7157 if (trivial_fn_p (cand
->fn
) || DECL_IMMEDIATE_FUNCTION_P (cand
->fn
))
7158 /* There won't be a CALL_EXPR. */;
7159 else if (result
&& result
!= error_mark_node
)
7161 tree call
= extract_call_expr (result
);
7162 CALL_EXPR_OPERATOR_SYNTAX (call
) = true;
7164 /* Specify evaluation order as per P0145R2. */
7165 CALL_EXPR_ORDERED_ARGS (call
) = false;
7166 switch (op_is_ordered (code
))
7169 CALL_EXPR_REVERSE_ARGS (call
) = true;
7173 CALL_EXPR_ORDERED_ARGS (call
) = true;
7181 /* If this was a C++20 rewritten comparison, adjust the result. */
7182 if (cand
->rewritten ())
7184 /* FIXME build_min_non_dep_op_overload can't handle rewrites. */
7186 *overload
= NULL_TREE
;
7190 gcc_checking_assert (cand
->reversed ());
7193 if (result
== error_mark_node
)
7195 /* If a rewritten operator== candidate is selected by
7196 overload resolution for an operator @, its return type
7197 shall be cv bool.... */
7198 else if (TREE_CODE (TREE_TYPE (result
)) != BOOLEAN_TYPE
)
7200 if (complain
& tf_error
)
7202 auto_diagnostic_group d
;
7203 error_at (loc
, "return type of %qD is not %qs",
7205 inform (loc
, "used as rewritten candidate for "
7206 "comparison of %qT and %qT",
7207 arg1_type
, arg2_type
);
7209 result
= error_mark_node
;
7211 else if (code
== NE_EXPR
)
7212 /* !(y == x) or !(x == y) */
7213 result
= build1_loc (loc
, TRUTH_NOT_EXPR
,
7214 boolean_type_node
, result
);
7217 /* If a rewritten operator<=> candidate is selected by
7218 overload resolution for an operator @, x @ y is
7219 interpreted as 0 @ (y <=> x) if the selected candidate is
7220 a synthesized candidate with reversed order of parameters,
7221 or (x <=> y) @ 0 otherwise, using the selected rewritten
7222 operator<=> candidate. */
7223 case SPACESHIP_EXPR
:
7224 if (!cand
->reversed ())
7225 /* We're in the build_new_op call below for an outer
7226 reversed call; we don't need to do anything more. */
7235 tree rhs
= integer_zero_node
;
7236 if (cand
->reversed ())
7237 std::swap (lhs
, rhs
);
7238 warning_sentinel
ws (warn_zero_as_null_pointer_constant
);
7239 result
= build_new_op (loc
, code
,
7240 LOOKUP_NORMAL
|LOOKUP_REWRITTEN
,
7241 lhs
, rhs
, NULL_TREE
, lookups
,
7251 /* In an expression of the form `a[]' where cand->fn
7252 which is operator[] turns out to be a static member function,
7253 `a' is none-the-less evaluated. */
7254 if (code
== ARRAY_REF
)
7255 result
= keep_unused_object_arg (result
, arg1
, cand
->fn
);
7259 /* Give any warnings we noticed during overload resolution. */
7260 if (cand
->warnings
&& (complain
& tf_warning
))
7262 struct candidate_warning
*w
;
7263 for (w
= cand
->warnings
; w
; w
= w
->next
)
7264 joust (cand
, w
->loser
, 1, complain
);
7267 /* Check for comparison of different enum types. */
7276 if (TREE_CODE (arg1_type
) == ENUMERAL_TYPE
7277 && TREE_CODE (arg2_type
) == ENUMERAL_TYPE
7278 && (TYPE_MAIN_VARIANT (arg1_type
)
7279 != TYPE_MAIN_VARIANT (arg2_type
))
7280 && (complain
& tf_warning
))
7281 warning_at (loc
, OPT_Wenum_compare
,
7282 "comparison between %q#T and %q#T",
7283 arg1_type
, arg2_type
);
7289 /* "If a built-in candidate is selected by overload resolution, the
7290 operands of class type are converted to the types of the
7291 corresponding parameters of the selected operation function,
7292 except that the second standard conversion sequence of a
7293 user-defined conversion sequence (12.3.3.1.2) is not applied." */
7294 conversion
*conv
= cand
->convs
[0];
7295 if (conv
->user_conv_p
)
7297 conv
= strip_standard_conversion (conv
);
7298 arg1
= convert_like (conv
, arg1
, complain
);
7303 conv
= cand
->convs
[1];
7304 if (conv
->user_conv_p
)
7306 conv
= strip_standard_conversion (conv
);
7307 arg2
= convert_like (conv
, arg2
, complain
);
7313 conv
= cand
->convs
[2];
7314 if (conv
->user_conv_p
)
7316 conv
= strip_standard_conversion (conv
);
7317 arg3
= convert_like (conv
, arg3
, complain
);
7323 if (result
|| result_valid_p
)
7330 return cp_build_modify_expr (loc
, arg1
, code2
, arg2
, complain
);
7333 return cp_build_indirect_ref (loc
, arg1
, RO_UNARY_STAR
, complain
);
7335 case TRUTH_ANDIF_EXPR
:
7336 case TRUTH_ORIF_EXPR
:
7337 case TRUTH_AND_EXPR
:
7339 if ((complain
& tf_warning
) && !processing_template_decl
)
7340 warn_logical_operator (loc
, code
, boolean_type_node
,
7341 code_orig_arg1
, arg1
,
7342 code_orig_arg2
, arg2
);
7350 if ((complain
& tf_warning
)
7351 && ((code_orig_arg1
== BOOLEAN_TYPE
)
7352 ^ (code_orig_arg2
== BOOLEAN_TYPE
)))
7353 maybe_warn_bool_compare (loc
, code
, arg1
, arg2
);
7354 if (complain
& tf_warning
&& warn_tautological_compare
)
7355 warn_tautological_cmp (loc
, code
, arg1
, arg2
);
7357 case SPACESHIP_EXPR
:
7361 case TRUNC_DIV_EXPR
:
7366 case TRUNC_MOD_EXPR
:
7370 return cp_build_binary_op (loc
, code
, arg1
, arg2
, complain
);
7372 case UNARY_PLUS_EXPR
:
7375 case TRUTH_NOT_EXPR
:
7376 case PREINCREMENT_EXPR
:
7377 case POSTINCREMENT_EXPR
:
7378 case PREDECREMENT_EXPR
:
7379 case POSTDECREMENT_EXPR
:
7384 return cp_build_unary_op (code
, arg1
, false, complain
);
7387 return cp_build_array_ref (input_location
, arg1
, arg2
, complain
);
7390 return build_m_component_ref (cp_build_indirect_ref (loc
, arg1
,
7395 /* The caller will deal with these. */
7407 /* Build a new call to operator[]. This may change ARGS. */
7410 build_op_subscript (const op_location_t
&loc
, tree obj
,
7411 vec
<tree
, va_gc
> **args
, tree
*overload
,
7412 tsubst_flags_t complain
)
7414 struct z_candidate
*candidates
= 0, *cand
;
7415 tree fns
, first_mem_arg
= NULL_TREE
;
7417 tree result
= NULL_TREE
;
7419 auto_cond_timevar
tv (TV_OVERLOAD
);
7421 obj
= mark_lvalue_use (obj
);
7423 if (error_operand_p (obj
))
7424 return error_mark_node
;
7426 tree type
= TREE_TYPE (obj
);
7428 obj
= prep_operand (obj
);
7430 if (TYPE_BINFO (type
))
7432 fns
= lookup_fnfields (TYPE_BINFO (type
), ovl_op_identifier (ARRAY_REF
),
7434 if (fns
== error_mark_node
)
7435 return error_mark_node
;
7440 if (args
!= NULL
&& *args
!= NULL
)
7442 *args
= resolve_args (*args
, complain
);
7444 return error_mark_node
;
7447 conversion_obstack_sentinel cos
;
7451 first_mem_arg
= obj
;
7453 add_candidates (BASELINK_FUNCTIONS (fns
),
7454 first_mem_arg
, *args
, NULL_TREE
,
7456 BASELINK_BINFO (fns
), BASELINK_ACCESS_BINFO (fns
),
7457 LOOKUP_NORMAL
, &candidates
, complain
);
7460 /* Be strict here because if we choose a bad conversion candidate, the
7461 errors we get won't mention the call context. */
7462 candidates
= splice_viable (candidates
, true, &any_viable_p
);
7465 if (complain
& tf_error
)
7467 auto_diagnostic_group d
;
7468 error ("no match for call to %<%T::operator[] (%A)%>",
7469 TREE_TYPE (obj
), build_tree_list_vec (*args
));
7470 print_z_candidates (loc
, candidates
);
7472 result
= error_mark_node
;
7476 cand
= tourney (candidates
, complain
);
7479 if (complain
& tf_error
)
7481 auto_diagnostic_group d
;
7482 error ("call of %<%T::operator[] (%A)%> is ambiguous",
7483 TREE_TYPE (obj
), build_tree_list_vec (*args
));
7484 print_z_candidates (loc
, candidates
);
7486 result
= error_mark_node
;
7488 else if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
7489 && DECL_OVERLOADED_OPERATOR_P (cand
->fn
)
7490 && DECL_OVERLOADED_OPERATOR_IS (cand
->fn
, ARRAY_REF
))
7493 *overload
= cand
->fn
;
7494 result
= build_over_call (cand
, LOOKUP_NORMAL
, complain
);
7495 if (trivial_fn_p (cand
->fn
) || DECL_IMMEDIATE_FUNCTION_P (cand
->fn
))
7496 /* There won't be a CALL_EXPR. */;
7497 else if (result
&& result
!= error_mark_node
)
7499 tree call
= extract_call_expr (result
);
7500 CALL_EXPR_OPERATOR_SYNTAX (call
) = true;
7502 /* Specify evaluation order as per P0145R2. */
7503 CALL_EXPR_ORDERED_ARGS (call
) = op_is_ordered (ARRAY_REF
) == 1;
7506 /* In an expression of the form `a[]' where cand->fn
7507 which is operator[] turns out to be a static member function,
7508 `a' is none-the-less evaluated. */
7509 result
= keep_unused_object_arg (result
, obj
, cand
->fn
);
7518 /* CALL was returned by some call-building function; extract the actual
7519 CALL_EXPR from any bits that have been tacked on, e.g. by
7520 convert_from_reference. */
7523 extract_call_expr (tree call
)
7525 while (TREE_CODE (call
) == COMPOUND_EXPR
)
7526 call
= TREE_OPERAND (call
, 1);
7527 if (REFERENCE_REF_P (call
))
7528 call
= TREE_OPERAND (call
, 0);
7529 if (TREE_CODE (call
) == TARGET_EXPR
)
7530 call
= TARGET_EXPR_INITIAL (call
);
7531 if (cxx_dialect
>= cxx20
)
7532 switch (TREE_CODE (call
))
7534 /* C++20 rewritten comparison operators. */
7535 case TRUTH_NOT_EXPR
:
7536 call
= TREE_OPERAND (call
, 0);
7542 case SPACESHIP_EXPR
:
7544 tree op0
= TREE_OPERAND (call
, 0);
7545 if (integer_zerop (op0
))
7546 call
= TREE_OPERAND (call
, 1);
7554 if (TREE_CODE (call
) != CALL_EXPR
7555 && TREE_CODE (call
) != AGGR_INIT_EXPR
7556 && call
!= error_mark_node
)
7561 /* Returns true if FN has two parameters, of which the second has type
7565 second_parm_is_size_t (tree fn
)
7567 tree t
= FUNCTION_ARG_CHAIN (fn
);
7568 if (!t
|| !same_type_p (TREE_VALUE (t
), size_type_node
))
7571 if (t
== void_list_node
)
7576 /* True if T, an allocation function, has std::align_val_t as its second
7580 aligned_allocation_fn_p (tree t
)
7582 if (!aligned_new_threshold
)
7585 tree a
= FUNCTION_ARG_CHAIN (t
);
7586 return (a
&& same_type_p (TREE_VALUE (a
), align_type_node
));
7589 /* True if T is std::destroying_delete_t. */
7592 std_destroying_delete_t_p (tree t
)
7594 return (TYPE_CONTEXT (t
) == std_node
7595 && id_equal (TYPE_IDENTIFIER (t
), "destroying_delete_t"));
7598 /* A deallocation function with at least two parameters whose second parameter
7599 type is of type std::destroying_delete_t is a destroying operator delete. A
7600 destroying operator delete shall be a class member function named operator
7601 delete. [ Note: Array deletion cannot use a destroying operator
7602 delete. --end note ] */
7605 destroying_delete_p (tree t
)
7607 tree a
= TYPE_ARG_TYPES (TREE_TYPE (t
));
7608 if (!a
|| !TREE_CHAIN (a
))
7610 tree type
= TREE_VALUE (TREE_CHAIN (a
));
7611 return std_destroying_delete_t_p (type
) ? type
: NULL_TREE
;
7621 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
7622 function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
7623 non-null, also set *DI. */
7626 usual_deallocation_fn_p (tree t
, dealloc_info
*di
)
7628 if (di
) *di
= dealloc_info();
7630 /* A template instance is never a usual deallocation function,
7631 regardless of its signature. */
7632 if (TREE_CODE (t
) == TEMPLATE_DECL
7633 || primary_template_specialization_p (t
))
7636 /* A usual deallocation function is a deallocation function whose parameters
7638 - optionally, a parameter of type std::destroying_delete_t, then
7639 - optionally, a parameter of type std::size_t, then
7640 - optionally, a parameter of type std::align_val_t. */
7641 bool global
= DECL_NAMESPACE_SCOPE_P (t
);
7642 tree chain
= FUNCTION_ARG_CHAIN (t
);
7643 if (chain
&& destroying_delete_p (t
))
7645 if (di
) di
->destroying
= TREE_VALUE (chain
);
7646 chain
= TREE_CHAIN (chain
);
7649 && (!global
|| flag_sized_deallocation
)
7650 && same_type_p (TREE_VALUE (chain
), size_type_node
))
7652 if (di
) di
->sized
= true;
7653 chain
= TREE_CHAIN (chain
);
7655 if (chain
&& aligned_new_threshold
7656 && same_type_p (TREE_VALUE (chain
), align_type_node
))
7658 if (di
) di
->aligned
= true;
7659 chain
= TREE_CHAIN (chain
);
7661 return (chain
== void_list_node
);
7664 /* Just return whether FN is a usual deallocation function. */
7667 usual_deallocation_fn_p (tree fn
)
7669 return usual_deallocation_fn_p (fn
, NULL
);
7672 /* Build a call to operator delete. This has to be handled very specially,
7673 because the restrictions on what signatures match are different from all
7674 other call instances. For a normal delete, only a delete taking (void *)
7675 or (void *, size_t) is accepted. For a placement delete, only an exact
7676 match with the placement new is accepted.
7678 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
7679 ADDR is the pointer to be deleted.
7680 SIZE is the size of the memory block to be deleted.
7681 GLOBAL_P is true if the delete-expression should not consider
7682 class-specific delete operators.
7683 PLACEMENT is the corresponding placement new call, or NULL_TREE.
7685 If this call to "operator delete" is being generated as part to
7686 deallocate memory allocated via a new-expression (as per [expr.new]
7687 which requires that if the initialization throws an exception then
7688 we call a deallocation function), then ALLOC_FN is the allocation
7692 build_op_delete_call (enum tree_code code
, tree addr
, tree size
,
7693 bool global_p
, tree placement
,
7694 tree alloc_fn
, tsubst_flags_t complain
)
7696 tree fn
= NULL_TREE
;
7697 tree fns
, fnname
, type
, t
;
7698 dealloc_info di_fn
= { };
7700 if (addr
== error_mark_node
)
7701 return error_mark_node
;
7703 type
= strip_array_types (TREE_TYPE (TREE_TYPE (addr
)));
7705 fnname
= ovl_op_identifier (false, code
);
7707 if (CLASS_TYPE_P (type
)
7708 && COMPLETE_TYPE_P (complete_type (type
))
7712 If the result of the lookup is ambiguous or inaccessible, or if
7713 the lookup selects a placement deallocation function, the
7714 program is ill-formed.
7716 Therefore, we ask lookup_fnfields to complain about ambiguity. */
7718 fns
= lookup_fnfields (TYPE_BINFO (type
), fnname
, 1, complain
);
7719 if (fns
== error_mark_node
)
7720 return error_mark_node
;
7725 if (fns
== NULL_TREE
)
7726 fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
);
7728 /* Strip const and volatile from addr. */
7730 addr
= cp_convert (ptr_type_node
, addr
, complain
);
7732 tree excluded_destroying
= NULL_TREE
;
7736 /* "A declaration of a placement deallocation function matches the
7737 declaration of a placement allocation function if it has the same
7738 number of parameters and, after parameter transformations (8.3.5),
7739 all parameter types except the first are identical."
7741 So we build up the function type we want and ask instantiate_type
7742 to get it for us. */
7743 t
= FUNCTION_ARG_CHAIN (alloc_fn
);
7744 t
= tree_cons (NULL_TREE
, ptr_type_node
, t
);
7745 t
= build_function_type (void_type_node
, t
);
7747 fn
= instantiate_type (t
, fns
, tf_none
);
7748 if (fn
== error_mark_node
)
7751 fn
= MAYBE_BASELINK_FUNCTIONS (fn
);
7753 /* "If the lookup finds the two-parameter form of a usual deallocation
7754 function (3.7.4.2) and that function, considered as a placement
7755 deallocation function, would have been selected as a match for the
7756 allocation function, the program is ill-formed." */
7757 if (second_parm_is_size_t (fn
))
7759 const char *const msg1
7760 = G_("exception cleanup for this placement new selects "
7761 "non-placement %<operator delete%>");
7762 const char *const msg2
7763 = G_("%qD is a usual (non-placement) deallocation "
7764 "function in C++14 (or with %<-fsized-deallocation%>)");
7766 /* But if the class has an operator delete (void *), then that is
7767 the usual deallocation function, so we shouldn't complain
7768 about using the operator delete (void *, size_t). */
7769 if (DECL_CLASS_SCOPE_P (fn
))
7770 for (tree elt
: lkp_range (MAYBE_BASELINK_FUNCTIONS (fns
)))
7772 if (usual_deallocation_fn_p (elt
)
7773 && FUNCTION_ARG_CHAIN (elt
) == void_list_node
)
7776 /* Before C++14 a two-parameter global deallocation function is
7777 always a placement deallocation function, but warn if
7779 else if (!flag_sized_deallocation
)
7781 if (complain
& tf_warning
)
7783 auto_diagnostic_group d
;
7784 if (warning (OPT_Wc__14_compat
, msg1
))
7785 inform (DECL_SOURCE_LOCATION (fn
), msg2
, fn
);
7790 if (complain
& tf_warning_or_error
)
7792 auto_diagnostic_group d
;
7793 if (permerror (input_location
, msg1
))
7795 /* Only mention C++14 for namespace-scope delete. */
7796 if (DECL_NAMESPACE_SCOPE_P (fn
))
7797 inform (DECL_SOURCE_LOCATION (fn
), msg2
, fn
);
7799 inform (DECL_SOURCE_LOCATION (fn
),
7800 "%qD is a usual (non-placement) deallocation "
7805 return error_mark_node
;
7810 /* "Any non-placement deallocation function matches a non-placement
7811 allocation function. If the lookup finds a single matching
7812 deallocation function, that function will be called; otherwise, no
7813 deallocation function will be called." */
7814 for (tree elt
: lkp_range (MAYBE_BASELINK_FUNCTIONS (fns
)))
7816 dealloc_info di_elt
;
7817 if (usual_deallocation_fn_p (elt
, &di_elt
))
7819 /* If we're called for an EH cleanup in a new-expression, we can't
7820 use a destroying delete; the exception was thrown before the
7821 object was constructed. */
7822 if (alloc_fn
&& di_elt
.destroying
)
7824 excluded_destroying
= elt
;
7835 /* -- If any of the deallocation functions is a destroying
7836 operator delete, all deallocation functions that are not
7837 destroying operator deletes are eliminated from further
7839 if (di_elt
.destroying
!= di_fn
.destroying
)
7841 if (di_elt
.destroying
)
7849 /* -- If the type has new-extended alignment, a function with a
7850 parameter of type std::align_val_t is preferred; otherwise a
7851 function without such a parameter is preferred. If exactly one
7852 preferred function is found, that function is selected and the
7853 selection process terminates. If more than one preferred
7854 function is found, all non-preferred functions are eliminated
7855 from further consideration. */
7856 if (aligned_new_threshold
)
7858 bool want_align
= type_has_new_extended_alignment (type
);
7859 if (di_elt
.aligned
!= di_fn
.aligned
)
7861 if (want_align
== di_elt
.aligned
)
7870 /* -- If the deallocation functions have class scope, the one
7871 without a parameter of type std::size_t is selected. */
7873 if (DECL_CLASS_SCOPE_P (fn
))
7876 /* -- If the type is complete and if, for the second alternative
7877 (delete array) only, the operand is a pointer to a class type
7878 with a non-trivial destructor or a (possibly multi-dimensional)
7879 array thereof, the function with a parameter of type std::size_t
7882 -- Otherwise, it is unspecified whether a deallocation function
7883 with a parameter of type std::size_t is selected. */
7886 want_size
= COMPLETE_TYPE_P (type
);
7887 if (code
== VEC_DELETE_EXPR
7888 && !TYPE_VEC_NEW_USES_COOKIE (type
))
7889 /* We need a cookie to determine the array size. */
7892 gcc_assert (di_fn
.sized
!= di_elt
.sized
);
7893 if (want_size
== di_elt
.sized
)
7901 /* If we have a matching function, call it. */
7904 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
7906 /* If the FN is a member function, make sure that it is
7908 if (BASELINK_P (fns
))
7909 perform_or_defer_access_check (BASELINK_BINFO (fns
), fn
, fn
,
7912 /* Core issue 901: It's ok to new a type with deleted delete. */
7913 if (DECL_DELETED_FN (fn
) && alloc_fn
)
7919 /* The placement args might not be suitable for overload
7920 resolution at this point, so build the call directly. */
7921 int nargs
= call_expr_nargs (placement
);
7922 tree
*argarray
= XALLOCAVEC (tree
, nargs
);
7925 for (i
= 1; i
< nargs
; i
++)
7926 argarray
[i
] = CALL_EXPR_ARG (placement
, i
);
7927 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
7928 return error_mark_node
;
7929 ret
= build_cxx_call (fn
, nargs
, argarray
, complain
);
7933 tree destroying
= di_fn
.destroying
;
7936 /* Strip const and volatile from addr but retain the type of the
7938 tree rtype
= TREE_TYPE (TREE_TYPE (oaddr
));
7939 rtype
= cv_unqualified (rtype
);
7940 rtype
= TYPE_POINTER_TO (rtype
);
7941 addr
= cp_convert (rtype
, oaddr
, complain
);
7942 destroying
= build_functional_cast (input_location
,
7943 destroying
, NULL_TREE
,
7948 args
->quick_push (addr
);
7950 args
->quick_push (destroying
);
7952 args
->quick_push (size
);
7955 tree al
= build_int_cst (align_type_node
, TYPE_ALIGN_UNIT (type
));
7956 args
->quick_push (al
);
7958 ret
= cp_build_function_call_vec (fn
, &args
, complain
);
7961 /* Set this flag for all callers of this function. In addition to
7962 delete-expressions, this is called for deallocating coroutine state;
7963 treat that as an implicit delete-expression. This is also called for
7964 the delete if the constructor throws in a new-expression, and for a
7965 deleting destructor (which implements a delete-expression). */
7966 /* But leave this flag off for destroying delete to avoid wrong
7967 assumptions in the optimizers. */
7968 tree call
= extract_call_expr (ret
);
7969 if (TREE_CODE (call
) == CALL_EXPR
&& !destroying_delete_p (fn
))
7970 CALL_FROM_NEW_OR_DELETE_P (call
) = 1;
7975 /* If there's only a destroying delete that we can't use because the
7976 object isn't constructed yet, and we used global new, use global
7978 if (excluded_destroying
7979 && DECL_NAMESPACE_SCOPE_P (alloc_fn
))
7980 return build_op_delete_call (code
, addr
, size
, true, placement
,
7981 alloc_fn
, complain
);
7985 If no unambiguous matching deallocation function can be found,
7986 propagating the exception does not cause the object's memory to
7990 if ((complain
& tf_warning
)
7993 bool w
= warning (0,
7994 "no corresponding deallocation function for %qD",
7996 if (w
&& excluded_destroying
)
7997 inform (DECL_SOURCE_LOCATION (excluded_destroying
), "destroying "
7998 "delete %qD cannot be used to release the allocated memory"
7999 " if the initialization throws because the object is not "
8000 "constructed yet", excluded_destroying
);
8005 if (complain
& tf_error
)
8006 error ("no suitable %<operator %s%> for %qT",
8007 OVL_OP_INFO (false, code
)->name
, type
);
8008 return error_mark_node
;
8011 /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
8014 If ISSUE_ERROR is true, then issue an error about the access, followed
8015 by a note showing the declaration. Otherwise, just show the note.
8017 DIAG_DECL and DIAG_LOCATION will almost always be the same.
8018 DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
8019 parameter used to specify why DECL wasn't accessible (e.g. ak_private
8020 would be because DECL was private). If not using NO_ACCESS_REASON,
8021 then it must be ak_none, and the access failure reason will be
8022 figured out by looking at the protection of DECL. */
8025 complain_about_access (tree decl
, tree diag_decl
, tree diag_location
,
8026 bool issue_error
, access_kind no_access_reason
)
8028 /* If we have not already figured out why DECL is inaccessible... */
8029 if (no_access_reason
== ak_none
)
8031 /* Examine the access of DECL to find out why. */
8032 if (TREE_PRIVATE (decl
))
8033 no_access_reason
= ak_private
;
8034 else if (TREE_PROTECTED (decl
))
8035 no_access_reason
= ak_protected
;
8038 /* Now generate an error message depending on calculated access. */
8039 if (no_access_reason
== ak_private
)
8042 error ("%q#D is private within this context", diag_decl
);
8043 inform (DECL_SOURCE_LOCATION (diag_location
), "declared private here");
8045 else if (no_access_reason
== ak_protected
)
8048 error ("%q#D is protected within this context", diag_decl
);
8049 inform (DECL_SOURCE_LOCATION (diag_location
), "declared protected here");
8051 /* Couldn't figure out why DECL is inaccesible, so just say it's
8056 error ("%q#D is inaccessible within this context", diag_decl
);
8057 inform (DECL_SOURCE_LOCATION (diag_decl
), "declared here");
8061 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
8062 bitwise or of LOOKUP_* values. If any errors are warnings are
8063 generated, set *DIAGNOSTIC_FN to "error" or "warning",
8064 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
8068 build_temp (tree expr
, tree type
, int flags
,
8069 diagnostic_t
*diagnostic_kind
, tsubst_flags_t complain
)
8073 *diagnostic_kind
= DK_UNSPECIFIED
;
8075 /* If the source is a packed field, calling the copy constructor will require
8076 binding the field to the reference parameter to the copy constructor, and
8077 we'll end up with an infinite loop. If we can use a bitwise copy, then
8079 if ((lvalue_kind (expr
) & clk_packed
)
8080 && CLASS_TYPE_P (TREE_TYPE (expr
))
8081 && !type_has_nontrivial_copy_init (TREE_TYPE (expr
)))
8082 return get_target_expr (expr
, complain
);
8084 /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
8085 But it turns out to be a subexpression, so perform temporary
8086 materialization now. */
8087 if (TREE_CODE (expr
) == CALL_EXPR
8088 && CLASS_TYPE_P (type
)
8089 && same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
8090 expr
= build_cplus_new (type
, expr
, complain
);
8092 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
8093 releasing_vec
args (make_tree_vector_single (expr
));
8094 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
8095 &args
, type
, flags
, complain
);
8096 if (warningcount
+ werrorcount
> savew
)
8097 *diagnostic_kind
= DK_WARNING
;
8098 else if (errorcount
> savee
)
8099 *diagnostic_kind
= DK_ERROR
;
8103 /* Get any location for EXPR, falling back to input_location.
8105 If the result is in a system header and is the virtual location for
8106 a token coming from the expansion of a macro, unwind it to the
8107 location of the expansion point of the macro (e.g. to avoid the
8108 diagnostic being suppressed for expansions of NULL where "NULL" is
8109 in a system header). */
8112 get_location_for_expr_unwinding_for_system_header (tree expr
)
8114 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
8115 loc
= expansion_point_location_if_in_system_header (loc
);
8119 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
8120 Also handle a subset of zero as null warnings.
8121 EXPR is implicitly converted to type TOTYPE.
8122 FN and ARGNUM are used for diagnostics. */
8125 conversion_null_warnings (tree totype
, tree expr
, tree fn
, int argnum
)
8127 /* Issue warnings about peculiar, but valid, uses of NULL. */
8128 if (TREE_CODE (totype
) != BOOLEAN_TYPE
8129 && ARITHMETIC_TYPE_P (totype
)
8130 && null_node_p (expr
))
8132 location_t loc
= get_location_for_expr_unwinding_for_system_header (expr
);
8135 auto_diagnostic_group d
;
8136 if (warning_at (loc
, OPT_Wconversion_null
,
8137 "passing NULL to non-pointer argument %P of %qD",
8139 inform (get_fndecl_argument_location (fn
, argnum
),
8143 warning_at (loc
, OPT_Wconversion_null
,
8144 "converting to non-pointer type %qT from NULL", totype
);
8147 /* Issue warnings if "false" is converted to a NULL pointer */
8148 else if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
8149 && TYPE_PTR_P (totype
))
8151 location_t loc
= get_location_for_expr_unwinding_for_system_header (expr
);
8154 auto_diagnostic_group d
;
8155 if (warning_at (loc
, OPT_Wconversion_null
,
8156 "converting %<false%> to pointer type for argument "
8157 "%P of %qD", argnum
, fn
))
8158 inform (get_fndecl_argument_location (fn
, argnum
),
8162 warning_at (loc
, OPT_Wconversion_null
,
8163 "converting %<false%> to pointer type %qT", totype
);
8165 /* Handle zero as null pointer warnings for cases other
8166 than EQ_EXPR and NE_EXPR */
8167 else if ((TYPE_PTR_OR_PTRMEM_P (totype
) || NULLPTR_TYPE_P (totype
))
8168 && null_ptr_cst_p (expr
))
8170 location_t loc
= get_location_for_expr_unwinding_for_system_header (expr
);
8171 maybe_warn_zero_as_null_pointer_constant (expr
, loc
);
8175 /* We gave a diagnostic during a conversion. If this was in the second
8176 standard conversion sequence of a user-defined conversion sequence, say
8177 which user-defined conversion. */
8180 maybe_print_user_conv_context (conversion
*convs
)
8182 if (convs
->user_conv_p
)
8183 for (conversion
*t
= convs
; t
; t
= next_conversion (t
))
8184 if (t
->kind
== ck_user
)
8186 print_z_candidate (0, N_(" after user-defined conversion:"),
8192 /* Locate the parameter with the given index within FNDECL.
8193 ARGNUM is zero based, -1 indicates the `this' argument of a method.
8194 Return the location of the FNDECL itself if there are problems. */
8197 get_fndecl_argument_location (tree fndecl
, int argnum
)
8199 /* The locations of implicitly-declared functions are likely to be
8200 more meaningful than those of their parameters. */
8201 if (DECL_ARTIFICIAL (fndecl
))
8202 return DECL_SOURCE_LOCATION (fndecl
);
8207 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8208 for (i
= 0, param
= FUNCTION_FIRST_USER_PARM (fndecl
);
8209 i
< argnum
&& param
;
8210 i
++, param
= TREE_CHAIN (param
))
8213 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8214 return the location of FNDECL. */
8216 return DECL_SOURCE_LOCATION (fndecl
);
8218 return DECL_SOURCE_LOCATION (param
);
8221 /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8222 within its declaration (or the fndecl itself if something went
8226 maybe_inform_about_fndecl_for_bogus_argument_init (tree fn
, int argnum
)
8229 inform (get_fndecl_argument_location (fn
, argnum
),
8230 " initializing argument %P of %qD", argnum
, fn
);
8233 /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8234 the conversion, EXPR is the expression we're converting. */
8237 maybe_warn_array_conv (location_t loc
, conversion
*c
, tree expr
)
8239 if (cxx_dialect
>= cxx20
)
8242 tree type
= TREE_TYPE (expr
);
8243 type
= strip_pointer_operator (type
);
8245 if (TREE_CODE (type
) != ARRAY_TYPE
8246 || TYPE_DOMAIN (type
) == NULL_TREE
)
8249 if (pedantic
&& conv_binds_to_array_of_unknown_bound (c
))
8250 pedwarn (loc
, OPT_Wc__20_extensions
,
8251 "conversions to arrays of unknown bound "
8252 "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8255 /* We call this recursively in convert_like_internal. */
8256 static tree
convert_like (conversion
*, tree
, tree
, int, bool, bool, bool,
8259 /* Perform the conversions in CONVS on the expression EXPR. FN and
8260 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8261 indicates the `this' argument of a method. INNER is nonzero when
8262 being called to continue a conversion chain. It is negative when a
8263 reference binding will be applied, positive otherwise. If
8264 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8265 conversions will be emitted if appropriate. If C_CAST_P is true,
8266 this conversion is coming from a C-style cast; in that case,
8267 conversions to inaccessible bases are permitted. */
8270 convert_like_internal (conversion
*convs
, tree expr
, tree fn
, int argnum
,
8271 bool issue_conversion_warnings
, bool c_cast_p
,
8272 bool nested_p
, tsubst_flags_t complain
)
8274 tree totype
= convs
->type
;
8275 diagnostic_t diag_kind
;
8277 location_t loc
= cp_expr_loc_or_input_loc (expr
);
8279 if (convs
->bad_p
&& !(complain
& tf_error
))
8280 return error_mark_node
;
8283 && convs
->kind
!= ck_user
8284 && convs
->kind
!= ck_list
8285 && convs
->kind
!= ck_ambig
8286 && (convs
->kind
!= ck_ref_bind
8287 || (convs
->user_conv_p
&& next_conversion (convs
)->bad_p
))
8288 && (convs
->kind
!= ck_rvalue
8289 || SCALAR_TYPE_P (totype
))
8290 && convs
->kind
!= ck_base
)
8293 conversion
*t
= convs
;
8295 /* Give a helpful error if this is bad because of excess braces. */
8296 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
8297 && SCALAR_TYPE_P (totype
)
8298 && CONSTRUCTOR_NELTS (expr
) > 0
8299 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr
, 0)->value
))
8301 complained
= permerror (loc
, "too many braces around initializer "
8303 while (BRACE_ENCLOSED_INITIALIZER_P (expr
)
8304 && CONSTRUCTOR_NELTS (expr
) == 1)
8305 expr
= CONSTRUCTOR_ELT (expr
, 0)->value
;
8308 /* Give a helpful error if this is bad because a conversion to bool
8309 from std::nullptr_t requires direct-initialization. */
8310 if (NULLPTR_TYPE_P (TREE_TYPE (expr
))
8311 && TREE_CODE (totype
) == BOOLEAN_TYPE
)
8312 complained
= permerror (loc
, "converting to %qH from %qI requires "
8313 "direct-initialization",
8314 totype
, TREE_TYPE (expr
));
8316 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr
))
8317 && SCALAR_FLOAT_TYPE_P (totype
)
8318 && (extended_float_type_p (TREE_TYPE (expr
))
8319 || extended_float_type_p (totype
)))
8320 switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr
),
8324 if (pedwarn (loc
, 0, "converting to %qH from %qI with greater "
8325 "conversion rank", totype
, TREE_TYPE (expr
)))
8327 else if (!complained
)
8331 if (pedwarn (loc
, 0, "converting to %qH from %qI with unordered "
8332 "conversion ranks", totype
, TREE_TYPE (expr
)))
8334 else if (!complained
)
8341 for (; t
; t
= next_conversion (t
))
8343 if (t
->kind
== ck_user
&& t
->cand
->reason
)
8345 auto_diagnostic_group d
;
8346 complained
= permerror (loc
, "invalid user-defined conversion "
8347 "from %qH to %qI", TREE_TYPE (expr
),
8350 print_z_candidate (loc
, N_("candidate is:"), t
->cand
);
8351 expr
= convert_like (t
, expr
, fn
, argnum
,
8352 /*issue_conversion_warnings=*/false,
8353 /*c_cast_p=*/false, /*nested_p=*/true,
8356 else if (t
->kind
== ck_user
|| !t
->bad_p
)
8358 expr
= convert_like (t
, expr
, fn
, argnum
,
8359 /*issue_conversion_warnings=*/false,
8360 /*c_cast_p=*/false, /*nested_p=*/true,
8366 else if (t
->kind
== ck_ambig
)
8367 return convert_like (t
, expr
, fn
, argnum
,
8368 /*issue_conversion_warnings=*/false,
8369 /*c_cast_p=*/false, /*nested_p=*/true,
8371 else if (t
->kind
== ck_identity
)
8374 if (!complained
&& expr
!= error_mark_node
)
8376 range_label_for_type_mismatch
label (TREE_TYPE (expr
), totype
);
8377 gcc_rich_location
richloc (loc
, &label
);
8378 complained
= permerror (&richloc
,
8379 "invalid conversion from %qH to %qI",
8380 TREE_TYPE (expr
), totype
);
8382 if (convs
->kind
== ck_ref_bind
)
8383 expr
= convert_to_reference (totype
, expr
, CONV_IMPLICIT
,
8384 LOOKUP_NORMAL
, NULL_TREE
,
8387 expr
= cp_convert (totype
, expr
, complain
);
8388 if (complained
== 1)
8389 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
8393 if (issue_conversion_warnings
&& (complain
& tf_warning
))
8394 conversion_null_warnings (totype
, expr
, fn
, argnum
);
8396 switch (convs
->kind
)
8400 struct z_candidate
*cand
= convs
->cand
;
8403 /* We chose the surrogate function from add_conv_candidate, now we
8404 actually need to build the conversion. */
8405 cand
= build_user_type_conversion_1 (totype
, expr
,
8406 LOOKUP_NO_CONVERSION
, complain
);
8408 tree convfn
= cand
->fn
;
8410 /* When converting from an init list we consider explicit
8411 constructors, but actually trying to call one is an error. */
8412 if (DECL_NONCONVERTING_P (convfn
) && DECL_CONSTRUCTOR_P (convfn
)
8413 && BRACE_ENCLOSED_INITIALIZER_P (expr
)
8414 /* Unless this is for direct-list-initialization. */
8415 && (!CONSTRUCTOR_IS_DIRECT_INIT (expr
) || convs
->need_temporary_p
)
8416 /* And in C++98 a default constructor can't be explicit. */
8417 && cxx_dialect
>= cxx11
)
8419 if (!(complain
& tf_error
))
8420 return error_mark_node
;
8421 location_t loc
= location_of (expr
);
8422 if (CONSTRUCTOR_NELTS (expr
) == 0
8423 && FUNCTION_FIRST_USER_PARMTYPE (convfn
) != void_list_node
)
8425 auto_diagnostic_group d
;
8426 if (pedwarn (loc
, 0, "converting to %qT from initializer list "
8427 "would use explicit constructor %qD",
8430 inform (DECL_SOURCE_LOCATION (convfn
), "%qD declared here",
8432 inform (loc
, "in C++11 and above a default constructor "
8438 auto_diagnostic_group d
;
8439 error ("converting to %qT from initializer list would use "
8440 "explicit constructor %qD", totype
, convfn
);
8441 inform (DECL_SOURCE_LOCATION (convfn
), "%qD declared here",
8446 /* If we're initializing from {}, it's value-initialization. */
8447 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
8448 && CONSTRUCTOR_NELTS (expr
) == 0
8449 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype
)
8450 && !processing_template_decl
)
8452 bool direct
= CONSTRUCTOR_IS_DIRECT_INIT (expr
);
8453 if (abstract_virtuals_error (NULL_TREE
, totype
, complain
))
8454 return error_mark_node
;
8455 expr
= build_value_init (totype
, complain
);
8456 expr
= get_target_expr (expr
, complain
);
8457 if (expr
!= error_mark_node
)
8459 TARGET_EXPR_LIST_INIT_P (expr
) = true;
8460 TARGET_EXPR_DIRECT_INIT_P (expr
) = direct
;
8465 /* We don't know here whether EXPR is being used as an lvalue or
8466 rvalue, but we know it's read. */
8467 mark_exp_read (expr
);
8469 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8471 expr
= build_over_call (cand
, LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
,
8474 /* If this is a constructor or a function returning an aggr type,
8475 we need to build up a TARGET_EXPR. */
8476 if (DECL_CONSTRUCTOR_P (convfn
))
8478 expr
= build_cplus_new (totype
, expr
, complain
);
8480 /* Remember that this was list-initialization. */
8481 if (convs
->check_narrowing
&& expr
!= error_mark_node
)
8482 TARGET_EXPR_LIST_INIT_P (expr
) = true;
8488 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
8490 int nelts
= CONSTRUCTOR_NELTS (expr
);
8492 expr
= build_value_init (totype
, complain
);
8493 else if (nelts
== 1)
8494 expr
= CONSTRUCTOR_ELT (expr
, 0)->value
;
8498 expr
= mark_use (expr
, /*rvalue_p=*/!convs
->rvaluedness_matches_p
,
8499 /*read_p=*/true, UNKNOWN_LOCATION
,
8500 /*reject_builtin=*/true);
8502 if (type_unknown_p (expr
))
8503 expr
= instantiate_type (totype
, expr
, complain
);
8504 if (!nested_p
&& TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
8505 expr
= cp_convert (totype
, TREE_OPERAND (expr
, 0), complain
);
8506 if (expr
== null_node
8507 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype
))
8508 /* If __null has been converted to an integer type, we do not want to
8509 continue to warn about uses of EXPR as an integer, rather than as a
8511 expr
= build_int_cst (totype
, 0);
8514 /* We leave bad_p off ck_ambig because overload resolution considers
8515 it valid, it just fails when we try to perform it. So we need to
8516 check complain here, too. */
8517 if (complain
& tf_error
)
8519 /* Call build_user_type_conversion again for the error. */
8520 int flags
= (convs
->need_temporary_p
8521 ? LOOKUP_IMPLICIT
: LOOKUP_NORMAL
);
8522 build_user_type_conversion (totype
, convs
->u
.expr
, flags
, complain
);
8523 gcc_assert (seen_error ());
8524 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
8526 return error_mark_node
;
8530 /* Conversion to std::initializer_list<T>. */
8531 tree elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype
), 0);
8532 unsigned len
= CONSTRUCTOR_NELTS (expr
);
8535 if (tree init
= maybe_init_list_as_array (elttype
, expr
))
8537 elttype
= cp_build_qualified_type
8538 (elttype
, cp_type_quals (elttype
) | TYPE_QUAL_CONST
);
8539 array
= build_array_of_n_type (elttype
, len
);
8540 array
= build_vec_init_expr (array
, init
, complain
);
8541 array
= get_target_expr (array
);
8542 array
= cp_build_addr_expr (array
, complain
);
8546 tree val
; unsigned ix
;
8548 tree new_ctor
= build_constructor (init_list_type_node
, NULL
);
8550 /* Convert all the elements. */
8551 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), ix
, val
)
8553 tree sub
= convert_like (convs
->u
.list
[ix
], val
, fn
,
8554 argnum
, false, false,
8555 /*nested_p=*/true, complain
);
8556 if (sub
== error_mark_node
)
8558 if (!BRACE_ENCLOSED_INITIALIZER_P (val
)
8559 && !check_narrowing (TREE_TYPE (sub
), val
, complain
))
8560 return error_mark_node
;
8561 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor
),
8563 if (!TREE_CONSTANT (sub
))
8564 TREE_CONSTANT (new_ctor
) = false;
8566 /* Build up the array. */
8567 elttype
= cp_build_qualified_type
8568 (elttype
, cp_type_quals (elttype
) | TYPE_QUAL_CONST
);
8569 array
= build_array_of_n_type (elttype
, len
);
8570 array
= finish_compound_literal (array
, new_ctor
, complain
);
8571 /* This is dubious now, should be blessed by P2752. */
8572 DECL_MERGEABLE (TARGET_EXPR_SLOT (array
)) = true;
8573 /* Take the address explicitly rather than via decay_conversion
8574 to avoid the error about taking the address of a temporary. */
8575 array
= cp_build_addr_expr (array
, complain
);
8578 array
= nullptr_node
;
8580 array
= cp_convert (build_pointer_type (elttype
), array
, complain
);
8581 if (array
== error_mark_node
)
8582 return error_mark_node
;
8584 /* Build up the initializer_list object. Note: fail gracefully
8585 if the object cannot be completed because, for example, no
8586 definition is provided (c++/80956). */
8587 totype
= complete_type_or_maybe_complain (totype
, NULL_TREE
, complain
);
8589 return error_mark_node
;
8590 tree field
= next_aggregate_field (TYPE_FIELDS (totype
));
8591 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
8592 CONSTRUCTOR_APPEND_ELT (vec
, field
, array
);
8593 field
= next_aggregate_field (DECL_CHAIN (field
));
8594 CONSTRUCTOR_APPEND_ELT (vec
, field
, size_int (len
));
8595 tree new_ctor
= build_constructor (totype
, vec
);
8596 return get_target_expr (new_ctor
, complain
);
8600 if (TREE_CODE (totype
) == COMPLEX_TYPE
)
8602 tree real
= CONSTRUCTOR_ELT (expr
, 0)->value
;
8603 tree imag
= CONSTRUCTOR_ELT (expr
, 1)->value
;
8604 real
= perform_implicit_conversion (TREE_TYPE (totype
),
8606 imag
= perform_implicit_conversion (TREE_TYPE (totype
),
8608 expr
= build2 (COMPLEX_EXPR
, totype
, real
, imag
);
8611 expr
= reshape_init (totype
, expr
, complain
);
8612 expr
= get_target_expr (digest_init (totype
, expr
, complain
),
8614 if (expr
!= error_mark_node
)
8615 TARGET_EXPR_LIST_INIT_P (expr
) = true;
8622 expr
= convert_like (next_conversion (convs
), expr
, fn
, argnum
,
8623 convs
->kind
== ck_ref_bind
8624 ? issue_conversion_warnings
: false,
8625 c_cast_p
, /*nested_p=*/true, complain
& ~tf_no_cleanup
);
8626 if (expr
== error_mark_node
)
8627 return error_mark_node
;
8629 switch (convs
->kind
)
8632 expr
= decay_conversion (expr
, complain
);
8633 if (expr
== error_mark_node
)
8635 if (complain
& tf_error
)
8637 auto_diagnostic_group d
;
8638 maybe_print_user_conv_context (convs
);
8639 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
8641 return error_mark_node
;
8644 if (! MAYBE_CLASS_TYPE_P (totype
))
8647 /* Don't introduce copies when passing arguments along to the inherited
8649 if (current_function_decl
8650 && flag_new_inheriting_ctors
8651 && DECL_INHERITED_CTOR (current_function_decl
))
8654 if (TREE_CODE (expr
) == TARGET_EXPR
8655 && TARGET_EXPR_LIST_INIT_P (expr
))
8656 /* Copy-list-initialization doesn't actually involve a copy. */
8661 if (convs
->kind
== ck_base
&& !convs
->need_temporary_p
)
8663 /* We are going to bind a reference directly to a base-class
8664 subobject of EXPR. */
8665 /* Build an expression for `*((base*) &expr)'. */
8666 expr
= convert_to_base (expr
, totype
,
8667 !c_cast_p
, /*nonnull=*/true, complain
);
8671 /* Copy-initialization where the cv-unqualified version of the source
8672 type is the same class as, or a derived class of, the class of the
8673 destination [is treated as direct-initialization]. [dcl.init] */
8674 flags
= LOOKUP_NORMAL
;
8675 /* This conversion is being done in the context of a user-defined
8676 conversion (i.e. the second step of copy-initialization), so
8677 don't allow any more. */
8678 if (convs
->user_conv_p
)
8679 flags
|= LOOKUP_NO_CONVERSION
;
8680 /* We might be performing a conversion of the argument
8681 to the user-defined conversion, i.e., not a conversion of the
8682 result of the user-defined conversion. In which case we skip
8683 explicit constructors. */
8684 if (convs
->copy_init_p
)
8685 flags
|= LOOKUP_ONLYCONVERTING
;
8686 expr
= build_temp (expr
, totype
, flags
, &diag_kind
, complain
);
8687 if (diag_kind
&& complain
)
8689 auto_diagnostic_group d
;
8690 maybe_print_user_conv_context (convs
);
8691 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
8694 return build_cplus_new (totype
, expr
, complain
);
8698 tree ref_type
= totype
;
8700 /* direct_reference_binding might have inserted a ck_qual under
8701 this ck_ref_bind for the benefit of conversion sequence ranking.
8702 Ignore the conversion; we'll create our own below. */
8703 if (next_conversion (convs
)->kind
== ck_qual
8704 && !convs
->need_temporary_p
)
8706 gcc_assert (same_type_p (TREE_TYPE (expr
),
8707 next_conversion (convs
)->type
));
8708 /* Strip the cast created by the ck_qual; cp_build_addr_expr
8709 below expects an lvalue. */
8713 if (convs
->bad_p
&& !next_conversion (convs
)->bad_p
)
8715 tree extype
= TREE_TYPE (expr
);
8716 auto_diagnostic_group d
;
8717 if (TYPE_REF_IS_RVALUE (ref_type
)
8719 error_at (loc
, "cannot bind rvalue reference of type %qH to "
8720 "lvalue of type %qI", totype
, extype
);
8721 else if (!TYPE_REF_IS_RVALUE (ref_type
) && !lvalue_p (expr
)
8722 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type
)))
8724 conversion
*next
= next_conversion (convs
);
8725 if (next
->kind
== ck_std
)
8727 next
= next_conversion (next
);
8728 error_at (loc
, "cannot bind non-const lvalue reference of "
8729 "type %qH to a value of type %qI",
8730 totype
, next
->type
);
8732 else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type
)))
8733 error_at (loc
, "cannot bind non-const lvalue reference of "
8734 "type %qH to an rvalue of type %qI", totype
, extype
);
8735 else // extype is volatile
8736 error_at (loc
, "cannot bind lvalue reference of type "
8737 "%qH to an rvalue of type %qI", totype
,
8740 else if (!reference_compatible_p (TREE_TYPE (totype
), extype
))
8742 /* If we're converting from T[] to T[N], don't talk
8743 about discarding qualifiers. (Converting from T[N] to
8744 T[] is allowed by P0388R4.) */
8745 if (TREE_CODE (extype
) == ARRAY_TYPE
8746 && TYPE_DOMAIN (extype
) == NULL_TREE
8747 && TREE_CODE (TREE_TYPE (totype
)) == ARRAY_TYPE
8748 && TYPE_DOMAIN (TREE_TYPE (totype
)) != NULL_TREE
)
8749 error_at (loc
, "cannot bind reference of type %qH to %qI "
8750 "due to different array bounds", totype
, extype
);
8752 error_at (loc
, "binding reference of type %qH to %qI "
8753 "discards qualifiers", totype
, extype
);
8757 maybe_print_user_conv_context (convs
);
8758 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
8760 return error_mark_node
;
8762 else if (complain
& tf_warning
)
8763 maybe_warn_array_conv (loc
, convs
, expr
);
8765 /* If necessary, create a temporary.
8767 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
8768 that need temporaries, even when their types are reference
8769 compatible with the type of reference being bound, so the
8770 upcoming call to cp_build_addr_expr doesn't fail. */
8771 if (convs
->need_temporary_p
8772 || TREE_CODE (expr
) == CONSTRUCTOR
8773 || TREE_CODE (expr
) == VA_ARG_EXPR
)
8775 /* Otherwise, a temporary of type "cv1 T1" is created and
8776 initialized from the initializer expression using the rules
8777 for a non-reference copy-initialization (8.5). */
8779 tree type
= TREE_TYPE (ref_type
);
8780 cp_lvalue_kind lvalue
= lvalue_kind (expr
);
8782 gcc_assert (similar_type_p (type
, next_conversion (convs
)->type
));
8783 if (!CP_TYPE_CONST_NON_VOLATILE_P (type
)
8784 && !TYPE_REF_IS_RVALUE (ref_type
))
8786 /* If the reference is volatile or non-const, we
8787 cannot create a temporary. */
8788 if (complain
& tf_error
)
8790 if (lvalue
& clk_bitfield
)
8791 error_at (loc
, "cannot bind bit-field %qE to %qT",
8793 else if (lvalue
& clk_packed
)
8794 error_at (loc
, "cannot bind packed field %qE to %qT",
8797 error_at (loc
, "cannot bind rvalue %qE to %qT",
8800 return error_mark_node
;
8802 /* If the source is a packed field, and we must use a copy
8803 constructor, then building the target expr will require
8804 binding the field to the reference parameter to the
8805 copy constructor, and we'll end up with an infinite
8806 loop. If we can use a bitwise copy, then we'll be
8808 if ((lvalue
& clk_packed
)
8809 && CLASS_TYPE_P (type
)
8810 && type_has_nontrivial_copy_init (type
))
8812 error_at (loc
, "cannot bind packed field %qE to %qT",
8814 return error_mark_node
;
8816 if (lvalue
& clk_bitfield
)
8818 expr
= convert_bitfield_to_declared_type (expr
);
8819 expr
= fold_convert (type
, expr
);
8822 /* Creating &TARGET_EXPR<> in a template would break when
8823 tsubsting the expression, so use an IMPLICIT_CONV_EXPR
8824 instead. This can happen even when there's no class
8825 involved, e.g., when converting an integer to a reference
8827 if (processing_template_decl
)
8828 return build1 (IMPLICIT_CONV_EXPR
, totype
, expr
);
8829 expr
= build_target_expr_with_type (expr
, type
, complain
);
8832 /* Take the address of the thing to which we will bind the
8834 expr
= cp_build_addr_expr (expr
, complain
);
8835 if (expr
== error_mark_node
)
8836 return error_mark_node
;
8838 /* Convert it to a pointer to the type referred to by the
8839 reference. This will adjust the pointer if a derived to
8840 base conversion is being performed. */
8841 expr
= cp_convert (build_pointer_type (TREE_TYPE (ref_type
)),
8843 /* Convert the pointer to the desired reference type. */
8844 return build_nop (ref_type
, expr
);
8848 return decay_conversion (expr
, complain
);
8851 /* ??? Should the address of a transaction-safe pointer point to the TM
8852 clone, and this conversion look up the primary function? */
8853 return build_nop (totype
, expr
);
8856 /* Warn about deprecated conversion if appropriate. */
8857 if (complain
& tf_warning
)
8859 string_conv_p (totype
, expr
, 1);
8860 maybe_warn_array_conv (loc
, convs
, expr
);
8866 expr
= convert_to_base (expr
, totype
, !c_cast_p
,
8867 /*nonnull=*/false, complain
);
8868 return build_nop (totype
, expr
);
8871 return convert_ptrmem (totype
, expr
, /*allow_inverse_p=*/false,
8872 c_cast_p
, complain
);
8878 if (convs
->check_narrowing
8879 && !check_narrowing (totype
, expr
, complain
,
8880 convs
->check_narrowing_const_only
))
8881 return error_mark_node
;
8883 warning_sentinel
w (warn_zero_as_null_pointer_constant
);
8884 if (issue_conversion_warnings
)
8885 expr
= cp_convert_and_check (totype
, expr
, complain
);
8888 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
8889 expr
= TREE_OPERAND (expr
, 0);
8890 expr
= cp_convert (totype
, expr
, complain
);
8896 /* Return true if converting FROM to TO is unsafe in a template. */
8899 conv_unsafe_in_template_p (tree to
, tree from
)
8901 /* Converting classes involves TARGET_EXPR. */
8902 if (CLASS_TYPE_P (to
) || CLASS_TYPE_P (from
))
8905 /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
8907 if (SCALAR_FLOAT_TYPE_P (from
) && INTEGRAL_OR_ENUMERATION_TYPE_P (to
))
8910 /* Converting integer to real isn't a trivial conversion, either. */
8911 if (INTEGRAL_OR_ENUMERATION_TYPE_P (from
) && SCALAR_FLOAT_TYPE_P (to
))
8917 /* Wrapper for convert_like_internal that handles creating
8918 IMPLICIT_CONV_EXPR. */
8921 convert_like (conversion
*convs
, tree expr
, tree fn
, int argnum
,
8922 bool issue_conversion_warnings
, bool c_cast_p
, bool nested_p
,
8923 tsubst_flags_t complain
)
8925 /* Creating &TARGET_EXPR<> in a template breaks when substituting,
8926 and creating a CALL_EXPR in a template breaks in finish_call_expr
8927 so use an IMPLICIT_CONV_EXPR for this conversion. We would have
8928 created such codes e.g. when calling a user-defined conversion
8930 tree conv_expr
= NULL_TREE
;
8931 if (processing_template_decl
8932 && convs
->kind
!= ck_identity
8933 && conv_unsafe_in_template_p (convs
->type
, TREE_TYPE (expr
)))
8935 conv_expr
= build1 (IMPLICIT_CONV_EXPR
, convs
->type
, expr
);
8936 if (convs
->kind
!= ck_ref_bind
)
8937 conv_expr
= convert_from_reference (conv_expr
);
8940 /* Do the normal processing to give the bad_p errors. But we still
8941 need to return the IMPLICIT_CONV_EXPR, unless we're returning
8944 expr
= convert_like_internal (convs
, expr
, fn
, argnum
,
8945 issue_conversion_warnings
, c_cast_p
,
8946 nested_p
, complain
);
8947 if (expr
== error_mark_node
)
8948 return error_mark_node
;
8949 return conv_expr
? conv_expr
: expr
;
8952 /* Convenience wrapper for convert_like. */
8955 convert_like (conversion
*convs
, tree expr
, tsubst_flags_t complain
)
8957 return convert_like (convs
, expr
, NULL_TREE
, 0,
8958 /*issue_conversion_warnings=*/true,
8959 /*c_cast_p=*/false, /*nested_p=*/false, complain
);
8962 /* Convenience wrapper for convert_like. */
8965 convert_like_with_context (conversion
*convs
, tree expr
, tree fn
, int argnum
,
8966 tsubst_flags_t complain
)
8968 return convert_like (convs
, expr
, fn
, argnum
,
8969 /*issue_conversion_warnings=*/true,
8970 /*c_cast_p=*/false, /*nested_p=*/false, complain
);
8973 /* ARG is being passed to a varargs function. Perform any conversions
8974 required. Return the converted value. */
8977 convert_arg_to_ellipsis (tree arg
, tsubst_flags_t complain
)
8979 tree arg_type
= TREE_TYPE (arg
);
8980 location_t loc
= cp_expr_loc_or_input_loc (arg
);
8984 If the argument has integral or enumeration type that is subject
8985 to the integral promotions (_conv.prom_), or a floating-point
8986 type that is subject to the floating-point promotion
8987 (_conv.fpprom_), the value of the argument is converted to the
8988 promoted type before the call. */
8989 if (SCALAR_FLOAT_TYPE_P (arg_type
)
8990 && (TYPE_PRECISION (arg_type
)
8991 < TYPE_PRECISION (double_type_node
))
8992 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type
))
8993 && !extended_float_type_p (arg_type
))
8995 if ((complain
& tf_warning
)
8996 && warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
8997 warning_at (loc
, OPT_Wdouble_promotion
,
8998 "implicit conversion from %qH to %qI when passing "
8999 "argument to function",
9000 arg_type
, double_type_node
);
9001 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
9002 arg
= TREE_OPERAND (arg
, 0);
9003 arg
= mark_rvalue_use (arg
);
9004 arg
= convert_to_real_nofold (double_type_node
, arg
);
9006 else if (NULLPTR_TYPE_P (arg_type
))
9008 arg
= mark_rvalue_use (arg
);
9009 if (TREE_SIDE_EFFECTS (arg
))
9011 warning_sentinel
w(warn_unused_result
);
9012 arg
= cp_build_compound_expr (arg
, null_pointer_node
, complain
);
9015 arg
= null_pointer_node
;
9017 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type
))
9019 if (SCOPED_ENUM_P (arg_type
))
9021 tree prom
= cp_convert (ENUM_UNDERLYING_TYPE (arg_type
), arg
,
9023 prom
= cp_perform_integral_promotions (prom
, complain
);
9024 if (abi_version_crosses (6)
9025 && TYPE_MODE (TREE_TYPE (prom
)) != TYPE_MODE (arg_type
)
9026 && (complain
& tf_warning
))
9027 warning_at (loc
, OPT_Wabi
, "scoped enum %qT passed through %<...%>"
9028 " as %qT before %<-fabi-version=6%>, %qT after",
9030 TREE_TYPE (prom
), ENUM_UNDERLYING_TYPE (arg_type
));
9031 if (!abi_version_at_least (6))
9035 arg
= cp_perform_integral_promotions (arg
, complain
);
9040 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9041 standard conversions are performed. */
9042 arg
= decay_conversion (arg
, complain
);
9044 arg
= require_complete_type (arg
, complain
);
9045 arg_type
= TREE_TYPE (arg
);
9047 if (arg
!= error_mark_node
9048 /* In a template (or ill-formed code), we can have an incomplete type
9049 even after require_complete_type, in which case we don't know
9050 whether it has trivial copy or not. */
9051 && COMPLETE_TYPE_P (arg_type
)
9052 && !cp_unevaluated_operand
)
9054 /* [expr.call] 5.2.2/7:
9055 Passing a potentially-evaluated argument of class type (Clause 9)
9056 with a non-trivial copy constructor or a non-trivial destructor
9057 with no corresponding parameter is conditionally-supported, with
9058 implementation-defined semantics.
9060 We support it as pass-by-invisible-reference, just like a normal
9063 If the call appears in the context of a sizeof expression,
9064 it is not potentially-evaluated. */
9065 if (type_has_nontrivial_copy_init (arg_type
)
9066 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type
))
9068 arg
= force_rvalue (arg
, complain
);
9069 if (complain
& tf_warning
)
9070 warning (OPT_Wconditionally_supported
,
9071 "passing objects of non-trivially-copyable "
9072 "type %q#T through %<...%> is conditionally supported",
9074 return build1 (ADDR_EXPR
, build_reference_type (arg_type
), arg
);
9076 /* Build up a real lvalue-to-rvalue conversion in case the
9077 copy constructor is trivial but not callable. */
9078 else if (CLASS_TYPE_P (arg_type
))
9079 force_rvalue (arg
, complain
);
9086 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
9089 build_x_va_arg (location_t loc
, tree expr
, tree type
)
9091 if (processing_template_decl
)
9093 tree r
= build_min (VA_ARG_EXPR
, type
, expr
);
9094 SET_EXPR_LOCATION (r
, loc
);
9098 type
= complete_type_or_else (type
, NULL_TREE
);
9100 if (expr
== error_mark_node
|| !type
)
9101 return error_mark_node
;
9103 expr
= mark_lvalue_use (expr
);
9105 if (TYPE_REF_P (type
))
9107 error ("cannot receive reference type %qT through %<...%>", type
);
9108 return error_mark_node
;
9111 if (type_has_nontrivial_copy_init (type
)
9112 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
9114 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
9115 it as pass by invisible reference. */
9116 warning_at (loc
, OPT_Wconditionally_supported
,
9117 "receiving objects of non-trivially-copyable type %q#T "
9118 "through %<...%> is conditionally-supported", type
);
9120 tree ref
= cp_build_reference_type (type
, false);
9121 expr
= build_va_arg (loc
, expr
, ref
);
9122 return convert_from_reference (expr
);
9125 tree ret
= build_va_arg (loc
, expr
, type
);
9126 if (CLASS_TYPE_P (type
))
9127 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
9128 know how to handle it. */
9129 ret
= get_target_expr (ret
);
9133 /* TYPE has been given to va_arg. Apply the default conversions which
9134 would have happened when passed via ellipsis. Return the promoted
9135 type, or the passed type if there is no change. */
9138 cxx_type_promotes_to (tree type
)
9142 /* Perform the array-to-pointer and function-to-pointer
9144 type
= type_decays_to (type
);
9146 promote
= type_promotes_to (type
);
9147 if (same_type_p (type
, promote
))
9153 /* ARG is a default argument expression being passed to a parameter of
9154 the indicated TYPE, which is a parameter to FN. PARMNUM is the
9155 zero-based argument number. Do any required conversions. Return
9156 the converted value. */
9158 static GTY(()) vec
<tree
, va_gc
> *default_arg_context
;
9160 push_defarg_context (tree fn
)
9161 { vec_safe_push (default_arg_context
, fn
); }
9164 pop_defarg_context (void)
9165 { default_arg_context
->pop (); }
9168 convert_default_arg (tree type
, tree arg
, tree fn
, int parmnum
,
9169 tsubst_flags_t complain
)
9174 /* See through clones. */
9175 fn
= DECL_ORIGIN (fn
);
9176 /* And inheriting ctors. */
9177 if (flag_new_inheriting_ctors
)
9178 fn
= strip_inheriting_ctors (fn
);
9180 /* Detect recursion. */
9181 FOR_EACH_VEC_SAFE_ELT (default_arg_context
, i
, t
)
9184 if (complain
& tf_error
)
9185 error ("recursive evaluation of default argument for %q#D", fn
);
9186 return error_mark_node
;
9189 /* If the ARG is an unparsed default argument expression, the
9190 conversion cannot be performed. */
9191 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
9193 if (complain
& tf_error
)
9194 error ("call to %qD uses the default argument for parameter %P, which "
9195 "is not yet defined", fn
, parmnum
);
9196 return error_mark_node
;
9199 push_defarg_context (fn
);
9201 if (fn
&& DECL_TEMPLATE_INFO (fn
))
9202 arg
= tsubst_default_argument (fn
, parmnum
, type
, arg
, complain
);
9208 The names in the expression are bound, and the semantic
9209 constraints are checked, at the point where the default
9210 expressions appears.
9212 we must not perform access checks here. */
9213 push_deferring_access_checks (dk_no_check
);
9214 /* We must make a copy of ARG, in case subsequent processing
9215 alters any part of it. */
9216 arg
= break_out_target_exprs (arg
, /*clear location*/true);
9218 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_IMPLICIT
,
9219 ICR_DEFAULT_ARGUMENT
, fn
, parmnum
,
9221 arg
= convert_for_arg_passing (type
, arg
, complain
);
9222 pop_deferring_access_checks();
9224 pop_defarg_context ();
9229 /* Returns the type which will really be used for passing an argument of
9233 type_passed_as (tree type
)
9235 /* Pass classes with copy ctors by invisible reference. */
9236 if (TREE_ADDRESSABLE (type
))
9237 type
= build_reference_type (type
);
9238 else if (targetm
.calls
.promote_prototypes (NULL_TREE
)
9239 && INTEGRAL_TYPE_P (type
)
9240 && COMPLETE_TYPE_P (type
)
9241 && tree_int_cst_lt (TYPE_SIZE (type
), TYPE_SIZE (integer_type_node
)))
9242 type
= integer_type_node
;
9247 /* Actually perform the appropriate conversion. */
9250 convert_for_arg_passing (tree type
, tree val
, tsubst_flags_t complain
)
9254 /* If VAL is a bitfield, then -- since it has already been converted
9255 to TYPE -- it cannot have a precision greater than TYPE.
9257 If it has a smaller precision, we must widen it here. For
9258 example, passing "int f:3;" to a function expecting an "int" will
9259 not result in any conversion before this point.
9261 If the precision is the same we must not risk widening. For
9262 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9263 often have type "int", even though the C++ type for the field is
9264 "long long". If the value is being passed to a function
9265 expecting an "int", then no conversions will be required. But,
9266 if we call convert_bitfield_to_declared_type, the bitfield will
9267 be converted to "long long". */
9268 bitfield_type
= is_bitfield_expr_with_lowered_type (val
);
9270 && TYPE_PRECISION (TREE_TYPE (val
)) < TYPE_PRECISION (type
))
9271 val
= convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type
), val
);
9273 if (val
== error_mark_node
)
9275 /* Pass classes with copy ctors by invisible reference. */
9276 else if (TREE_ADDRESSABLE (type
))
9277 val
= build1 (ADDR_EXPR
, build_reference_type (type
), val
);
9278 else if (targetm
.calls
.promote_prototypes (NULL_TREE
)
9279 && INTEGRAL_TYPE_P (type
)
9280 && COMPLETE_TYPE_P (type
)
9281 && tree_int_cst_lt (TYPE_SIZE (type
), TYPE_SIZE (integer_type_node
)))
9282 val
= cp_perform_integral_promotions (val
, complain
);
9283 if (complain
& tf_warning
)
9285 if (warn_suggest_attribute_format
)
9287 tree rhstype
= TREE_TYPE (val
);
9288 const enum tree_code coder
= TREE_CODE (rhstype
);
9289 const enum tree_code codel
= TREE_CODE (type
);
9290 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
9292 && check_missing_format_attribute (type
, rhstype
))
9293 warning (OPT_Wsuggest_attribute_format
,
9294 "argument of function call might be a candidate "
9295 "for a format attribute");
9297 maybe_warn_parm_abi (type
, cp_expr_loc_or_input_loc (val
));
9300 if (complain
& tf_warning
)
9301 warn_for_address_or_pointer_of_packed_member (type
, val
);
9306 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9307 which just decay_conversion or no conversions at all should be done.
9308 This is true for some builtins which don't act like normal functions.
9309 Return 2 if just decay_conversion and removal of excess precision should
9310 be done, 1 if just decay_conversion. Return 3 for special treatment of
9311 the 3rd argument for __builtin_*_overflow_p. */
9314 magic_varargs_p (tree fn
)
9316 if (DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
9317 switch (DECL_FUNCTION_CODE (fn
))
9319 case BUILT_IN_CLASSIFY_TYPE
:
9320 case BUILT_IN_CONSTANT_P
:
9321 case BUILT_IN_NEXT_ARG
:
9322 case BUILT_IN_VA_START
:
9325 case BUILT_IN_ADD_OVERFLOW_P
:
9326 case BUILT_IN_SUB_OVERFLOW_P
:
9327 case BUILT_IN_MUL_OVERFLOW_P
:
9330 case BUILT_IN_ISFINITE
:
9331 case BUILT_IN_ISINF
:
9332 case BUILT_IN_ISINF_SIGN
:
9333 case BUILT_IN_ISNAN
:
9334 case BUILT_IN_ISNORMAL
:
9335 case BUILT_IN_FPCLASSIFY
:
9339 return lookup_attribute ("type generic",
9340 TYPE_ATTRIBUTES (TREE_TYPE (fn
))) != 0;
9346 /* Returns the decl of the dispatcher function if FN is a function version. */
9349 get_function_version_dispatcher (tree fn
)
9351 tree dispatcher_decl
= NULL
;
9353 if (DECL_LOCAL_DECL_P (fn
))
9354 fn
= DECL_LOCAL_DECL_ALIAS (fn
);
9356 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
9357 && DECL_FUNCTION_VERSIONED (fn
));
9359 gcc_assert (targetm
.get_function_versions_dispatcher
);
9360 dispatcher_decl
= targetm
.get_function_versions_dispatcher (fn
);
9362 if (dispatcher_decl
== NULL
)
9364 error_at (input_location
, "use of multiversioned function "
9365 "without a default");
9369 retrofit_lang_decl (dispatcher_decl
);
9370 gcc_assert (dispatcher_decl
!= NULL
);
9371 return dispatcher_decl
;
9374 /* fn is a function version dispatcher that is marked used. Mark all the
9375 semantically identical function versions it will dispatch as used. */
9378 mark_versions_used (tree fn
)
9380 struct cgraph_node
*node
;
9381 struct cgraph_function_version_info
*node_v
;
9382 struct cgraph_function_version_info
*it_v
;
9384 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9386 node
= cgraph_node::get (fn
);
9390 gcc_assert (node
->dispatcher_function
);
9392 node_v
= node
->function_version ();
9396 /* All semantically identical versions are chained. Traverse and mark each
9397 one of them as used. */
9398 it_v
= node_v
->next
;
9399 while (it_v
!= NULL
)
9401 mark_used (it_v
->this_node
->decl
);
9406 /* Build a call to "the copy constructor" for the type of A, even if it
9407 wouldn't be selected by normal overload resolution. Used for
9411 call_copy_ctor (tree a
, tsubst_flags_t complain
)
9413 tree ctype
= TYPE_MAIN_VARIANT (TREE_TYPE (a
));
9414 tree binfo
= TYPE_BINFO (ctype
);
9415 tree copy
= get_copy_ctor (ctype
, complain
);
9416 copy
= build_baselink (binfo
, binfo
, copy
, NULL_TREE
);
9417 tree ob
= build_dummy_object (ctype
);
9418 releasing_vec
args (make_tree_vector_single (a
));
9419 tree r
= build_new_method_call (ob
, copy
, &args
, NULL_TREE
,
9420 LOOKUP_NORMAL
, NULL
, complain
);
9424 /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9427 base_ctor_for (tree complete_ctor
)
9430 FOR_EACH_CLONE (clone
, DECL_CLONED_FUNCTION (complete_ctor
))
9431 if (DECL_BASE_CONSTRUCTOR_P (clone
))
9436 /* Try to make EXP suitable to be used as the initializer for a base subobject,
9437 and return whether we were successful. EXP must have already been cleared
9438 by unsafe_copy_elision_p{,_opt}. */
9441 make_base_init_ok (tree exp
)
9443 if (TREE_CODE (exp
) == TARGET_EXPR
)
9444 exp
= TARGET_EXPR_INITIAL (exp
);
9445 while (TREE_CODE (exp
) == COMPOUND_EXPR
)
9446 exp
= TREE_OPERAND (exp
, 1);
9447 if (TREE_CODE (exp
) == COND_EXPR
)
9449 bool ret
= make_base_init_ok (TREE_OPERAND (exp
, 2));
9450 if (tree op1
= TREE_OPERAND (exp
, 1))
9452 bool r1
= make_base_init_ok (op1
);
9453 /* If unsafe_copy_elision_p was false, the arms should match. */
9454 gcc_assert (r1
== ret
);
9458 if (TREE_CODE (exp
) != AGGR_INIT_EXPR
)
9459 /* A trivial copy is OK. */
9461 if (!AGGR_INIT_VIA_CTOR_P (exp
))
9462 /* unsafe_copy_elision_p_opt must have said this is OK. */
9464 tree fn
= cp_get_callee_fndecl_nofold (exp
);
9465 if (DECL_BASE_CONSTRUCTOR_P (fn
))
9467 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn
));
9468 fn
= base_ctor_for (fn
);
9469 if (!fn
|| DECL_HAS_VTT_PARM_P (fn
))
9470 /* The base constructor has more parameters, so we can't just change the
9471 call target. It would be possible to splice in the appropriate
9472 arguments, but probably not worth the complexity. */
9475 AGGR_INIT_EXPR_FN (exp
) = build_address (fn
);
9479 /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
9480 neither of which can be used for return by invisible reference. We avoid
9481 doing C++17 mandatory copy elision for either of these cases.
9483 This returns non-zero even if the type of T has no tail padding that other
9484 data could be allocated into, because that depends on the particular ABI.
9485 unsafe_copy_elision_p_opt does consider whether there is padding. */
9488 unsafe_return_slot_p (tree t
)
9490 /* Check empty bases separately, they don't have fields. */
9491 if (is_empty_base_ref (t
))
9494 /* A delegating constructor might be used to initialize a base. */
9495 if (current_function_decl
9496 && DECL_CONSTRUCTOR_P (current_function_decl
)
9497 && (t
== current_class_ref
9498 || tree_strip_nop_conversions (t
) == current_class_ptr
))
9502 if (TREE_CODE (t
) == ADDR_EXPR
)
9503 t
= TREE_OPERAND (t
, 0);
9504 if (TREE_CODE (t
) == COMPONENT_REF
)
9505 t
= TREE_OPERAND (t
, 1);
9506 if (TREE_CODE (t
) != FIELD_DECL
)
9508 if (!CLASS_TYPE_P (TREE_TYPE (t
)))
9509 /* The middle-end will do the right thing for scalar types. */
9511 if (DECL_FIELD_IS_BASE (t
))
9513 if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t
)))
9518 /* True IFF EXP is a prvalue that represents return by invisible reference. */
9521 init_by_return_slot_p (tree exp
)
9523 /* Copy elision only happens with a TARGET_EXPR. */
9524 if (TREE_CODE (exp
) != TARGET_EXPR
)
9526 tree init
= TARGET_EXPR_INITIAL (exp
);
9527 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
9528 while (TREE_CODE (init
) == COMPOUND_EXPR
)
9529 init
= TREE_OPERAND (init
, 1);
9530 if (TREE_CODE (init
) == COND_EXPR
)
9532 /* We'll end up copying from each of the arms of the COND_EXPR directly
9533 into the target, so look at them. */
9534 if (tree op
= TREE_OPERAND (init
, 1))
9535 if (init_by_return_slot_p (op
))
9537 return init_by_return_slot_p (TREE_OPERAND (init
, 2));
9539 return (TREE_CODE (init
) == AGGR_INIT_EXPR
9540 && !AGGR_INIT_VIA_CTOR_P (init
));
9543 /* We can't elide a copy from a function returning by value to a
9544 potentially-overlapping subobject, as the callee might clobber tail padding.
9545 Return true iff this could be that case.
9547 Places that use this function (or _opt) to decide to elide a copy should
9548 probably use make_safe_copy_elision instead. */
9551 unsafe_copy_elision_p (tree target
, tree exp
)
9553 return unsafe_return_slot_p (target
) && init_by_return_slot_p (exp
);
9556 /* As above, but for optimization allow more cases that are actually safe. */
9559 unsafe_copy_elision_p_opt (tree target
, tree exp
)
9561 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
9562 /* It's safe to elide the copy for a class with no tail padding. */
9563 if (!is_empty_class (type
)
9564 && tree_int_cst_equal (TYPE_SIZE (type
), CLASSTYPE_SIZE (type
)))
9566 return unsafe_copy_elision_p (target
, exp
);
9569 /* Try to make EXP suitable to be used as the initializer for TARGET,
9570 and return whether we were successful. */
9573 make_safe_copy_elision (tree target
, tree exp
)
9575 int uns
= unsafe_return_slot_p (target
);
9578 if (init_by_return_slot_p (exp
))
9582 return make_base_init_ok (exp
);
9585 /* True IFF the result of the conversion C is a prvalue. */
9588 conv_is_prvalue (conversion
*c
)
9590 if (c
->kind
== ck_rvalue
)
9592 if (c
->kind
== ck_base
&& c
->need_temporary_p
)
9594 if (c
->kind
== ck_user
&& !TYPE_REF_P (c
->type
))
9596 if (c
->kind
== ck_identity
&& c
->u
.expr
9597 && TREE_CODE (c
->u
.expr
) == TARGET_EXPR
)
9603 /* True iff C is a conversion that binds a reference to a prvalue. */
9606 conv_binds_ref_to_prvalue (conversion
*c
)
9608 if (c
->kind
!= ck_ref_bind
)
9610 if (c
->need_temporary_p
)
9613 return conv_is_prvalue (next_conversion (c
));
9616 /* True iff EXPR represents a (subobject of a) temporary. */
9619 expr_represents_temporary_p (tree expr
)
9621 while (handled_component_p (expr
))
9622 expr
= TREE_OPERAND (expr
, 0);
9623 return TREE_CODE (expr
) == TARGET_EXPR
;
9626 /* True iff C is a conversion that binds a reference to a temporary.
9627 This is a superset of conv_binds_ref_to_prvalue: here we're also
9628 interested in xvalues. */
9631 conv_binds_ref_to_temporary (conversion
*c
)
9633 if (conv_binds_ref_to_prvalue (c
))
9635 if (c
->kind
!= ck_ref_bind
)
9637 c
= next_conversion (c
);
9638 /* This is the case for
9640 struct Derived : Base {};
9641 const Base& b(Derived{});
9642 where we bind 'b' to the Base subobject of a temporary object of type
9643 Derived. The subobject is an xvalue; the whole object is a prvalue.
9645 The ck_base doesn't have to be present for cases like X{}.m. */
9646 if (c
->kind
== ck_base
)
9647 c
= next_conversion (c
);
9648 if (c
->kind
== ck_identity
&& c
->u
.expr
9649 && expr_represents_temporary_p (c
->u
.expr
))
9654 /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
9655 the reference to a temporary. Return tristate::TS_FALSE if converting
9656 EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
9657 the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
9658 says whether the conversion should be done in direct- or copy-initialization
9662 ref_conv_binds_to_temporary (tree type
, tree expr
, bool direct_init_p
/*=false*/)
9664 gcc_assert (TYPE_REF_P (type
));
9666 conversion_obstack_sentinel cos
;
9668 const int flags
= direct_init_p
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
;
9669 conversion
*conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
9670 /*c_cast_p=*/false, flags
, tf_none
);
9671 tristate
ret (tristate::TS_UNKNOWN
);
9672 if (conv
&& !conv
->bad_p
)
9673 ret
= tristate (conv_binds_ref_to_temporary (conv
));
9678 /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
9679 class type or a pointer to class type. If NO_PTR_DEREF is true and
9680 INSTANCE has pointer type, clobber the pointer rather than what it points
9684 build_trivial_dtor_call (tree instance
, bool no_ptr_deref
)
9686 gcc_assert (!is_dummy_object (instance
));
9688 if (!flag_lifetime_dse
)
9691 return fold_convert (void_type_node
, instance
);
9694 if (INDIRECT_TYPE_P (TREE_TYPE (instance
))
9695 && (!no_ptr_deref
|| TYPE_REF_P (TREE_TYPE (instance
))))
9697 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
9699 instance
= cp_build_fold_indirect_ref (instance
);
9702 /* A trivial destructor should still clobber the object. */
9703 tree clobber
= build_clobber (TREE_TYPE (instance
));
9704 return build2 (MODIFY_EXPR
, void_type_node
,
9708 /* Return true if in an immediate function context, or an unevaluated operand,
9709 or a default argument/member initializer, or a subexpression of an immediate
9713 in_immediate_context ()
9715 return (cp_unevaluated_operand
!= 0
9716 || (current_function_decl
!= NULL_TREE
9717 && DECL_IMMEDIATE_FUNCTION_P (current_function_decl
))
9718 /* DR 2631: default args and DMI aren't immediately evaluated.
9719 Return true here so immediate_invocation_p returns false. */
9720 || current_binding_level
->kind
== sk_function_parms
9721 || current_binding_level
->kind
== sk_template_parms
9723 || in_consteval_if_p
);
9726 /* Return true if a call to FN with number of arguments NARGS
9727 is an immediate invocation. */
9730 immediate_invocation_p (tree fn
)
9732 return (TREE_CODE (fn
) == FUNCTION_DECL
9733 && DECL_IMMEDIATE_FUNCTION_P (fn
)
9734 && !in_immediate_context ());
9737 /* Subroutine of the various build_*_call functions. Overload resolution
9738 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
9739 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
9740 bitmask of various LOOKUP_* flags which apply to the call itself. */
9743 build_over_call (struct z_candidate
*cand
, int flags
, tsubst_flags_t complain
)
9746 const vec
<tree
, va_gc
> *args
= cand
->args
;
9747 tree first_arg
= cand
->first_arg
;
9748 conversion
**convs
= cand
->convs
;
9750 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9755 unsigned int arg_index
= 0;
9759 bool already_used
= false;
9761 /* In a template, there is no need to perform all of the work that
9762 is normally done. We are only interested in the type of the call
9763 expression, i.e., the return type of the function. Any semantic
9764 errors will be deferred until the template is instantiated. */
9765 if (processing_template_decl
)
9767 if (undeduced_auto_decl (fn
))
9768 mark_used (fn
, complain
);
9770 /* Otherwise set TREE_USED for the benefit of -Wunused-function.
9774 tree return_type
= TREE_TYPE (TREE_TYPE (fn
));
9776 if (first_arg
== NULL_TREE
)
9778 callee
= build_addr_func (fn
, complain
);
9779 if (callee
== error_mark_node
)
9780 return error_mark_node
;
9784 callee
= build_baselink (cand
->conversion_path
, cand
->access_path
,
9786 callee
= build_min (COMPONENT_REF
, TREE_TYPE (fn
),
9787 first_arg
, callee
, NULL_TREE
);
9790 tree expr
= build_call_vec (return_type
, callee
, args
);
9791 SET_EXPR_LOCATION (expr
, input_location
);
9792 if (TREE_THIS_VOLATILE (fn
) && cfun
)
9793 current_function_returns_abnormally
= 1;
9794 if (immediate_invocation_p (fn
))
9796 tree obj_arg
= NULL_TREE
, exprimm
= expr
;
9797 if (DECL_CONSTRUCTOR_P (fn
))
9798 obj_arg
= first_arg
;
9800 && is_dummy_object (obj_arg
)
9801 && !type_dependent_expression_p (obj_arg
))
9803 exprimm
= build_cplus_new (DECL_CONTEXT (fn
), expr
, complain
);
9804 obj_arg
= NULL_TREE
;
9806 /* Look through *(const T *)&obj. */
9807 else if (obj_arg
&& INDIRECT_REF_P (obj_arg
))
9809 tree addr
= TREE_OPERAND (obj_arg
, 0);
9811 if (TREE_CODE (addr
) == ADDR_EXPR
)
9813 tree typeo
= TREE_TYPE (obj_arg
);
9814 tree typei
= TREE_TYPE (TREE_OPERAND (addr
, 0));
9815 if (same_type_ignoring_top_level_qualifiers_p (typeo
, typei
))
9816 obj_arg
= TREE_OPERAND (addr
, 0);
9819 fold_non_dependent_expr (exprimm
, complain
,
9820 /*manifestly_const_eval=*/true,
9823 return convert_from_reference (expr
);
9826 /* Give any warnings we noticed during overload resolution. */
9827 if (cand
->warnings
&& (complain
& tf_warning
))
9829 struct candidate_warning
*w
;
9830 for (w
= cand
->warnings
; w
; w
= w
->next
)
9831 joust (cand
, w
->loser
, 1, complain
);
9834 /* Core issue 2327: P0135 doesn't say how to handle the case where the
9835 argument to the copy constructor ends up being a prvalue after
9836 conversion. Let's do the normal processing, but pretend we aren't
9837 actually using the copy constructor. */
9838 bool force_elide
= false;
9839 if (cxx_dialect
>= cxx17
9840 && cand
->num_convs
== 1
9841 && DECL_COMPLETE_CONSTRUCTOR_P (fn
)
9842 && (DECL_COPY_CONSTRUCTOR_P (fn
)
9843 || DECL_MOVE_CONSTRUCTOR_P (fn
))
9844 && !unsafe_return_slot_p (first_arg
)
9845 && conv_binds_ref_to_prvalue (convs
[0]))
9848 goto not_really_used
;
9851 /* OK, we're actually calling this inherited constructor; set its deletedness
9852 appropriately. We can get away with doing this here because calling is
9853 the only way to refer to a constructor. */
9854 if (DECL_INHERITED_CTOR (fn
)
9855 && !deduce_inheriting_ctor (fn
))
9857 if (complain
& tf_error
)
9859 return error_mark_node
;
9862 /* Make =delete work with SFINAE. */
9863 if (DECL_DELETED_FN (fn
))
9865 if (complain
& tf_error
)
9867 return error_mark_node
;
9870 if (DECL_FUNCTION_MEMBER_P (fn
))
9873 /* If FN is a template function, two cases must be considered.
9878 template <class T> void f();
9880 template <class T> struct B {
9884 struct C : A, B<int> {
9886 using B<int>::g; // #2
9889 In case #1 where `A::f' is a member template, DECL_ACCESS is
9890 recorded in the primary template but not in its specialization.
9891 We check access of FN using its primary template.
9893 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
9894 because it is a member of class template B, DECL_ACCESS is
9895 recorded in the specialization `B<int>::g'. We cannot use its
9896 primary template because `B<T>::g' and `B<int>::g' may have
9897 different access. */
9898 if (DECL_TEMPLATE_INFO (fn
)
9899 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
9900 access_fn
= DECL_TI_TEMPLATE (fn
);
9903 if (!perform_or_defer_access_check (cand
->access_path
, access_fn
,
9905 return error_mark_node
;
9908 /* If we're checking for implicit delete, don't bother with argument
9910 if (flags
& LOOKUP_SPECULATIVE
)
9912 if (cand
->viable
== 1)
9914 else if (!(complain
& tf_error
))
9915 /* Reject bad conversions now. */
9916 return error_mark_node
;
9917 /* else continue to get conversion error. */
9922 /* N3276 magic doesn't apply to nested calls. */
9923 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
9924 complain
&= ~tf_decltype
;
9925 /* No-Cleanup doesn't apply to nested calls either. */
9926 tsubst_flags_t no_cleanup_complain
= complain
;
9927 complain
&= ~tf_no_cleanup
;
9929 /* Find maximum size of vector to hold converted arguments. */
9930 parmlen
= list_length (parm
);
9931 nargs
= vec_safe_length (args
) + (first_arg
!= NULL_TREE
? 1 : 0);
9932 if (parmlen
> nargs
)
9934 argarray
= XALLOCAVEC (tree
, nargs
);
9936 in_consteval_if_p_temp_override icip
;
9937 /* If the call is immediate function invocation, make sure
9938 taking address of immediate functions is allowed in its arguments. */
9939 if (immediate_invocation_p (STRIP_TEMPLATE (fn
)))
9940 in_consteval_if_p
= true;
9942 /* The implicit parameters to a constructor are not considered by overload
9943 resolution, and must be of the proper type. */
9944 if (DECL_CONSTRUCTOR_P (fn
))
9947 if (first_arg
!= NULL_TREE
)
9949 object_arg
= first_arg
;
9950 first_arg
= NULL_TREE
;
9954 object_arg
= (*args
)[arg_index
];
9957 argarray
[j
++] = build_this (object_arg
);
9958 parm
= TREE_CHAIN (parm
);
9959 /* We should never try to call the abstract constructor. */
9960 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn
));
9962 if (DECL_HAS_VTT_PARM_P (fn
))
9964 argarray
[j
++] = (*args
)[arg_index
];
9966 parm
= TREE_CHAIN (parm
);
9969 /* Bypass access control for 'this' parameter. */
9970 else if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
9972 tree arg
= build_this (first_arg
!= NULL_TREE
9974 : (*args
)[arg_index
]);
9975 tree argtype
= TREE_TYPE (arg
);
9977 if (arg
== error_mark_node
)
9978 return error_mark_node
;
9980 if (convs
[i
]->bad_p
)
9982 if (complain
& tf_error
)
9984 auto_diagnostic_group d
;
9985 if (permerror (input_location
, "passing %qT as %<this%> "
9986 "argument discards qualifiers",
9987 TREE_TYPE (argtype
)))
9988 inform (DECL_SOURCE_LOCATION (fn
), " in call to %qD", fn
);
9991 return error_mark_node
;
9994 /* The class where FN is defined. */
9995 tree ctx
= DECL_CONTEXT (fn
);
9997 /* See if the function member or the whole class type is declared
9998 final and the call can be devirtualized. */
9999 if (DECL_FINAL_P (fn
) || CLASSTYPE_FINAL (ctx
))
10000 flags
|= LOOKUP_NONVIRTUAL
;
10002 /* [class.mfct.non-static]: If a non-static member function of a class
10003 X is called for an object that is not of type X, or of a type
10004 derived from X, the behavior is undefined.
10006 So we can assume that anything passed as 'this' is non-null, and
10007 optimize accordingly. */
10008 /* Check that the base class is accessible. */
10009 if (!accessible_base_p (TREE_TYPE (argtype
),
10010 BINFO_TYPE (cand
->conversion_path
), true))
10012 if (complain
& tf_error
)
10013 error ("%qT is not an accessible base of %qT",
10014 BINFO_TYPE (cand
->conversion_path
),
10015 TREE_TYPE (argtype
));
10017 return error_mark_node
;
10019 /* If fn was found by a using declaration, the conversion path
10020 will be to the derived class, not the base declaring fn. We
10021 must convert to the base. */
10022 tree base_binfo
= cand
->conversion_path
;
10023 if (BINFO_TYPE (base_binfo
) != ctx
)
10025 base_binfo
= lookup_base (base_binfo
, ctx
, ba_unique
, NULL
, complain
);
10026 if (base_binfo
== error_mark_node
)
10027 return error_mark_node
;
10030 /* If we know the dynamic type of the object, look up the final overrider
10032 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0
10033 && resolves_to_fixed_type_p (arg
))
10035 tree ov
= lookup_vfn_in_binfo (DECL_VINDEX (fn
), base_binfo
);
10037 /* And unwind base_binfo to match. If we don't find the type we're
10038 looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
10039 inheritance; for now do a normal virtual call in that case. */
10040 tree octx
= DECL_CONTEXT (ov
);
10041 tree obinfo
= base_binfo
;
10042 while (obinfo
&& !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo
), octx
))
10043 obinfo
= BINFO_INHERITANCE_CHAIN (obinfo
);
10047 base_binfo
= obinfo
;
10048 flags
|= LOOKUP_NONVIRTUAL
;
10052 tree converted_arg
= build_base_path (PLUS_EXPR
, arg
,
10053 base_binfo
, 1, complain
);
10055 argarray
[j
++] = converted_arg
;
10056 parm
= TREE_CHAIN (parm
);
10057 if (first_arg
!= NULL_TREE
)
10058 first_arg
= NULL_TREE
;
10065 gcc_assert (first_arg
== NULL_TREE
);
10066 for (; arg_index
< vec_safe_length (args
) && parm
;
10067 parm
= TREE_CHAIN (parm
), ++arg_index
, ++i
)
10069 tree type
= TREE_VALUE (parm
);
10070 tree arg
= (*args
)[arg_index
];
10071 bool conversion_warning
= true;
10075 /* If the argument is NULL and used to (implicitly) instantiate a
10076 template function (and bind one of the template arguments to
10077 the type of 'long int'), we don't want to warn about passing NULL
10078 to non-pointer argument.
10079 For example, if we have this template function:
10081 template<typename T> void func(T x) {}
10083 we want to warn (when -Wconversion is enabled) in this case:
10089 but not in this case:
10095 if (null_node_p (arg
)
10096 && DECL_TEMPLATE_INFO (fn
)
10097 && cand
->template_decl
10098 && !cand
->explicit_targs
)
10099 conversion_warning
= false;
10101 /* Set user_conv_p on the argument conversions, so rvalue/base handling
10102 knows not to allow any more UDCs. This needs to happen after we
10103 process cand->warnings. */
10104 if (flags
& LOOKUP_NO_CONVERSION
)
10105 conv
->user_conv_p
= true;
10107 tsubst_flags_t arg_complain
= complain
;
10108 if (!conversion_warning
)
10109 arg_complain
&= ~tf_warning
;
10111 if (arg_complain
& tf_warning
)
10112 maybe_warn_pessimizing_move (arg
, type
, /*return_p*/false);
10114 val
= convert_like_with_context (conv
, arg
, fn
, i
- is_method
,
10116 val
= convert_for_arg_passing (type
, val
, arg_complain
);
10118 if (val
== error_mark_node
)
10119 return error_mark_node
;
10121 argarray
[j
++] = val
;
10124 /* Default arguments */
10125 for (; parm
&& parm
!= void_list_node
; parm
= TREE_CHAIN (parm
), i
++)
10127 if (TREE_VALUE (parm
) == error_mark_node
)
10128 return error_mark_node
;
10129 val
= convert_default_arg (TREE_VALUE (parm
),
10130 TREE_PURPOSE (parm
),
10133 if (val
== error_mark_node
)
10134 return error_mark_node
;
10135 argarray
[j
++] = val
;
10139 int magic
= magic_varargs_p (fn
);
10140 for (; arg_index
< vec_safe_length (args
); ++arg_index
)
10142 tree a
= (*args
)[arg_index
];
10143 if (magic
== 3 && arg_index
== 2)
10145 /* Do no conversions for certain magic varargs. */
10146 a
= mark_type_use (a
);
10147 if (TREE_CODE (a
) == FUNCTION_DECL
&& reject_gcc_builtin (a
))
10148 return error_mark_node
;
10150 else if (magic
!= 0)
10152 /* Don't truncate excess precision to the semantic type. */
10153 if (magic
== 1 && TREE_CODE (a
) == EXCESS_PRECISION_EXPR
)
10154 a
= TREE_OPERAND (a
, 0);
10155 /* For other magic varargs only do decay_conversion. */
10156 a
= decay_conversion (a
, complain
);
10158 else if (DECL_CONSTRUCTOR_P (fn
)
10159 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn
),
10162 /* Avoid infinite recursion trying to call A(...). */
10163 if (complain
& tf_error
)
10164 /* Try to call the actual copy constructor for a good error. */
10165 call_copy_ctor (a
, complain
);
10166 return error_mark_node
;
10169 a
= convert_arg_to_ellipsis (a
, complain
);
10170 if (a
== error_mark_node
)
10171 return error_mark_node
;
10175 gcc_assert (j
<= nargs
);
10179 /* Avoid performing argument transformation if warnings are disabled.
10180 When tf_warning is set and at least one of the warnings is active
10181 the check_function_arguments function might warn about something. */
10183 bool warned_p
= false;
10184 if ((complain
& tf_warning
)
10187 || warn_suggest_attribute_format
10190 tree
*fargs
= (!nargs
? argarray
10191 : (tree
*) alloca (nargs
* sizeof (tree
)));
10192 for (j
= 0; j
< nargs
; j
++)
10194 /* For -Wformat undo the implicit passing by hidden reference
10195 done by convert_arg_to_ellipsis. */
10196 if (TREE_CODE (argarray
[j
]) == ADDR_EXPR
10197 && TYPE_REF_P (TREE_TYPE (argarray
[j
])))
10198 fargs
[j
] = TREE_OPERAND (argarray
[j
], 0);
10200 fargs
[j
] = argarray
[j
];
10203 warned_p
= check_function_arguments (input_location
, fn
, TREE_TYPE (fn
),
10204 nargs
, fargs
, NULL
);
10207 if (DECL_INHERITED_CTOR (fn
))
10209 /* Check for passing ellipsis arguments to an inherited constructor. We
10210 could handle this by open-coding the inherited constructor rather than
10211 defining it, but let's not bother now. */
10212 if (!cp_unevaluated_operand
10214 && cand
->convs
[cand
->num_convs
-1]->ellipsis_p
)
10216 if (complain
& tf_error
)
10218 sorry ("passing arguments to ellipsis of inherited constructor "
10220 inform (DECL_SOURCE_LOCATION (cand
->fn
), "declared here");
10222 return error_mark_node
;
10225 /* A base constructor inheriting from a virtual base doesn't get the
10226 inherited arguments, just this and __vtt. */
10227 if (ctor_omit_inherited_parms (fn
))
10231 /* Avoid actually calling copy constructors and copy assignment operators,
10234 if (! flag_elide_constructors
&& !force_elide
)
10235 /* Do things the hard way. */;
10236 else if (cand
->num_convs
== 1
10237 && (DECL_COPY_CONSTRUCTOR_P (fn
)
10238 || DECL_MOVE_CONSTRUCTOR_P (fn
))
10239 /* It's unsafe to elide the constructor when handling
10240 a noexcept-expression, it may evaluate to the wrong
10241 value (c++/53025). */
10242 && (force_elide
|| cp_noexcept_operand
== 0))
10245 tree arg
= argarray
[num_artificial_parms_for (fn
)];
10246 tree fa
= argarray
[0];
10247 bool trivial
= trivial_fn_p (fn
);
10249 /* Pull out the real argument, disregarding const-correctness. */
10251 /* Strip the reference binding for the constructor parameter. */
10252 if (CONVERT_EXPR_P (targ
)
10253 && TYPE_REF_P (TREE_TYPE (targ
)))
10254 targ
= TREE_OPERAND (targ
, 0);
10255 /* But don't strip any other reference bindings; binding a temporary to a
10256 reference prevents copy elision. */
10257 while ((CONVERT_EXPR_P (targ
)
10258 && !TYPE_REF_P (TREE_TYPE (targ
)))
10259 || TREE_CODE (targ
) == NON_LVALUE_EXPR
)
10260 targ
= TREE_OPERAND (targ
, 0);
10261 if (TREE_CODE (targ
) == ADDR_EXPR
)
10263 targ
= TREE_OPERAND (targ
, 0);
10264 if (!same_type_ignoring_top_level_qualifiers_p
10265 (TREE_TYPE (TREE_TYPE (arg
)), TREE_TYPE (targ
)))
10274 arg
= cp_build_fold_indirect_ref (arg
);
10276 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10277 potentially-overlapping subobject. */
10278 if (CHECKING_P
&& cxx_dialect
>= cxx17
)
10279 gcc_assert (TREE_CODE (arg
) != TARGET_EXPR
10281 /* It's from binding the ref parm to a packed field. */
10282 || convs
[0]->need_temporary_p
10284 /* See unsafe_copy_elision_p. */
10285 || unsafe_return_slot_p (fa
));
10287 bool unsafe
= unsafe_copy_elision_p_opt (fa
, arg
);
10288 bool eliding_temp
= (TREE_CODE (arg
) == TARGET_EXPR
&& !unsafe
);
10290 /* [class.copy]: the copy constructor is implicitly defined even if the
10291 implementation elided its use. But don't warn about deprecation when
10292 eliding a temporary, as then no copy is actually performed. */
10293 warning_sentinel
s (warn_deprecated_copy
, eliding_temp
);
10295 /* The language says this isn't called. */;
10298 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
10299 return error_mark_node
;
10300 already_used
= true;
10303 cp_handle_deprecated_or_unavailable (fn
, complain
);
10305 if (eliding_temp
&& DECL_BASE_CONSTRUCTOR_P (fn
)
10306 && !make_base_init_ok (arg
))
10309 /* If we're creating a temp and we already have one, don't create a
10310 new one. If we're not creating a temp but we get one, use
10311 INIT_EXPR to collapse the temp into our target. Otherwise, if the
10312 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10313 temp or an INIT_EXPR otherwise. */
10314 if (is_dummy_object (fa
))
10316 if (TREE_CODE (arg
) == TARGET_EXPR
)
10319 return force_target_expr (DECL_CONTEXT (fn
), arg
, complain
);
10321 else if ((trivial
|| TREE_CODE (arg
) == TARGET_EXPR
)
10324 tree to
= cp_build_fold_indirect_ref (fa
);
10325 val
= cp_build_init_expr (to
, arg
);
10329 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
10330 && DECL_OVERLOADED_OPERATOR_IS (fn
, NOP_EXPR
)
10331 && trivial_fn_p (fn
))
10333 /* Don't use cp_build_fold_indirect_ref, op= returns an lvalue even if
10334 the object argument isn't one. */
10335 tree to
= cp_build_indirect_ref (input_location
, argarray
[0],
10336 RO_ARROW
, complain
);
10337 tree type
= TREE_TYPE (to
);
10338 tree as_base
= CLASSTYPE_AS_BASE (type
);
10339 tree arg
= argarray
[1];
10340 location_t loc
= cp_expr_loc_or_input_loc (arg
);
10342 if (is_really_empty_class (type
, /*ignore_vptr*/true))
10344 /* Avoid copying empty classes. */
10345 val
= build2 (COMPOUND_EXPR
, type
, arg
, to
);
10346 suppress_warning (val
, OPT_Wunused
);
10348 else if (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (as_base
)))
10350 if (is_std_init_list (type
)
10351 && conv_binds_ref_to_prvalue (convs
[1]))
10352 warning_at (loc
, OPT_Winit_list_lifetime
,
10353 "assignment from temporary %<initializer_list%> does "
10354 "not extend the lifetime of the underlying array");
10355 arg
= cp_build_fold_indirect_ref (arg
);
10356 val
= build2 (MODIFY_EXPR
, TREE_TYPE (to
), to
, arg
);
10360 /* We must only copy the non-tail padding parts. */
10361 tree arg0
, arg2
, t
;
10362 tree array_type
, alias_set
;
10364 arg2
= TYPE_SIZE_UNIT (as_base
);
10365 to
= cp_stabilize_reference (to
);
10366 arg0
= cp_build_addr_expr (to
, complain
);
10368 array_type
= build_array_type (unsigned_char_type_node
,
10370 (size_binop (MINUS_EXPR
,
10371 arg2
, size_int (1))));
10372 alias_set
= build_int_cst (build_pointer_type (type
), 0);
10373 t
= build2 (MODIFY_EXPR
, void_type_node
,
10374 build2 (MEM_REF
, array_type
, arg0
, alias_set
),
10375 build2 (MEM_REF
, array_type
, arg
, alias_set
));
10376 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (to
), t
, to
);
10377 suppress_warning (val
, OPT_Wunused
);
10380 cp_handle_deprecated_or_unavailable (fn
, complain
);
10384 else if (trivial_fn_p (fn
))
10386 if (DECL_DESTRUCTOR_P (fn
))
10387 return build_trivial_dtor_call (argarray
[0]);
10388 else if (default_ctor_p (fn
))
10390 if (is_dummy_object (argarray
[0]))
10391 return force_target_expr (DECL_CONTEXT (fn
), void_node
,
10392 no_cleanup_complain
);
10394 return cp_build_fold_indirect_ref (argarray
[0]);
10398 gcc_assert (!force_elide
);
10401 && !mark_used (fn
, complain
))
10402 return error_mark_node
;
10404 /* Warn if the built-in writes to an object of a non-trivial type. */
10405 if (warn_class_memaccess
10406 && vec_safe_length (args
) >= 2
10407 && DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
10408 maybe_warn_class_memaccess (input_location
, fn
, args
);
10410 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0)
10413 tree binfo
= lookup_base (TREE_TYPE (TREE_TYPE (argarray
[0])),
10415 ba_any
, NULL
, complain
);
10416 gcc_assert (binfo
&& binfo
!= error_mark_node
);
10418 argarray
[0] = build_base_path (PLUS_EXPR
, argarray
[0], binfo
, 1,
10420 if (TREE_SIDE_EFFECTS (argarray
[0]))
10421 argarray
[0] = save_expr (argarray
[0]);
10422 t
= build_pointer_type (TREE_TYPE (fn
));
10423 fn
= build_vfn_ref (argarray
[0], DECL_VINDEX (fn
));
10424 TREE_TYPE (fn
) = t
;
10428 /* If FN is marked deprecated or unavailable, then we've already
10429 issued a diagnostic from mark_used above, so avoid redundantly
10430 issuing another one from build_addr_func. */
10431 auto w
= make_temp_override (deprecated_state
,
10432 UNAVAILABLE_DEPRECATED_SUPPRESS
);
10434 fn
= build_addr_func (fn
, complain
);
10435 if (fn
== error_mark_node
)
10436 return error_mark_node
;
10438 /* We're actually invoking the function. (Immediate functions get an
10439 & when invoking it even though the user didn't use &.) */
10440 ADDR_EXPR_DENOTES_CALL_P (fn
) = true;
10443 tree call
= build_cxx_call (fn
, nargs
, argarray
, complain
|decltype_flag
);
10444 if (call
== error_mark_node
)
10446 if (cand
->flags
& LOOKUP_LIST_INIT_CTOR
)
10448 tree c
= extract_call_expr (call
);
10449 /* build_new_op will clear this when appropriate. */
10450 CALL_EXPR_ORDERED_ARGS (c
) = true;
10454 tree c
= extract_call_expr (call
);
10455 if (TREE_CODE (c
) == CALL_EXPR
)
10456 suppress_warning (c
/* Suppress all warnings. */);
10465 /* Return the DECL of the first non-static subobject of class TYPE
10466 that satisfies the predicate PRED or null if none can be found. */
10468 template <class Predicate
>
10470 first_non_static_field (tree type
, Predicate pred
)
10472 if (!type
|| !CLASS_TYPE_P (type
))
10475 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
10477 if (TREE_CODE (field
) != FIELD_DECL
)
10479 if (TREE_STATIC (field
))
10487 for (tree base_binfo
, binfo
= TYPE_BINFO (type
);
10488 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
10490 tree base
= TREE_TYPE (base_binfo
);
10493 if (tree field
= first_non_static_field (base
, pred
))
10500 struct NonPublicField
10502 bool operator() (const_tree t
) const
10504 return DECL_P (t
) && (TREE_PRIVATE (t
) || TREE_PROTECTED (t
));
10508 /* Return the DECL of the first non-public subobject of class TYPE
10509 or null if none can be found. */
10512 first_non_public_field (tree type
)
10514 return first_non_static_field (type
, NonPublicField ());
10517 struct NonTrivialField
10519 bool operator() (const_tree t
) const
10521 return !trivial_type_p (DECL_P (t
) ? TREE_TYPE (t
) : t
);
10525 /* Return the DECL of the first non-trivial subobject of class TYPE
10526 or null if none can be found. */
10529 first_non_trivial_field (tree type
)
10531 return first_non_static_field (type
, NonTrivialField ());
10534 } /* unnamed namespace */
10536 /* Return true if all copy and move assignment operator overloads for
10537 class TYPE are trivial and at least one of them is not deleted and,
10538 when ACCESS is set, accessible. Return false otherwise. Set
10539 HASASSIGN to true when the TYPE has a (not necessarily trivial)
10540 copy or move assignment. */
10543 has_trivial_copy_assign_p (tree type
, bool access
, bool *hasassign
)
10545 tree fns
= get_class_binding (type
, assign_op_identifier
);
10546 bool all_trivial
= true;
10548 /* Iterate over overloads of the assignment operator, checking
10549 accessible copy assignments for triviality. */
10551 for (tree f
: ovl_range (fns
))
10553 /* Skip operators that aren't copy assignments. */
10554 if (!copy_fn_p (f
))
10557 bool accessible
= (!access
|| !(TREE_PRIVATE (f
) || TREE_PROTECTED (f
))
10558 || accessible_p (TYPE_BINFO (type
), f
, true));
10560 /* Skip template assignment operators and deleted functions. */
10561 if (TREE_CODE (f
) != FUNCTION_DECL
|| DECL_DELETED_FN (f
))
10567 if (!accessible
|| !trivial_fn_p (f
))
10568 all_trivial
= false;
10570 /* Break early when both properties have been determined. */
10571 if (*hasassign
&& !all_trivial
)
10575 /* Return true if they're all trivial and one of the expressions
10576 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
10577 tree ref
= cp_build_reference_type (type
, false);
10578 return (all_trivial
10579 && (is_trivially_xible (MODIFY_EXPR
, type
, type
)
10580 || is_trivially_xible (MODIFY_EXPR
, type
, ref
)));
10583 /* Return true if all copy and move ctor overloads for class TYPE are
10584 trivial and at least one of them is not deleted and, when ACCESS is
10585 set, accessible. Return false otherwise. Set each element of HASCTOR[]
10586 to true when the TYPE has a (not necessarily trivial) default and copy
10587 (or move) ctor, respectively. */
10590 has_trivial_copy_p (tree type
, bool access
, bool hasctor
[2])
10592 tree fns
= get_class_binding (type
, complete_ctor_identifier
);
10593 bool all_trivial
= true;
10595 for (tree f
: ovl_range (fns
))
10597 /* Skip template constructors. */
10598 if (TREE_CODE (f
) != FUNCTION_DECL
)
10601 bool cpy_or_move_ctor_p
= copy_fn_p (f
);
10603 /* Skip ctors other than default, copy, and move. */
10604 if (!cpy_or_move_ctor_p
&& !default_ctor_p (f
))
10607 if (DECL_DELETED_FN (f
))
10610 bool accessible
= (!access
|| !(TREE_PRIVATE (f
) || TREE_PROTECTED (f
))
10611 || accessible_p (TYPE_BINFO (type
), f
, true));
10614 hasctor
[cpy_or_move_ctor_p
] = true;
10616 if (cpy_or_move_ctor_p
&& (!accessible
|| !trivial_fn_p (f
)))
10617 all_trivial
= false;
10619 /* Break early when both properties have been determined. */
10620 if (hasctor
[0] && hasctor
[1] && !all_trivial
)
10624 return all_trivial
;
10627 /* Issue a warning on a call to the built-in function FNDECL if it is
10628 a raw memory write whose destination is not an object of (something
10629 like) trivial or standard layout type with a non-deleted assignment
10630 and copy ctor. Detects const correctness violations, corrupting
10631 references, virtual table pointers, and bypassing non-trivial
10635 maybe_warn_class_memaccess (location_t loc
, tree fndecl
,
10636 const vec
<tree
, va_gc
> *args
)
10638 /* Except for bcopy where it's second, the destination pointer is
10639 the first argument for all functions handled here. Compute
10640 the index of the destination and source arguments. */
10641 unsigned dstidx
= DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_BCOPY
;
10642 unsigned srcidx
= !dstidx
;
10644 tree dest
= (*args
)[dstidx
];
10645 if (!TREE_TYPE (dest
)
10646 || (TREE_CODE (TREE_TYPE (dest
)) != ARRAY_TYPE
10647 && !INDIRECT_TYPE_P (TREE_TYPE (dest
))))
10650 tree srctype
= NULL_TREE
;
10652 /* Determine the type of the pointed-to object and whether it's
10653 a complete class type. */
10654 tree desttype
= TREE_TYPE (TREE_TYPE (dest
));
10656 if (!desttype
|| !COMPLETE_TYPE_P (desttype
) || !CLASS_TYPE_P (desttype
))
10659 /* Check to see if the raw memory call is made by a non-static member
10660 function with THIS as the destination argument for the destination
10661 type. If so, and if the class has no non-trivial bases or members,
10662 be more permissive. */
10663 if (current_function_decl
10664 && DECL_NONSTATIC_MEMBER_FUNCTION_P (current_function_decl
)
10665 && is_this_parameter (tree_strip_nop_conversions (dest
)))
10667 tree ctx
= DECL_CONTEXT (current_function_decl
);
10668 bool special
= same_type_ignoring_top_level_qualifiers_p (ctx
, desttype
);
10669 tree binfo
= TYPE_BINFO (ctx
);
10672 && !BINFO_VTABLE (binfo
)
10673 && !first_non_trivial_field (desttype
))
10677 /* True if the class is trivial. */
10678 bool trivial
= trivial_type_p (desttype
);
10680 /* Set to true if DESTYPE has an accessible copy assignment. */
10681 bool hasassign
= false;
10682 /* True if all of the class' overloaded copy assignment operators
10683 are all trivial (and not deleted) and at least one of them is
10685 bool trivassign
= has_trivial_copy_assign_p (desttype
, true, &hasassign
);
10687 /* Set to true if DESTTYPE has an accessible default and copy ctor,
10689 bool hasctors
[2] = { false, false };
10691 /* True if all of the class' overloaded copy constructors are all
10692 trivial (and not deleted) and at least one of them is accessible. */
10693 bool trivcopy
= has_trivial_copy_p (desttype
, true, hasctors
);
10695 /* Set FLD to the first private/protected member of the class. */
10696 tree fld
= trivial
? first_non_public_field (desttype
) : NULL_TREE
;
10698 /* The warning format string. */
10699 const char *warnfmt
= NULL
;
10700 /* A suggested alternative to offer instead of the raw memory call.
10701 Empty string when none can be come up with. */
10702 const char *suggest
= "";
10703 bool warned
= false;
10705 switch (DECL_FUNCTION_CODE (fndecl
))
10707 case BUILT_IN_MEMSET
:
10708 if (!integer_zerop (maybe_constant_value ((*args
)[1])))
10710 /* Diagnose setting non-copy-assignable or non-trivial types,
10711 or types with a private member, to (potentially) non-zero
10712 bytes. Since the value of the bytes being written is unknown,
10713 suggest using assignment instead (if one exists). Also warn
10714 for writes into objects for which zero-initialization doesn't
10715 mean all bits clear (pointer-to-member data, where null is all
10716 bits set). Since the value being written is (most likely)
10717 non-zero, simply suggest assignment (but not copy assignment). */
10718 suggest
= "; use assignment instead";
10720 warnfmt
= G_("%qD writing to an object of type %#qT with "
10721 "no trivial copy-assignment");
10723 warnfmt
= G_("%qD writing to an object of non-trivial type %#qT%s");
10726 const char *access
= TREE_PRIVATE (fld
) ? "private" : "protected";
10727 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
10728 "%qD writing to an object of type %#qT with "
10730 fndecl
, desttype
, access
, fld
);
10732 else if (!zero_init_p (desttype
))
10733 warnfmt
= G_("%qD writing to an object of type %#qT containing "
10734 "a pointer to data member%s");
10738 /* Fall through. */
10740 case BUILT_IN_BZERO
:
10741 /* Similarly to the above, diagnose clearing non-trivial or non-
10742 standard layout objects, or objects of types with no assignmenmt.
10743 Since the value being written is known to be zero, suggest either
10744 copy assignment, copy ctor, or default ctor as an alternative,
10745 depending on what's available. */
10747 if (hasassign
&& hasctors
[0])
10748 suggest
= G_("; use assignment or value-initialization instead");
10749 else if (hasassign
)
10750 suggest
= G_("; use assignment instead");
10751 else if (hasctors
[0])
10752 suggest
= G_("; use value-initialization instead");
10755 warnfmt
= G_("%qD clearing an object of type %#qT with "
10756 "no trivial copy-assignment%s");
10758 warnfmt
= G_("%qD clearing an object of non-trivial type %#qT%s");
10759 else if (!zero_init_p (desttype
))
10760 warnfmt
= G_("%qD clearing an object of type %#qT containing "
10761 "a pointer-to-member%s");
10764 case BUILT_IN_BCOPY
:
10765 case BUILT_IN_MEMCPY
:
10766 case BUILT_IN_MEMMOVE
:
10767 case BUILT_IN_MEMPCPY
:
10768 /* Determine the type of the source object. */
10769 srctype
= TREE_TYPE ((*args
)[srcidx
]);
10770 if (!srctype
|| !INDIRECT_TYPE_P (srctype
))
10771 srctype
= void_type_node
;
10773 srctype
= TREE_TYPE (srctype
);
10775 /* Since it's impossible to determine wheter the byte copy is
10776 being used in place of assignment to an existing object or
10777 as a substitute for initialization, assume it's the former.
10778 Determine the best alternative to use instead depending on
10779 what's not deleted. */
10780 if (hasassign
&& hasctors
[1])
10781 suggest
= G_("; use copy-assignment or copy-initialization instead");
10782 else if (hasassign
)
10783 suggest
= G_("; use copy-assignment instead");
10784 else if (hasctors
[1])
10785 suggest
= G_("; use copy-initialization instead");
10788 warnfmt
= G_("%qD writing to an object of type %#qT with no trivial "
10789 "copy-assignment%s");
10790 else if (!trivially_copyable_p (desttype
))
10791 warnfmt
= G_("%qD writing to an object of non-trivially copyable "
10793 else if (!trivcopy
)
10794 warnfmt
= G_("%qD writing to an object with a deleted copy constructor");
10797 && !VOID_TYPE_P (srctype
)
10798 && !is_byte_access_type (srctype
)
10799 && !same_type_ignoring_top_level_qualifiers_p (desttype
,
10802 /* Warn when copying into a non-trivial object from an object
10803 of a different type other than void or char. */
10804 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
10805 "%qD copying an object of non-trivial type "
10806 "%#qT from an array of %#qT",
10807 fndecl
, desttype
, srctype
);
10810 && !VOID_TYPE_P (srctype
)
10811 && !is_byte_access_type (srctype
)
10812 && !same_type_ignoring_top_level_qualifiers_p (desttype
,
10815 const char *access
= TREE_PRIVATE (fld
) ? "private" : "protected";
10816 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
10817 "%qD copying an object of type %#qT with "
10818 "%qs member %qD from an array of %#qT; use "
10819 "assignment or copy-initialization instead",
10820 fndecl
, desttype
, access
, fld
, srctype
);
10822 else if (!trivial
&& vec_safe_length (args
) > 2)
10824 tree sz
= maybe_constant_value ((*args
)[2]);
10825 if (!tree_fits_uhwi_p (sz
))
10828 /* Finally, warn on partial copies. */
10829 unsigned HOST_WIDE_INT typesize
10830 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype
));
10833 if (unsigned HOST_WIDE_INT partial
= tree_to_uhwi (sz
) % typesize
)
10834 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
10835 (typesize
- partial
> 1
10836 ? G_("%qD writing to an object of "
10837 "a non-trivial type %#qT leaves %wu "
10839 : G_("%qD writing to an object of "
10840 "a non-trivial type %#qT leaves %wu "
10841 "byte unchanged")),
10842 fndecl
, desttype
, typesize
- partial
);
10846 case BUILT_IN_REALLOC
:
10848 if (!trivially_copyable_p (desttype
))
10849 warnfmt
= G_("%qD moving an object of non-trivially copyable type "
10850 "%#qT; use %<new%> and %<delete%> instead");
10851 else if (!trivcopy
)
10852 warnfmt
= G_("%qD moving an object of type %#qT with deleted copy "
10853 "constructor; use %<new%> and %<delete%> instead");
10854 else if (!get_dtor (desttype
, tf_none
))
10855 warnfmt
= G_("%qD moving an object of type %#qT with deleted "
10859 tree sz
= maybe_constant_value ((*args
)[1]);
10860 if (TREE_CODE (sz
) == INTEGER_CST
10861 && tree_int_cst_lt (sz
, TYPE_SIZE_UNIT (desttype
)))
10862 /* Finally, warn on reallocation into insufficient space. */
10863 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
10864 "%qD moving an object of non-trivial type "
10865 "%#qT and size %E into a region of size %E",
10866 fndecl
, desttype
, TYPE_SIZE_UNIT (desttype
),
10878 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
10879 warnfmt
, fndecl
, desttype
, suggest
);
10881 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
10882 warnfmt
, fndecl
, desttype
);
10886 inform (location_of (desttype
), "%#qT declared here", desttype
);
10889 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
10890 If FN is the result of resolving an overloaded target built-in,
10891 ORIG_FNDECL is the original function decl, otherwise it is null.
10892 This function performs no overload resolution, conversion, or other
10893 high-level operations. */
10896 build_cxx_call (tree fn
, int nargs
, tree
*argarray
,
10897 tsubst_flags_t complain
, tree orig_fndecl
)
10901 /* Remember roughly where this call is. */
10902 location_t loc
= cp_expr_loc_or_input_loc (fn
);
10903 fn
= build_call_a (fn
, nargs
, argarray
);
10904 SET_EXPR_LOCATION (fn
, loc
);
10906 fndecl
= get_callee_fndecl (fn
);
10908 orig_fndecl
= fndecl
;
10910 /* Check that arguments to builtin functions match the expectations. */
10912 && !processing_template_decl
10913 && fndecl_built_in_p (fndecl
))
10917 /* We need to take care that values to BUILT_IN_NORMAL
10919 for (i
= 0; i
< nargs
; i
++)
10920 argarray
[i
] = maybe_constant_value (argarray
[i
]);
10922 if (!check_builtin_function_arguments (EXPR_LOCATION (fn
), vNULL
, fndecl
,
10923 orig_fndecl
, nargs
, argarray
))
10924 return error_mark_node
;
10925 else if (fndecl_built_in_p (fndecl
, BUILT_IN_CLEAR_PADDING
))
10927 tree arg0
= argarray
[0];
10929 if (TREE_CODE (arg0
) == ADDR_EXPR
10930 && DECL_P (TREE_OPERAND (arg0
, 0))
10931 && same_type_ignoring_top_level_qualifiers_p
10932 (TREE_TYPE (TREE_TYPE (argarray
[0])),
10933 TREE_TYPE (TREE_TYPE (arg0
))))
10934 /* For __builtin_clear_padding (&var) we know the type
10935 is for a complete object, so there is no risk in clearing
10936 padding that is reused in some derived class member. */;
10937 else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray
[0]))))
10939 error_at (EXPR_LOC_OR_LOC (argarray
[0], input_location
),
10940 "argument %u in call to function %qE "
10941 "has pointer to a non-trivially-copyable type (%qT)",
10942 1, fndecl
, TREE_TYPE (argarray
[0]));
10943 return error_mark_node
;
10948 if (VOID_TYPE_P (TREE_TYPE (fn
)))
10951 /* 5.2.2/11: If a function call is a prvalue of object type: if the
10952 function call is either the operand of a decltype-specifier or the
10953 right operand of a comma operator that is the operand of a
10954 decltype-specifier, a temporary object is not introduced for the
10955 prvalue. The type of the prvalue may be incomplete. */
10956 if (!(complain
& tf_decltype
))
10958 fn
= require_complete_type (fn
, complain
);
10959 if (fn
== error_mark_node
)
10960 return error_mark_node
;
10962 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn
)))
10964 fn
= build_cplus_new (TREE_TYPE (fn
), fn
, complain
);
10965 maybe_warn_parm_abi (TREE_TYPE (fn
), loc
);
10968 return convert_from_reference (fn
);
10971 /* Returns the value to use for the in-charge parameter when making a
10972 call to a function with the indicated NAME.
10974 FIXME:Can't we find a neater way to do this mapping? */
10977 in_charge_arg_for_name (tree name
)
10979 if (IDENTIFIER_CTOR_P (name
))
10981 if (name
== complete_ctor_identifier
)
10982 return integer_one_node
;
10983 gcc_checking_assert (name
== base_ctor_identifier
);
10987 if (name
== complete_dtor_identifier
)
10988 return integer_two_node
;
10989 else if (name
== deleting_dtor_identifier
)
10990 return integer_three_node
;
10991 gcc_checking_assert (name
== base_dtor_identifier
);
10994 return integer_zero_node
;
10997 /* We've built up a constructor call RET. Complain if it delegates to the
10998 constructor we're currently compiling. */
11001 check_self_delegation (tree ret
)
11003 if (TREE_CODE (ret
) == TARGET_EXPR
)
11004 ret
= TARGET_EXPR_INITIAL (ret
);
11005 tree fn
= cp_get_callee_fndecl_nofold (ret
);
11006 if (fn
&& DECL_ABSTRACT_ORIGIN (fn
) == current_function_decl
)
11007 error ("constructor delegates to itself");
11010 /* Build a call to a constructor, destructor, or an assignment
11011 operator for INSTANCE, an expression with class type. NAME
11012 indicates the special member function to call; *ARGS are the
11013 arguments. ARGS may be NULL. This may change ARGS. BINFO
11014 indicates the base of INSTANCE that is to be passed as the `this'
11015 parameter to the member function called.
11017 FLAGS are the LOOKUP_* flags to use when processing the call.
11019 If NAME indicates a complete object constructor, INSTANCE may be
11020 NULL_TREE. In this case, the caller will call build_cplus_new to
11021 store the newly constructed object into a VAR_DECL. */
11024 build_special_member_call (tree instance
, tree name
, vec
<tree
, va_gc
> **args
,
11025 tree binfo
, int flags
, tsubst_flags_t complain
)
11028 /* The type of the subobject to be constructed or destroyed. */
11030 vec
<tree
, va_gc
> *allocated
= NULL
;
11033 gcc_assert (IDENTIFIER_CDTOR_P (name
) || name
== assign_op_identifier
);
11035 if (error_operand_p (instance
))
11036 return error_mark_node
;
11038 if (IDENTIFIER_DTOR_P (name
))
11040 gcc_assert (args
== NULL
|| vec_safe_is_empty (*args
));
11041 if (!type_build_dtor_call (TREE_TYPE (instance
)))
11042 /* Shortcut to avoid lazy destructor declaration. */
11043 return build_trivial_dtor_call (instance
);
11046 if (TYPE_P (binfo
))
11048 /* Resolve the name. */
11049 if (!complete_type_or_maybe_complain (binfo
, NULL_TREE
, complain
))
11050 return error_mark_node
;
11052 binfo
= TYPE_BINFO (binfo
);
11055 gcc_assert (binfo
!= NULL_TREE
);
11057 class_type
= BINFO_TYPE (binfo
);
11059 /* Handle the special case where INSTANCE is NULL_TREE. */
11060 if (name
== complete_ctor_identifier
&& !instance
)
11061 instance
= build_dummy_object (class_type
);
11064 /* Convert to the base class, if necessary. */
11065 if (!same_type_ignoring_top_level_qualifiers_p
11066 (TREE_TYPE (instance
), BINFO_TYPE (binfo
)))
11068 if (IDENTIFIER_CDTOR_P (name
))
11069 /* For constructors and destructors, either the base is
11070 non-virtual, or it is virtual but we are doing the
11071 conversion from a constructor or destructor for the
11072 complete object. In either case, we can convert
11074 instance
= convert_to_base_statically (instance
, binfo
);
11077 /* However, for assignment operators, we must convert
11078 dynamically if the base is virtual. */
11079 gcc_checking_assert (name
== assign_op_identifier
);
11080 instance
= build_base_path (PLUS_EXPR
, instance
,
11081 binfo
, /*nonnull=*/1, complain
);
11086 gcc_assert (instance
!= NULL_TREE
);
11088 /* In C++17, "If the initializer expression is a prvalue and the
11089 cv-unqualified version of the source type is the same class as the class
11090 of the destination, the initializer expression is used to initialize the
11091 destination object." Handle that here to avoid doing overload
11093 if (cxx_dialect
>= cxx17
11094 && args
&& vec_safe_length (*args
) == 1
11095 && !unsafe_return_slot_p (instance
))
11097 tree arg
= (**args
)[0];
11099 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
11100 && !TYPE_HAS_LIST_CTOR (class_type
)
11101 && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg
)
11102 && CONSTRUCTOR_NELTS (arg
) == 1)
11103 arg
= CONSTRUCTOR_ELT (arg
, 0)->value
;
11105 if ((TREE_CODE (arg
) == TARGET_EXPR
11106 || TREE_CODE (arg
) == CONSTRUCTOR
)
11107 && (same_type_ignoring_top_level_qualifiers_p
11108 (class_type
, TREE_TYPE (arg
))))
11110 if (is_dummy_object (instance
))
11112 else if (TREE_CODE (arg
) == TARGET_EXPR
)
11113 TARGET_EXPR_DIRECT_INIT_P (arg
) = true;
11115 if ((complain
& tf_error
)
11116 && (flags
& LOOKUP_DELEGATING_CONS
))
11117 check_self_delegation (arg
);
11118 /* Avoid change of behavior on Wunused-var-2.C. */
11119 instance
= mark_lvalue_use (instance
);
11120 return cp_build_init_expr (instance
, arg
);
11124 fns
= lookup_fnfields (binfo
, name
, 1, complain
);
11126 /* When making a call to a constructor or destructor for a subobject
11127 that uses virtual base classes, pass down a pointer to a VTT for
11129 if ((name
== base_ctor_identifier
11130 || name
== base_dtor_identifier
)
11131 && CLASSTYPE_VBASECLASSES (class_type
))
11136 /* If the current function is a complete object constructor
11137 or destructor, then we fetch the VTT directly.
11138 Otherwise, we look it up using the VTT we were given. */
11139 vtt
= DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type
));
11140 vtt
= decay_conversion (vtt
, complain
);
11141 if (vtt
== error_mark_node
)
11142 return error_mark_node
;
11143 vtt
= build_if_in_charge (vtt
, current_vtt_parm
);
11144 if (BINFO_SUBVTT_INDEX (binfo
))
11145 sub_vtt
= fold_build_pointer_plus (vtt
, BINFO_SUBVTT_INDEX (binfo
));
11151 allocated
= make_tree_vector ();
11155 vec_safe_insert (*args
, 0, sub_vtt
);
11158 ret
= build_new_method_call (instance
, fns
, args
,
11159 TYPE_BINFO (BINFO_TYPE (binfo
)),
11160 flags
, /*fn=*/NULL
,
11163 if (allocated
!= NULL
)
11164 release_tree_vector (allocated
);
11166 if ((complain
& tf_error
)
11167 && (flags
& LOOKUP_DELEGATING_CONS
)
11168 && name
== complete_ctor_identifier
)
11169 check_self_delegation (ret
);
11174 /* Return the NAME, as a C string. The NAME indicates a function that
11175 is a member of TYPE. *FREE_P is set to true if the caller must
11176 free the memory returned.
11178 Rather than go through all of this, we should simply set the names
11179 of constructors and destructors appropriately, and dispense with
11180 ctor_identifier, dtor_identifier, etc. */
11183 name_as_c_string (tree name
, tree type
, bool *free_p
)
11185 const char *pretty_name
;
11187 /* Assume that we will not allocate memory. */
11189 /* Constructors and destructors are special. */
11190 if (IDENTIFIER_CDTOR_P (name
))
11193 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type
)));
11194 /* For a destructor, add the '~'. */
11195 if (IDENTIFIER_DTOR_P (name
))
11197 pretty_name
= concat ("~", pretty_name
, NULL
);
11198 /* Remember that we need to free the memory allocated. */
11202 else if (IDENTIFIER_CONV_OP_P (name
))
11204 pretty_name
= concat ("operator ",
11205 type_as_string_translate (TREE_TYPE (name
),
11206 TFF_PLAIN_IDENTIFIER
),
11208 /* Remember that we need to free the memory allocated. */
11212 pretty_name
= identifier_to_locale (IDENTIFIER_POINTER (name
));
11214 return CONST_CAST (char *, pretty_name
);
11217 /* If CANDIDATES contains exactly one candidate, return it, otherwise
11220 static z_candidate
*
11221 single_z_candidate (z_candidate
*candidates
)
11223 if (candidates
== NULL
)
11226 if (candidates
->next
)
11232 /* If CANDIDATE is invalid due to a bad argument type, return the
11233 pertinent conversion_info.
11235 Otherwise, return NULL. */
11237 static const conversion_info
*
11238 maybe_get_bad_conversion_for_unmatched_call (const z_candidate
*candidate
)
11240 /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11241 rejection_reason
*r
= candidate
->reason
;
11251 case rr_arg_conversion
:
11252 return &r
->u
.conversion
;
11254 case rr_bad_arg_conversion
:
11255 return &r
->u
.bad_conversion
;
11259 /* Issue an error and note complaining about a bad argument type at a
11260 callsite with a single candidate FNDECL.
11262 ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11263 case input_location is used).
11264 FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11265 the formal parameter. */
11268 complain_about_bad_argument (location_t arg_loc
,
11269 tree from_type
, tree to_type
,
11270 tree fndecl
, int parmnum
)
11272 auto_diagnostic_group d
;
11273 range_label_for_type_mismatch
rhs_label (from_type
, to_type
);
11274 range_label
*label
= &rhs_label
;
11275 if (arg_loc
== UNKNOWN_LOCATION
)
11277 arg_loc
= input_location
;
11280 gcc_rich_location
richloc (arg_loc
, label
);
11281 error_at (&richloc
,
11282 "cannot convert %qH to %qI",
11283 from_type
, to_type
);
11284 maybe_inform_about_fndecl_for_bogus_argument_init (fndecl
,
11288 /* Subroutine of build_new_method_call_1, for where there are no viable
11289 candidates for the call. */
11292 complain_about_no_candidates_for_method_call (tree instance
,
11293 z_candidate
*candidates
,
11294 tree explicit_targs
,
11296 tree optype
, tree name
,
11297 bool skip_first_for_error
,
11298 vec
<tree
, va_gc
> *user_args
)
11300 auto_diagnostic_group d
;
11301 if (!COMPLETE_OR_OPEN_TYPE_P (basetype
))
11302 cxx_incomplete_type_error (instance
, basetype
);
11304 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11305 basetype
, optype
, build_tree_list_vec (user_args
),
11306 TREE_TYPE (instance
));
11309 /* Special-case for when there's a single candidate that's failing
11310 due to a bad argument type. */
11311 if (z_candidate
*candidate
= single_z_candidate (candidates
))
11312 if (const conversion_info
*conv
11313 = maybe_get_bad_conversion_for_unmatched_call (candidate
))
11315 tree from_type
= conv
->from
;
11316 if (!TYPE_P (conv
->from
))
11317 from_type
= lvalue_type (conv
->from
);
11318 complain_about_bad_argument (conv
->loc
,
11319 from_type
, conv
->to_type
,
11320 candidate
->fn
, conv
->n_arg
);
11324 tree arglist
= build_tree_list_vec (user_args
);
11325 tree errname
= name
;
11326 bool twiddle
= false;
11327 if (IDENTIFIER_CDTOR_P (errname
))
11329 twiddle
= IDENTIFIER_DTOR_P (errname
);
11330 errname
= constructor_name (basetype
);
11332 if (explicit_targs
)
11333 errname
= lookup_template_function (errname
, explicit_targs
);
11334 if (skip_first_for_error
)
11335 arglist
= TREE_CHAIN (arglist
);
11336 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
11337 basetype
, &"~"[!twiddle
], errname
, arglist
,
11338 TREE_TYPE (instance
));
11340 print_z_candidates (location_of (name
), candidates
);
11343 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
11344 be set, upon return, to the function called. ARGS may be NULL.
11345 This may change ARGS. */
11348 build_new_method_call (tree instance
, tree fns
, vec
<tree
, va_gc
> **args
,
11349 tree conversion_path
, int flags
,
11350 tree
*fn_p
, tsubst_flags_t complain
)
11352 struct z_candidate
*candidates
= 0, *cand
;
11353 tree explicit_targs
= NULL_TREE
;
11354 tree basetype
= NULL_TREE
;
11357 tree first_mem_arg
= NULL_TREE
;
11359 bool skip_first_for_error
;
11360 vec
<tree
, va_gc
> *user_args
;
11363 int template_only
= 0;
11365 tree orig_instance
;
11367 vec
<tree
, va_gc
> *orig_args
= NULL
;
11369 auto_cond_timevar
tv (TV_OVERLOAD
);
11371 gcc_assert (instance
!= NULL_TREE
);
11373 /* We don't know what function we're going to call, yet. */
11377 if (error_operand_p (instance
)
11378 || !fns
|| error_operand_p (fns
))
11379 return error_mark_node
;
11381 if (!BASELINK_P (fns
))
11383 if (complain
& tf_error
)
11384 error ("call to non-function %qD", fns
);
11385 return error_mark_node
;
11388 orig_instance
= instance
;
11391 /* Dismantle the baselink to collect all the information we need. */
11392 if (!conversion_path
)
11393 conversion_path
= BASELINK_BINFO (fns
);
11394 access_binfo
= BASELINK_ACCESS_BINFO (fns
);
11395 optype
= BASELINK_OPTYPE (fns
);
11396 fns
= BASELINK_FUNCTIONS (fns
);
11397 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11399 explicit_targs
= TREE_OPERAND (fns
, 1);
11400 fns
= TREE_OPERAND (fns
, 0);
11403 gcc_assert (OVL_P (fns
));
11404 fn
= OVL_FIRST (fns
);
11405 name
= DECL_NAME (fn
);
11407 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (instance
));
11408 gcc_assert (CLASS_TYPE_P (basetype
));
11410 user_args
= args
== NULL
? NULL
: *args
;
11411 /* Under DR 147 A::A() is an invalid constructor call,
11412 not a functional cast. */
11413 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
11415 if (! (complain
& tf_error
))
11416 return error_mark_node
;
11418 basetype
= DECL_CONTEXT (fn
);
11419 name
= constructor_name (basetype
);
11420 auto_diagnostic_group d
;
11421 if (permerror (input_location
,
11422 "cannot call constructor %<%T::%D%> directly",
11424 inform (input_location
, "for a function-style cast, remove the "
11425 "redundant %<::%D%>", name
);
11426 call
= build_functional_cast (input_location
, basetype
,
11427 build_tree_list_vec (user_args
),
11432 if (processing_template_decl
)
11434 orig_args
= args
== NULL
? NULL
: make_tree_vector_copy (*args
);
11435 instance
= build_non_dependent_expr (instance
);
11437 make_args_non_dependent (*args
);
11440 /* Process the argument list. */
11441 if (args
!= NULL
&& *args
!= NULL
)
11443 *args
= resolve_args (*args
, complain
);
11445 return error_mark_node
;
11449 /* Consider the object argument to be used even if we end up selecting a
11450 static member function. */
11451 instance
= mark_type_use (instance
);
11453 /* Figure out whether to skip the first argument for the error
11454 message we will display to users if an error occurs. We don't
11455 want to display any compiler-generated arguments. The "this"
11456 pointer hasn't been added yet. However, we must remove the VTT
11457 pointer if this is a call to a base-class constructor or
11459 skip_first_for_error
= false;
11460 if (IDENTIFIER_CDTOR_P (name
))
11462 /* Callers should explicitly indicate whether they want to ctor
11463 the complete object or just the part without virtual bases. */
11464 gcc_assert (name
!= ctor_identifier
);
11466 /* Remove the VTT pointer, if present. */
11467 if ((name
== base_ctor_identifier
|| name
== base_dtor_identifier
)
11468 && CLASSTYPE_VBASECLASSES (basetype
))
11469 skip_first_for_error
= true;
11471 /* It's OK to call destructors and constructors on cv-qualified
11472 objects. Therefore, convert the INSTANCE to the unqualified
11473 type, if necessary. */
11474 if (!same_type_p (basetype
, TREE_TYPE (instance
)))
11476 instance
= build_this (instance
);
11477 instance
= build_nop (build_pointer_type (basetype
), instance
);
11478 instance
= build_fold_indirect_ref (instance
);
11482 gcc_assert (!DECL_DESTRUCTOR_P (fn
) && !DECL_CONSTRUCTOR_P (fn
));
11484 /* For the overload resolution we need to find the actual `this`
11485 that would be captured if the call turns out to be to a
11486 non-static member function. Do not actually capture it at this
11488 if (DECL_CONSTRUCTOR_P (fn
))
11489 /* Constructors don't use the enclosing 'this'. */
11490 first_mem_arg
= instance
;
11492 first_mem_arg
= maybe_resolve_dummy (instance
, false);
11494 conversion_obstack_sentinel cos
;
11496 /* The number of arguments artificial parms in ARGS; we subtract one because
11497 there's no 'this' in ARGS. */
11498 unsigned skip
= num_artificial_parms_for (fn
) - 1;
11500 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
11501 initializer, not T({ }). */
11502 if (DECL_CONSTRUCTOR_P (fn
)
11503 && vec_safe_length (user_args
) > skip
11504 && DIRECT_LIST_INIT_P ((*user_args
)[skip
]))
11506 tree init_list
= (*user_args
)[skip
];
11507 tree init
= NULL_TREE
;
11509 gcc_assert (user_args
->length () == skip
+ 1
11510 && !(flags
& LOOKUP_ONLYCONVERTING
));
11512 /* If the initializer list has no elements and T is a class type with
11513 a default constructor, the object is value-initialized. Handle
11514 this here so we don't need to handle it wherever we use
11515 build_special_member_call. */
11516 if (CONSTRUCTOR_NELTS (init_list
) == 0
11517 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
11518 /* For a user-provided default constructor, use the normal
11519 mechanisms so that protected access works. */
11520 && type_has_non_user_provided_default_constructor (basetype
)
11521 && !processing_template_decl
)
11522 init
= build_value_init (basetype
, complain
);
11524 /* If BASETYPE is an aggregate, we need to do aggregate
11526 else if (CP_AGGREGATE_TYPE_P (basetype
))
11528 init
= reshape_init (basetype
, init_list
, complain
);
11529 init
= digest_init (basetype
, init
, complain
);
11534 if (is_dummy_object (instance
))
11535 return get_target_expr (init
, complain
);
11536 return cp_build_init_expr (instance
, init
);
11539 /* Otherwise go ahead with overload resolution. */
11540 add_list_candidates (fns
, first_mem_arg
, user_args
,
11541 basetype
, explicit_targs
, template_only
,
11542 conversion_path
, access_binfo
, flags
,
11543 &candidates
, complain
);
11546 add_candidates (fns
, first_mem_arg
, user_args
, optype
,
11547 explicit_targs
, template_only
, conversion_path
,
11548 access_binfo
, flags
, &candidates
, complain
);
11550 any_viable_p
= false;
11551 candidates
= splice_viable (candidates
, false, &any_viable_p
);
11555 /* [dcl.init], 17.6.2.2:
11557 Otherwise, if no constructor is viable, the destination type is
11558 a (possibly cv-qualified) aggregate class A, and the initializer
11559 is a parenthesized expression-list, the object is initialized as
11562 We achieve this by building up a CONSTRUCTOR, as for list-init,
11563 and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
11565 if (DECL_CONSTRUCTOR_P (fn
)
11566 && !(flags
& LOOKUP_ONLYCONVERTING
)
11567 && cxx_dialect
>= cxx20
11568 && CP_AGGREGATE_TYPE_P (basetype
)
11569 && !vec_safe_is_empty (user_args
))
11571 /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
11572 tree ctor
= build_constructor_from_vec (init_list_type_node
,
11574 CONSTRUCTOR_IS_DIRECT_INIT (ctor
) = true;
11575 CONSTRUCTOR_IS_PAREN_INIT (ctor
) = true;
11576 if (is_dummy_object (instance
))
11580 ctor
= digest_init (basetype
, ctor
, complain
);
11581 if (ctor
== error_mark_node
)
11582 return error_mark_node
;
11583 return cp_build_init_expr (instance
, ctor
);
11586 if (complain
& tf_error
)
11587 complain_about_no_candidates_for_method_call (instance
, candidates
,
11588 explicit_targs
, basetype
,
11590 skip_first_for_error
,
11592 call
= error_mark_node
;
11596 cand
= tourney (candidates
, complain
);
11603 if (complain
& tf_error
)
11605 pretty_name
= name_as_c_string (name
, basetype
, &free_p
);
11606 arglist
= build_tree_list_vec (user_args
);
11607 if (skip_first_for_error
)
11608 arglist
= TREE_CHAIN (arglist
);
11609 auto_diagnostic_group d
;
11610 if (!any_strictly_viable (candidates
))
11611 error ("no matching function for call to %<%s(%A)%>",
11612 pretty_name
, arglist
);
11614 error ("call of overloaded %<%s(%A)%> is ambiguous",
11615 pretty_name
, arglist
);
11616 print_z_candidates (location_of (name
), candidates
);
11618 free (pretty_name
);
11620 call
= error_mark_node
;
11622 *fn_p
= error_mark_node
;
11629 if (!(flags
& LOOKUP_NONVIRTUAL
)
11630 && DECL_PURE_VIRTUAL_P (fn
)
11631 && instance
== current_class_ref
11632 && (complain
& tf_warning
))
11634 /* This is not an error, it is runtime undefined
11636 if (!current_function_decl
)
11637 warning (0, "pure virtual %q#D called from "
11638 "non-static data member initializer", fn
);
11639 else if (DECL_CONSTRUCTOR_P (current_function_decl
)
11640 || DECL_DESTRUCTOR_P (current_function_decl
))
11641 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl
)
11642 ? G_("pure virtual %q#D called from constructor")
11643 : G_("pure virtual %q#D called from destructor")),
11647 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
11648 && !DECL_CONSTRUCTOR_P (fn
)
11649 && is_dummy_object (instance
))
11651 instance
= maybe_resolve_dummy (instance
, true);
11652 if (instance
== error_mark_node
)
11653 call
= error_mark_node
;
11654 else if (!is_dummy_object (instance
))
11656 /* We captured 'this' in the current lambda now that
11657 we know we really need it. */
11658 cand
->first_arg
= instance
;
11660 else if (current_class_ptr
&& any_dependent_bases_p ())
11661 /* We can't tell until instantiation time whether we can use
11662 *this as the implicit object argument. */;
11665 if (complain
& tf_error
)
11666 error ("cannot call member function %qD without object",
11668 call
= error_mark_node
;
11672 if (call
!= error_mark_node
)
11674 /* Now we know what function is being called. */
11677 /* Build the actual CALL_EXPR. */
11678 call
= build_over_call (cand
, flags
, complain
);
11680 /* Suppress warnings for if (my_struct.operator= (x)) where
11681 my_struct is implicitly converted to bool. */
11682 if (TREE_CODE (call
) == MODIFY_EXPR
)
11683 suppress_warning (call
, OPT_Wparentheses
);
11685 /* In an expression of the form `a->f()' where `f' turns
11686 out to be a static member function, `a' is
11687 none-the-less evaluated. */
11688 if (!is_dummy_object (instance
))
11689 call
= keep_unused_object_arg (call
, instance
, fn
);
11690 if (call
!= error_mark_node
11691 && DECL_DESTRUCTOR_P (cand
->fn
)
11692 && !VOID_TYPE_P (TREE_TYPE (call
)))
11693 /* An explicit call of the form "x->~X()" has type
11694 "void". However, on platforms where destructors
11695 return "this" (i.e., those where
11696 targetm.cxx.cdtor_returns_this is true), such calls
11697 will appear to have a return value of pointer type
11698 to the low-level call machinery. We do not want to
11699 change the low-level machinery, since we want to be
11700 able to optimize "delete f()" on such platforms as
11701 "operator delete(~X(f()))" (rather than generating
11702 "t = f(), ~X(t), operator delete (t)"). */
11703 call
= build_nop (void_type_node
, call
);
11708 if (processing_template_decl
&& call
!= error_mark_node
)
11710 bool cast_to_void
= false;
11712 if (TREE_CODE (call
) == COMPOUND_EXPR
)
11713 call
= TREE_OPERAND (call
, 1);
11714 else if (TREE_CODE (call
) == NOP_EXPR
)
11716 cast_to_void
= true;
11717 call
= TREE_OPERAND (call
, 0);
11719 if (INDIRECT_REF_P (call
))
11720 call
= TREE_OPERAND (call
, 0);
11722 /* Prune all but the selected function from the original overload
11723 set so that we can avoid some duplicate work at instantiation time. */
11724 if (really_overloaded_fn (fns
))
11726 if (DECL_TEMPLATE_INFO (fn
)
11727 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
11729 /* Use the selected template, not the specialization, so that
11730 this looks like an actual lookup result for sake of
11731 filter_memfn_lookup. */
11733 if (OVL_SINGLE_P (fns
))
11734 /* If the original overload set consists of a single function
11735 template, this isn't beneficial. */
11738 fn
= ovl_make (DECL_TI_TEMPLATE (fn
));
11740 fn
= lookup_template_function (fn
, explicit_targs
);
11742 orig_fns
= copy_node (orig_fns
);
11743 BASELINK_FUNCTIONS (orig_fns
) = fn
;
11744 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns
) = true;
11748 call
= (build_min_non_dep_call_vec
11750 build_min (COMPONENT_REF
, TREE_TYPE (CALL_EXPR_FN (call
)),
11751 orig_instance
, orig_fns
, NULL_TREE
),
11753 SET_EXPR_LOCATION (call
, input_location
);
11754 call
= convert_from_reference (call
);
11756 call
= build_nop (void_type_node
, call
);
11759 if (orig_args
!= NULL
)
11760 release_tree_vector (orig_args
);
11765 /* Returns true iff standard conversion sequence ICS1 is a proper
11766 subsequence of ICS2. */
11769 is_subseq (conversion
*ics1
, conversion
*ics2
)
11771 /* We can assume that a conversion of the same code
11772 between the same types indicates a subsequence since we only get
11773 here if the types we are converting from are the same. */
11775 while (ics1
->kind
== ck_rvalue
11776 || ics1
->kind
== ck_lvalue
)
11777 ics1
= next_conversion (ics1
);
11781 while (ics2
->kind
== ck_rvalue
11782 || ics2
->kind
== ck_lvalue
)
11783 ics2
= next_conversion (ics2
);
11785 if (ics2
->kind
== ck_user
11786 || !has_next (ics2
->kind
))
11787 /* At this point, ICS1 cannot be a proper subsequence of
11788 ICS2. We can get a USER_CONV when we are comparing the
11789 second standard conversion sequence of two user conversion
11793 ics2
= next_conversion (ics2
);
11795 while (ics2
->kind
== ck_rvalue
11796 || ics2
->kind
== ck_lvalue
)
11797 ics2
= next_conversion (ics2
);
11799 if (ics2
->kind
== ics1
->kind
11800 && same_type_p (ics2
->type
, ics1
->type
)
11801 && (ics1
->kind
== ck_identity
11802 || same_type_p (next_conversion (ics2
)->type
,
11803 next_conversion (ics1
)->type
)))
11808 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
11809 be any _TYPE nodes. */
11812 is_properly_derived_from (tree derived
, tree base
)
11814 if (!CLASS_TYPE_P (derived
) || !CLASS_TYPE_P (base
))
11817 /* We only allow proper derivation here. The DERIVED_FROM_P macro
11818 considers every class derived from itself. */
11819 return (!same_type_ignoring_top_level_qualifiers_p (derived
, base
)
11820 && DERIVED_FROM_P (base
, derived
));
11823 /* We build the ICS for an implicit object parameter as a pointer
11824 conversion sequence. However, such a sequence should be compared
11825 as if it were a reference conversion sequence. If ICS is the
11826 implicit conversion sequence for an implicit object parameter,
11827 modify it accordingly. */
11830 maybe_handle_implicit_object (conversion
**ics
)
11832 if ((*ics
)->this_p
)
11834 /* [over.match.funcs]
11836 For non-static member functions, the type of the
11837 implicit object parameter is "reference to cv X"
11838 where X is the class of which the function is a
11839 member and cv is the cv-qualification on the member
11840 function declaration. */
11841 conversion
*t
= *ics
;
11842 tree reference_type
;
11844 /* The `this' parameter is a pointer to a class type. Make the
11845 implicit conversion talk about a reference to that same class
11847 reference_type
= TREE_TYPE (t
->type
);
11848 reference_type
= build_reference_type (reference_type
);
11850 if (t
->kind
== ck_qual
)
11851 t
= next_conversion (t
);
11852 if (t
->kind
== ck_ptr
)
11853 t
= next_conversion (t
);
11854 t
= build_identity_conv (TREE_TYPE (t
->type
), NULL_TREE
);
11855 t
= direct_reference_binding (reference_type
, t
);
11857 t
->rvaluedness_matches_p
= 0;
11862 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
11863 and return the initial reference binding conversion. Otherwise,
11864 leave *ICS unchanged and return NULL. */
11866 static conversion
*
11867 maybe_handle_ref_bind (conversion
**ics
)
11869 if ((*ics
)->kind
== ck_ref_bind
)
11871 conversion
*old_ics
= *ics
;
11872 *ics
= next_conversion (old_ics
);
11873 (*ics
)->user_conv_p
= old_ics
->user_conv_p
;
11880 /* Get the expression at the beginning of the conversion chain C. */
11883 conv_get_original_expr (conversion
*c
)
11885 for (; c
; c
= next_conversion (c
))
11886 if (c
->kind
== ck_identity
|| c
->kind
== ck_ambig
|| c
->kind
== ck_aggr
)
11891 /* Return a tree representing the number of elements initialized by the
11892 list-initialization C. The caller must check that C converts to an
11896 nelts_initialized_by_list_init (conversion
*c
)
11898 /* If the array we're converting to has a dimension, we'll use that. */
11899 if (TYPE_DOMAIN (c
->type
))
11900 return array_type_nelts_top (c
->type
);
11903 /* Otherwise, we look at how many elements the constructor we're
11904 initializing from has. */
11905 tree ctor
= conv_get_original_expr (c
);
11906 return size_int (CONSTRUCTOR_NELTS (ctor
));
11910 /* True iff C is a conversion that binds a reference or a pointer to
11911 an array of unknown bound. */
11914 conv_binds_to_array_of_unknown_bound (conversion
*c
)
11916 /* ck_ref_bind won't have the reference stripped. */
11917 tree type
= non_reference (c
->type
);
11918 /* ck_qual won't have the pointer stripped. */
11919 type
= strip_pointer_operator (type
);
11920 return (TREE_CODE (type
) == ARRAY_TYPE
11921 && TYPE_DOMAIN (type
) == NULL_TREE
);
11924 /* Compare two implicit conversion sequences according to the rules set out in
11925 [over.ics.rank]. Return values:
11927 1: ics1 is better than ics2
11928 -1: ics2 is better than ics1
11929 0: ics1 and ics2 are indistinguishable */
11932 compare_ics (conversion
*ics1
, conversion
*ics2
)
11938 tree deref_from_type1
= NULL_TREE
;
11939 tree deref_from_type2
= NULL_TREE
;
11940 tree deref_to_type1
= NULL_TREE
;
11941 tree deref_to_type2
= NULL_TREE
;
11942 conversion_rank rank1
, rank2
;
11944 /* REF_BINDING is nonzero if the result of the conversion sequence
11945 is a reference type. In that case REF_CONV is the reference
11946 binding conversion. */
11947 conversion
*ref_conv1
;
11948 conversion
*ref_conv2
;
11950 /* Compare badness before stripping the reference conversion. */
11951 if (ics1
->bad_p
> ics2
->bad_p
)
11953 else if (ics1
->bad_p
< ics2
->bad_p
)
11956 /* Handle implicit object parameters. */
11957 maybe_handle_implicit_object (&ics1
);
11958 maybe_handle_implicit_object (&ics2
);
11960 /* Handle reference parameters. */
11961 ref_conv1
= maybe_handle_ref_bind (&ics1
);
11962 ref_conv2
= maybe_handle_ref_bind (&ics2
);
11964 /* List-initialization sequence L1 is a better conversion sequence than
11965 list-initialization sequence L2 if L1 converts to
11966 std::initializer_list<X> for some X and L2 does not. */
11967 if (ics1
->kind
== ck_list
&& ics2
->kind
!= ck_list
)
11969 if (ics2
->kind
== ck_list
&& ics1
->kind
!= ck_list
)
11974 When comparing the basic forms of implicit conversion sequences (as
11975 defined in _over.best.ics_)
11977 --a standard conversion sequence (_over.ics.scs_) is a better
11978 conversion sequence than a user-defined conversion sequence
11979 or an ellipsis conversion sequence, and
11981 --a user-defined conversion sequence (_over.ics.user_) is a
11982 better conversion sequence than an ellipsis conversion sequence
11983 (_over.ics.ellipsis_). */
11984 /* Use BAD_CONVERSION_RANK because we already checked for a badness
11985 mismatch. If both ICS are bad, we try to make a decision based on
11986 what would have happened if they'd been good. This is not an
11987 extension, we'll still give an error when we build up the call; this
11988 just helps us give a more helpful error message. */
11989 rank1
= BAD_CONVERSION_RANK (ics1
);
11990 rank2
= BAD_CONVERSION_RANK (ics2
);
11994 else if (rank1
< rank2
)
11997 if (ics1
->ellipsis_p
)
11998 /* Both conversions are ellipsis conversions. */
12001 /* User-defined conversion sequence U1 is a better conversion sequence
12002 than another user-defined conversion sequence U2 if they contain the
12003 same user-defined conversion operator or constructor and if the sec-
12004 ond standard conversion sequence of U1 is better than the second
12005 standard conversion sequence of U2. */
12007 /* Handle list-conversion with the same code even though it isn't always
12008 ranked as a user-defined conversion and it doesn't have a second
12009 standard conversion sequence; it will still have the desired effect.
12010 Specifically, we need to do the reference binding comparison at the
12011 end of this function. */
12013 if (ics1
->user_conv_p
|| ics1
->kind
== ck_list
12014 || ics1
->kind
== ck_aggr
|| ics2
->kind
== ck_aggr
)
12016 conversion
*t1
= strip_standard_conversion (ics1
);
12017 conversion
*t2
= strip_standard_conversion (ics2
);
12019 if (!t1
|| !t2
|| t1
->kind
!= t2
->kind
)
12021 else if (t1
->kind
== ck_user
)
12023 tree f1
= t1
->cand
? t1
->cand
->fn
: t1
->type
;
12024 tree f2
= t2
->cand
? t2
->cand
->fn
: t2
->type
;
12028 /* List-initialization sequence L1 is a better conversion sequence than
12029 list-initialization sequence L2 if
12031 -- L1 and L2 convert to arrays of the same element type, and either
12032 the number of elements n1 initialized by L1 is less than the number
12033 of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
12034 of unknown bound and L1 does not. (Added in CWG 1307 and extended by
12036 else if (t1
->kind
== ck_aggr
12037 && TREE_CODE (t1
->type
) == ARRAY_TYPE
12038 && TREE_CODE (t2
->type
) == ARRAY_TYPE
12039 && same_type_p (TREE_TYPE (t1
->type
), TREE_TYPE (t2
->type
)))
12041 tree n1
= nelts_initialized_by_list_init (t1
);
12042 tree n2
= nelts_initialized_by_list_init (t2
);
12043 if (tree_int_cst_lt (n1
, n2
))
12045 else if (tree_int_cst_lt (n2
, n1
))
12047 /* The n1 == n2 case. */
12048 bool c1
= conv_binds_to_array_of_unknown_bound (t1
);
12049 bool c2
= conv_binds_to_array_of_unknown_bound (t2
);
12052 else if (!c1
&& c2
)
12059 /* For ambiguous or aggregate conversions, use the target type as
12060 a proxy for the conversion function. */
12061 if (!same_type_ignoring_top_level_qualifiers_p (t1
->type
, t2
->type
))
12065 /* We can just fall through here, after setting up
12066 FROM_TYPE1 and FROM_TYPE2. */
12067 from_type1
= t1
->type
;
12068 from_type2
= t2
->type
;
12075 /* We're dealing with two standard conversion sequences.
12079 Standard conversion sequence S1 is a better conversion
12080 sequence than standard conversion sequence S2 if
12082 --S1 is a proper subsequence of S2 (comparing the conversion
12083 sequences in the canonical form defined by _over.ics.scs_,
12084 excluding any Lvalue Transformation; the identity
12085 conversion sequence is considered to be a subsequence of
12086 any non-identity conversion sequence */
12089 while (t1
->kind
!= ck_identity
)
12090 t1
= next_conversion (t1
);
12091 from_type1
= t1
->type
;
12094 while (t2
->kind
!= ck_identity
)
12095 t2
= next_conversion (t2
);
12096 from_type2
= t2
->type
;
12099 /* One sequence can only be a subsequence of the other if they start with
12100 the same type. They can start with different types when comparing the
12101 second standard conversion sequence in two user-defined conversion
12103 if (same_type_p (from_type1
, from_type2
))
12105 if (is_subseq (ics1
, ics2
))
12107 if (is_subseq (ics2
, ics1
))
12115 --the rank of S1 is better than the rank of S2 (by the rules
12118 Standard conversion sequences are ordered by their ranks: an Exact
12119 Match is a better conversion than a Promotion, which is a better
12120 conversion than a Conversion.
12122 Two conversion sequences with the same rank are indistinguishable
12123 unless one of the following rules applies:
12125 --A conversion that does not a convert a pointer, pointer to member,
12126 or std::nullptr_t to bool is better than one that does.
12128 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
12129 so that we do not have to check it explicitly. */
12130 if (ics1
->rank
< ics2
->rank
)
12132 else if (ics2
->rank
< ics1
->rank
)
12135 to_type1
= ics1
->type
;
12136 to_type2
= ics2
->type
;
12138 /* A conversion from scalar arithmetic type to complex is worse than a
12139 conversion between scalar arithmetic types. */
12140 if (same_type_p (from_type1
, from_type2
)
12141 && ARITHMETIC_TYPE_P (from_type1
)
12142 && ARITHMETIC_TYPE_P (to_type1
)
12143 && ARITHMETIC_TYPE_P (to_type2
)
12144 && ((TREE_CODE (to_type1
) == COMPLEX_TYPE
)
12145 != (TREE_CODE (to_type2
) == COMPLEX_TYPE
)))
12147 if (TREE_CODE (to_type1
) == COMPLEX_TYPE
)
12154 /* A conversion in either direction between floating-point type FP1 and
12155 floating-point type FP2 is better than a conversion in the same
12156 direction between FP1 and arithmetic type T3 if
12157 - the floating-point conversion rank of FP1 is equal to the rank of
12159 - T3 is not a floating-point type, or T3 is a floating-point type
12160 whose rank is not equal to the rank of FP1, or the floating-point
12161 conversion subrank of FP2 is greater than the subrank of T3. */
12162 tree fp1
= from_type1
;
12163 tree fp2
= to_type1
;
12164 tree fp3
= from_type2
;
12165 tree t3
= to_type2
;
12167 if (TYPE_MAIN_VARIANT (fp2
) == TYPE_MAIN_VARIANT (t3
))
12169 std::swap (fp1
, fp2
);
12170 std::swap (fp3
, t3
);
12172 if (TYPE_MAIN_VARIANT (fp1
) == TYPE_MAIN_VARIANT (fp3
)
12173 && SCALAR_FLOAT_TYPE_P (fp1
)
12174 /* Only apply this rule if at least one of the 3 types is
12175 extended floating-point type, otherwise keep them as
12176 before for compatibility reasons with types like __float128.
12177 float, double and long double alone have different conversion
12178 ranks and so when just those 3 types are involved, this
12179 rule doesn't trigger. */
12180 && (extended_float_type_p (fp1
)
12181 || (SCALAR_FLOAT_TYPE_P (fp2
) && extended_float_type_p (fp2
))
12182 || (SCALAR_FLOAT_TYPE_P (t3
) && extended_float_type_p (t3
))))
12184 if (TREE_CODE (fp2
) != REAL_TYPE
)
12187 std::swap (fp2
, t3
);
12189 if (SCALAR_FLOAT_TYPE_P (fp2
))
12191 /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12192 if the conversion rank is equal (-1 or 1 if the subrank is
12194 if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1
,
12198 /* Conversion ranks of FP1 and FP2 are equal. */
12199 if (TREE_CODE (t3
) != REAL_TYPE
12200 || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12203 /* FP1 <-> FP2 conversion is better. */
12205 int c
= cp_compare_floating_point_conversion_ranks (fp2
, t3
);
12206 gcc_assert (IN_RANGE (c
, -1, 1));
12208 /* Conversion subrank of FP2 is greater than subrank of T3.
12209 FP1 <-> FP2 conversion is better. */
12212 /* Conversion subrank of FP2 is less than subrank of T3.
12213 FP1 <-> T3 conversion is better. */
12216 else if (SCALAR_FLOAT_TYPE_P (t3
)
12217 && IN_RANGE (cp_compare_floating_point_conversion_ranks
12220 /* Conversion ranks of FP1 and FP2 are not equal, conversion
12221 ranks of FP1 and T3 are equal.
12222 FP1 <-> T3 conversion is better. */
12228 if (TYPE_PTR_P (from_type1
)
12229 && TYPE_PTR_P (from_type2
)
12230 && TYPE_PTR_P (to_type1
)
12231 && TYPE_PTR_P (to_type2
))
12233 deref_from_type1
= TREE_TYPE (from_type1
);
12234 deref_from_type2
= TREE_TYPE (from_type2
);
12235 deref_to_type1
= TREE_TYPE (to_type1
);
12236 deref_to_type2
= TREE_TYPE (to_type2
);
12238 /* The rules for pointers to members A::* are just like the rules
12239 for pointers A*, except opposite: if B is derived from A then
12240 A::* converts to B::*, not vice versa. For that reason, we
12241 switch the from_ and to_ variables here. */
12242 else if ((TYPE_PTRDATAMEM_P (from_type1
) && TYPE_PTRDATAMEM_P (from_type2
)
12243 && TYPE_PTRDATAMEM_P (to_type1
) && TYPE_PTRDATAMEM_P (to_type2
))
12244 || (TYPE_PTRMEMFUNC_P (from_type1
)
12245 && TYPE_PTRMEMFUNC_P (from_type2
)
12246 && TYPE_PTRMEMFUNC_P (to_type1
)
12247 && TYPE_PTRMEMFUNC_P (to_type2
)))
12249 deref_to_type1
= TYPE_PTRMEM_CLASS_TYPE (from_type1
);
12250 deref_to_type2
= TYPE_PTRMEM_CLASS_TYPE (from_type2
);
12251 deref_from_type1
= TYPE_PTRMEM_CLASS_TYPE (to_type1
);
12252 deref_from_type2
= TYPE_PTRMEM_CLASS_TYPE (to_type2
);
12255 if (deref_from_type1
!= NULL_TREE
12256 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1
))
12257 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2
)))
12259 /* This was one of the pointer or pointer-like conversions.
12263 --If class B is derived directly or indirectly from class A,
12264 conversion of B* to A* is better than conversion of B* to
12265 void*, and conversion of A* to void* is better than
12266 conversion of B* to void*. */
12267 if (VOID_TYPE_P (deref_to_type1
)
12268 && VOID_TYPE_P (deref_to_type2
))
12270 if (is_properly_derived_from (deref_from_type1
,
12273 else if (is_properly_derived_from (deref_from_type2
,
12277 else if (VOID_TYPE_P (deref_to_type1
)
12278 || VOID_TYPE_P (deref_to_type2
))
12280 if (same_type_p (deref_from_type1
, deref_from_type2
))
12282 if (VOID_TYPE_P (deref_to_type2
))
12284 if (is_properly_derived_from (deref_from_type1
,
12288 /* We know that DEREF_TO_TYPE1 is `void' here. */
12289 else if (is_properly_derived_from (deref_from_type1
,
12294 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1
))
12295 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2
)))
12299 --If class B is derived directly or indirectly from class A
12300 and class C is derived directly or indirectly from B,
12302 --conversion of C* to B* is better than conversion of C* to
12305 --conversion of B* to A* is better than conversion of C* to
12307 if (same_type_p (deref_from_type1
, deref_from_type2
))
12309 if (is_properly_derived_from (deref_to_type1
,
12312 else if (is_properly_derived_from (deref_to_type2
,
12316 else if (same_type_p (deref_to_type1
, deref_to_type2
))
12318 if (is_properly_derived_from (deref_from_type2
,
12321 else if (is_properly_derived_from (deref_from_type1
,
12327 else if (CLASS_TYPE_P (non_reference (from_type1
))
12328 && same_type_p (from_type1
, from_type2
))
12330 tree from
= non_reference (from_type1
);
12334 --binding of an expression of type C to a reference of type
12335 B& is better than binding an expression of type C to a
12336 reference of type A&
12338 --conversion of C to B is better than conversion of C to A, */
12339 if (is_properly_derived_from (from
, to_type1
)
12340 && is_properly_derived_from (from
, to_type2
))
12342 if (is_properly_derived_from (to_type1
, to_type2
))
12344 else if (is_properly_derived_from (to_type2
, to_type1
))
12348 else if (CLASS_TYPE_P (non_reference (to_type1
))
12349 && same_type_p (to_type1
, to_type2
))
12351 tree to
= non_reference (to_type1
);
12355 --binding of an expression of type B to a reference of type
12356 A& is better than binding an expression of type C to a
12357 reference of type A&,
12359 --conversion of B to A is better than conversion of C to A */
12360 if (is_properly_derived_from (from_type1
, to
)
12361 && is_properly_derived_from (from_type2
, to
))
12363 if (is_properly_derived_from (from_type2
, from_type1
))
12365 else if (is_properly_derived_from (from_type1
, from_type2
))
12372 --S1 and S2 differ only in their qualification conversion and yield
12373 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
12374 qualification signature of type T1 is a proper subset of the cv-
12375 qualification signature of type T2 */
12376 if (ics1
->kind
== ck_qual
12377 && ics2
->kind
== ck_qual
12378 && same_type_p (from_type1
, from_type2
))
12380 int result
= comp_cv_qual_signature (to_type1
, to_type2
);
12387 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
12388 to an implicit object parameter of a non-static member function
12389 declared without a ref-qualifier, and either S1 binds an lvalue
12390 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
12391 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
12392 draft standard, 13.3.3.2)
12394 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
12395 types to which the references refer are the same type except for
12396 top-level cv-qualifiers, and the type to which the reference
12397 initialized by S2 refers is more cv-qualified than the type to
12398 which the reference initialized by S1 refers.
12400 DR 1328 [over.match.best]: the context is an initialization by
12401 conversion function for direct reference binding (13.3.1.6) of a
12402 reference to function type, the return type of F1 is the same kind of
12403 reference (i.e. lvalue or rvalue) as the reference being initialized,
12404 and the return type of F2 is not. */
12406 if (ref_conv1
&& ref_conv2
)
12408 if (!ref_conv1
->this_p
&& !ref_conv2
->this_p
12409 && (ref_conv1
->rvaluedness_matches_p
12410 != ref_conv2
->rvaluedness_matches_p
)
12411 && (same_type_p (ref_conv1
->type
, ref_conv2
->type
)
12412 || (TYPE_REF_IS_RVALUE (ref_conv1
->type
)
12413 != TYPE_REF_IS_RVALUE (ref_conv2
->type
))))
12415 if (ref_conv1
->bad_p
12416 && !same_type_p (TREE_TYPE (ref_conv1
->type
),
12417 TREE_TYPE (ref_conv2
->type
)))
12418 /* Don't prefer a bad conversion that drops cv-quals to a bad
12419 conversion with the wrong rvalueness. */
12421 return (ref_conv1
->rvaluedness_matches_p
12422 - ref_conv2
->rvaluedness_matches_p
);
12425 if (same_type_ignoring_top_level_qualifiers_p (to_type1
, to_type2
))
12429 void f (int(&)[]), // (1)
12430 f (int(&)[1]), // (2)
12433 (2) is better than (1), but (3) should be equal to (1) and to
12434 (2). For that reason we don't use ck_qual for (1) which would
12435 give it the cr_exact rank while (3) remains ck_identity.
12436 Therefore we compare (1) and (2) here. For (1) we'll have
12438 ck_ref_bind <- ck_identity
12441 so to handle this we must look at ref_conv. */
12442 bool c1
= conv_binds_to_array_of_unknown_bound (ref_conv1
);
12443 bool c2
= conv_binds_to_array_of_unknown_bound (ref_conv2
);
12446 else if (!c1
&& c2
)
12449 int q1
= cp_type_quals (TREE_TYPE (ref_conv1
->type
));
12450 int q2
= cp_type_quals (TREE_TYPE (ref_conv2
->type
));
12451 if (ref_conv1
->bad_p
)
12453 /* Prefer the one that drops fewer cv-quals. */
12454 tree ftype
= next_conversion (ref_conv1
)->type
;
12455 int fquals
= cp_type_quals (ftype
);
12459 return comp_cv_qualification (q2
, q1
);
12466 -- A conversion that promotes an enumeration whose underlying type
12467 is fixed to its underlying type is better than one that promotes to
12468 the promoted underlying type, if the two are different. */
12469 if (ics1
->rank
== cr_promotion
12470 && ics2
->rank
== cr_promotion
12471 && UNSCOPED_ENUM_P (from_type1
)
12472 && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1
)
12473 && same_type_p (from_type1
, from_type2
))
12475 tree utype
= ENUM_UNDERLYING_TYPE (from_type1
);
12476 tree prom
= type_promotes_to (from_type1
);
12477 if (!same_type_p (utype
, prom
))
12479 if (same_type_p (to_type1
, utype
)
12480 && same_type_p (to_type2
, prom
))
12482 else if (same_type_p (to_type2
, utype
)
12483 && same_type_p (to_type1
, prom
))
12488 /* Neither conversion sequence is better than the other. */
12492 /* The source type for this standard conversion sequence. */
12495 source_type (conversion
*t
)
12497 return strip_standard_conversion (t
)->type
;
12500 /* Note a warning about preferring WINNER to LOSER. We do this by storing
12501 a pointer to LOSER and re-running joust to produce the warning if WINNER
12502 is actually used. */
12505 add_warning (struct z_candidate
*winner
, struct z_candidate
*loser
)
12507 candidate_warning
*cw
= (candidate_warning
*)
12508 conversion_obstack_alloc (sizeof (candidate_warning
));
12510 cw
->next
= winner
->warnings
;
12511 winner
->warnings
= cw
;
12514 /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
12515 prvalue returned from a conversion function, return true. Otherwise, return
12519 joust_maybe_elide_copy (z_candidate
*cand
)
12521 tree fn
= cand
->fn
;
12522 if (!DECL_COPY_CONSTRUCTOR_P (fn
) && !DECL_MOVE_CONSTRUCTOR_P (fn
))
12524 conversion
*conv
= cand
->convs
[0];
12525 if (conv
->kind
== ck_ambig
)
12527 gcc_checking_assert (conv
->kind
== ck_ref_bind
);
12528 conv
= next_conversion (conv
);
12529 if (conv
->kind
== ck_user
&& !TYPE_REF_P (conv
->type
))
12531 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
12532 (conv
->type
, DECL_CONTEXT (fn
)));
12533 z_candidate
*uc
= conv
->cand
;
12534 if (DECL_CONV_FN_P (uc
->fn
))
12540 /* True if the defining declarations of the two candidates have equivalent
12544 cand_parms_match (z_candidate
*c1
, z_candidate
*c2
)
12550 if (identifier_p (fn1
) || identifier_p (fn2
))
12552 /* We don't look at c1->template_decl because that's only set for primary
12553 templates, not e.g. non-template member functions of class templates. */
12554 tree t1
= most_general_template (fn1
);
12555 tree t2
= most_general_template (fn2
);
12562 fn1
= DECL_TEMPLATE_RESULT (t1
);
12563 fn2
= DECL_TEMPLATE_RESULT (t2
);
12565 tree parms1
= TYPE_ARG_TYPES (TREE_TYPE (fn1
));
12566 tree parms2
= TYPE_ARG_TYPES (TREE_TYPE (fn2
));
12567 if (DECL_FUNCTION_MEMBER_P (fn1
)
12568 && DECL_FUNCTION_MEMBER_P (fn2
)
12569 && (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn1
)
12570 != DECL_NONSTATIC_MEMBER_FUNCTION_P (fn2
)))
12572 /* Ignore 'this' when comparing the parameters of a static member
12573 function with those of a non-static one. */
12574 parms1
= skip_artificial_parms_for (fn1
, parms1
);
12575 parms2
= skip_artificial_parms_for (fn2
, parms2
);
12577 return compparms (parms1
, parms2
);
12580 /* True iff FN is a copy or move constructor or assignment operator. */
12583 sfk_copy_or_move (tree fn
)
12585 if (TREE_CODE (fn
) != FUNCTION_DECL
)
12587 special_function_kind sfk
= special_function_p (fn
);
12588 return sfk
>= sfk_copy_constructor
&& sfk
<= sfk_move_assignment
;
12591 /* Compare two candidates for overloading as described in
12592 [over.match.best]. Return values:
12594 1: cand1 is better than cand2
12595 -1: cand2 is better than cand1
12596 0: cand1 and cand2 are indistinguishable */
12599 joust (struct z_candidate
*cand1
, struct z_candidate
*cand2
, bool warn
,
12600 tsubst_flags_t complain
)
12603 int off1
= 0, off2
= 0;
12607 /* Candidates that involve bad conversions are always worse than those
12609 if (cand1
->viable
> cand2
->viable
)
12611 if (cand1
->viable
< cand2
->viable
)
12614 /* If we have two pseudo-candidates for conversions to the same type,
12615 or two candidates for the same function, arbitrarily pick one. */
12616 if (cand1
->fn
== cand2
->fn
12617 && cand1
->reversed () == cand2
->reversed ()
12618 && (IS_TYPE_OR_DECL_P (cand1
->fn
)))
12621 /* Prefer a non-deleted function over an implicitly deleted move
12622 constructor or assignment operator. This differs slightly from the
12623 wording for issue 1402 (which says the move op is ignored by overload
12624 resolution), but this way produces better error messages. */
12625 if (TREE_CODE (cand1
->fn
) == FUNCTION_DECL
12626 && TREE_CODE (cand2
->fn
) == FUNCTION_DECL
12627 && DECL_DELETED_FN (cand1
->fn
) != DECL_DELETED_FN (cand2
->fn
))
12629 if (DECL_DELETED_FN (cand1
->fn
) && DECL_DEFAULTED_FN (cand1
->fn
)
12630 && move_fn_p (cand1
->fn
))
12632 if (DECL_DELETED_FN (cand2
->fn
) && DECL_DEFAULTED_FN (cand2
->fn
)
12633 && move_fn_p (cand2
->fn
))
12637 /* a viable function F1
12638 is defined to be a better function than another viable function F2 if
12639 for all arguments i, ICSi(F1) is not a worse conversion sequence than
12640 ICSi(F2), and then */
12642 /* for some argument j, ICSj(F1) is a better conversion sequence than
12645 /* For comparing static and non-static member functions, we ignore
12646 the implicit object parameter of the non-static function. The
12647 standard says to pretend that the static function has an object
12648 parm, but that won't work with operator overloading. */
12649 len
= cand1
->num_convs
;
12650 if (len
!= cand2
->num_convs
)
12652 int static_1
= (TREE_CODE (cand1
->fn
) == FUNCTION_DECL
12653 && DECL_STATIC_FUNCTION_P (cand1
->fn
));
12654 int static_2
= (TREE_CODE (cand2
->fn
) == FUNCTION_DECL
12655 && DECL_STATIC_FUNCTION_P (cand2
->fn
));
12657 if (TREE_CODE (cand1
->fn
) == FUNCTION_DECL
12658 && TREE_CODE (cand2
->fn
) == FUNCTION_DECL
12659 && DECL_CONSTRUCTOR_P (cand1
->fn
)
12660 && is_list_ctor (cand1
->fn
) != is_list_ctor (cand2
->fn
))
12661 /* We're comparing a near-match list constructor and a near-match
12662 non-list constructor. Just treat them as unordered. */
12665 gcc_assert (static_1
!= static_2
);
12669 /* C++23 [over.best.ics.general] says:
12670 When the parameter is the implicit object parameter of a static
12671 member function, the implicit conversion sequence is a standard
12672 conversion sequence that is neither better nor worse than any
12673 other standard conversion sequence. */
12674 if (CONVERSION_RANK (cand2
->convs
[0]) >= cr_user
)
12680 if (CONVERSION_RANK (cand1
->convs
[0]) >= cr_user
)
12687 for (i
= 0; i
< len
; ++i
)
12689 conversion
*t1
= cand1
->convs
[i
+ off1
];
12690 conversion
*t2
= cand2
->convs
[i
+ off2
];
12691 int comp
= compare_ics (t1
, t2
);
12695 if ((complain
& tf_warning
)
12697 && (CONVERSION_RANK (t1
) + CONVERSION_RANK (t2
)
12698 == cr_std
+ cr_promotion
)
12699 && t1
->kind
== ck_std
12700 && t2
->kind
== ck_std
12701 && TREE_CODE (t1
->type
) == INTEGER_TYPE
12702 && TREE_CODE (t2
->type
) == INTEGER_TYPE
12703 && (TYPE_PRECISION (t1
->type
)
12704 == TYPE_PRECISION (t2
->type
))
12705 && (TYPE_UNSIGNED (next_conversion (t1
)->type
)
12706 || (TREE_CODE (next_conversion (t1
)->type
)
12707 == ENUMERAL_TYPE
)))
12709 tree type
= next_conversion (t1
)->type
;
12711 struct z_candidate
*w
, *l
;
12713 type1
= t1
->type
, type2
= t2
->type
,
12714 w
= cand1
, l
= cand2
;
12716 type1
= t2
->type
, type2
= t1
->type
,
12717 w
= cand2
, l
= cand1
;
12721 warning (OPT_Wsign_promo
, "passing %qT chooses %qT over %qT",
12722 type
, type1
, type2
);
12723 warning (OPT_Wsign_promo
, " in call to %qD", w
->fn
);
12726 add_warning (w
, l
);
12729 if (winner
&& comp
!= winner
)
12731 /* Ambiguity between normal and reversed comparison operators
12732 with the same parameter types. P2468 decided not to go with
12733 this approach to resolving the ambiguity, so pedwarn. */
12734 if ((complain
& tf_warning_or_error
)
12735 && (cand1
->reversed () != cand2
->reversed ())
12736 && cand_parms_match (cand1
, cand2
))
12738 struct z_candidate
*w
, *l
;
12739 if (cand2
->reversed ())
12740 winner
= 1, w
= cand1
, l
= cand2
;
12742 winner
= -1, w
= cand2
, l
= cand1
;
12745 auto_diagnostic_group d
;
12746 if (pedwarn (input_location
, 0,
12747 "C++20 says that these are ambiguous, "
12748 "even though the second is reversed:"))
12750 print_z_candidate (input_location
,
12751 N_("candidate 1:"), w
);
12752 print_z_candidate (input_location
,
12753 N_("candidate 2:"), l
);
12755 && DECL_NONSTATIC_MEMBER_FUNCTION_P (w
->fn
)
12756 && (type_memfn_quals (TREE_TYPE (w
->fn
))
12757 & TYPE_QUAL_CONST
) == 0)
12759 /* Suggest adding const to
12760 struct A { bool operator==(const A&); }; */
12762 = FUNCTION_FIRST_USER_PARMTYPE (w
->fn
);
12763 parmtype
= TREE_VALUE (parmtype
);
12764 if (TYPE_REF_P (parmtype
)
12765 && TYPE_READONLY (TREE_TYPE (parmtype
))
12766 && (same_type_ignoring_top_level_qualifiers_p
12767 (TREE_TYPE (parmtype
),
12768 DECL_CONTEXT (w
->fn
))))
12769 inform (DECL_SOURCE_LOCATION (w
->fn
),
12770 "try making the operator a %<const%> "
12771 "member function");
12776 add_warning (w
, l
);
12787 /* warn about confusing overload resolution for user-defined conversions,
12788 either between a constructor and a conversion op, or between two
12790 if ((complain
& tf_warning
)
12791 /* In C++17, the constructor might have been elided, which means that
12792 an originally null ->second_conv could become non-null. */
12793 && winner
&& warn_conversion
&& cand1
->second_conv
&& cand2
->second_conv
12794 && (!DECL_CONSTRUCTOR_P (cand1
->fn
) || !DECL_CONSTRUCTOR_P (cand2
->fn
))
12795 && winner
!= compare_ics (cand1
->second_conv
, cand2
->second_conv
))
12797 struct z_candidate
*w
, *l
;
12798 bool give_warning
= false;
12801 w
= cand1
, l
= cand2
;
12803 w
= cand2
, l
= cand1
;
12805 /* We don't want to complain about `X::operator T1 ()'
12806 beating `X::operator T2 () const', when T2 is a no less
12807 cv-qualified version of T1. */
12808 if (DECL_CONTEXT (w
->fn
) == DECL_CONTEXT (l
->fn
)
12809 && !DECL_CONSTRUCTOR_P (w
->fn
) && !DECL_CONSTRUCTOR_P (l
->fn
))
12811 tree t
= TREE_TYPE (TREE_TYPE (l
->fn
));
12812 tree f
= TREE_TYPE (TREE_TYPE (w
->fn
));
12814 if (TREE_CODE (t
) == TREE_CODE (f
) && INDIRECT_TYPE_P (t
))
12819 if (!comp_ptr_ttypes (t
, f
))
12820 give_warning
= true;
12823 give_warning
= true;
12829 tree source
= source_type (w
->convs
[0]);
12830 if (INDIRECT_TYPE_P (source
))
12831 source
= TREE_TYPE (source
);
12832 auto_diagnostic_group d
;
12833 if (warning (OPT_Wconversion
, "choosing %qD over %qD", w
->fn
, l
->fn
)
12834 && warning (OPT_Wconversion
, " for conversion from %qH to %qI",
12835 source
, w
->second_conv
->type
))
12837 inform (input_location
, " because conversion sequence "
12838 "for the argument is better");
12842 add_warning (w
, l
);
12848 /* DR 495 moved this tiebreaker above the template ones. */
12849 /* or, if not that,
12850 the context is an initialization by user-defined conversion (see
12851 _dcl.init_ and _over.match.user_) and the standard conversion
12852 sequence from the return type of F1 to the destination type (i.e.,
12853 the type of the entity being initialized) is a better conversion
12854 sequence than the standard conversion sequence from the return type
12855 of F2 to the destination type. */
12857 if (cand1
->second_conv
)
12859 winner
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
12864 /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an
12865 explicit conversion (due to list-initialization) is worse. */
12867 z_candidate
*sp
= nullptr;
12868 if (sfk_copy_or_move (cand1
->fn
))
12870 if (sfk_copy_or_move (cand2
->fn
))
12871 sp
= sp
? nullptr : cand2
;
12874 conversion
*conv
= sp
->convs
[!DECL_CONSTRUCTOR_P (sp
->fn
)];
12875 if (conv
->user_conv_p
)
12876 for (; conv
; conv
= next_conversion (conv
))
12877 if (conv
->kind
== ck_user
12878 && DECL_P (conv
->cand
->fn
)
12879 && DECL_NONCONVERTING_P (conv
->cand
->fn
))
12880 return (sp
== cand1
) ? -1 : 1;
12884 /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context.
12885 The standard currently says that only constructors are candidates, but if
12886 one copies a prvalue returned by a conversion function we prefer that.
12888 Clang does something similar, as discussed at
12889 http://lists.isocpp.org/core/2017/10/3166.php
12890 http://lists.isocpp.org/core/2019/03/5721.php */
12891 if (len
== 1 && cxx_dialect
>= cxx17
12892 && DECL_P (cand1
->fn
)
12893 && DECL_COMPLETE_CONSTRUCTOR_P (cand1
->fn
)
12894 && !(cand1
->flags
& LOOKUP_ONLYCONVERTING
))
12896 bool elided1
= joust_maybe_elide_copy (cand1
);
12897 bool elided2
= joust_maybe_elide_copy (cand2
);
12898 winner
= elided1
- elided2
;
12903 /* or, if not that,
12904 F1 is a non-template function and F2 is a template function
12907 if (!cand1
->template_decl
&& cand2
->template_decl
)
12909 else if (cand1
->template_decl
&& !cand2
->template_decl
)
12912 /* or, if not that,
12913 F1 and F2 are template functions and the function template for F1 is
12914 more specialized than the template for F2 according to the partial
12917 if (cand1
->template_decl
&& cand2
->template_decl
)
12919 winner
= more_specialized_fn
12920 (TI_TEMPLATE (cand1
->template_decl
),
12921 TI_TEMPLATE (cand2
->template_decl
),
12922 /* [temp.func.order]: The presence of unused ellipsis and default
12923 arguments has no effect on the partial ordering of function
12924 templates. add_function_candidate() will not have
12925 counted the "this" argument for constructors. */
12926 cand1
->num_convs
+ DECL_CONSTRUCTOR_P (cand1
->fn
));
12931 /* Concepts: F1 and F2 are non-template functions with the same
12932 parameter-type-lists, and F1 is more constrained than F2 according to the
12933 partial ordering of constraints described in 13.5.4. */
12935 if (flag_concepts
&& DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
12936 && !cand1
->template_decl
&& !cand2
->template_decl
12937 && cand_parms_match (cand1
, cand2
))
12939 winner
= more_constrained (cand1
->fn
, cand2
->fn
);
12944 /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
12945 rewritten candidates, and F2 is a synthesized candidate with reversed
12946 order of parameters and F1 is not. */
12947 if (cand1
->rewritten ())
12949 if (!cand2
->rewritten ())
12951 if (!cand1
->reversed () && cand2
->reversed ())
12953 if (cand1
->reversed () && !cand2
->reversed ())
12956 else if (cand2
->rewritten ())
12959 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
12960 if (deduction_guide_p (cand1
->fn
))
12962 gcc_assert (deduction_guide_p (cand2
->fn
));
12963 /* We distinguish between candidates from an explicit deduction guide and
12964 candidates built from a constructor based on DECL_ARTIFICIAL. */
12965 int art1
= DECL_ARTIFICIAL (cand1
->fn
);
12966 int art2
= DECL_ARTIFICIAL (cand2
->fn
);
12968 return art2
- art1
;
12972 /* Prefer the special copy guide over a declared copy/move
12974 if (copy_guide_p (cand1
->fn
))
12976 if (copy_guide_p (cand2
->fn
))
12979 /* Prefer a candidate generated from a non-template constructor. */
12980 int tg1
= template_guide_p (cand1
->fn
);
12981 int tg2
= template_guide_p (cand2
->fn
);
12987 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
12988 for all arguments the corresponding parameters of F1 and F2 have the same
12989 type (CWG 2273/2277). */
12990 if (DECL_P (cand1
->fn
) && DECL_CLASS_SCOPE_P (cand1
->fn
)
12991 && !DECL_CONV_FN_P (cand1
->fn
)
12992 && DECL_P (cand2
->fn
) && DECL_CLASS_SCOPE_P (cand2
->fn
)
12993 && !DECL_CONV_FN_P (cand2
->fn
))
12995 tree base1
= DECL_CONTEXT (strip_inheriting_ctors (cand1
->fn
));
12996 tree base2
= DECL_CONTEXT (strip_inheriting_ctors (cand2
->fn
));
12998 bool used1
= false;
12999 bool used2
= false;
13000 if (base1
== base2
)
13001 /* No difference. */;
13002 else if (DERIVED_FROM_P (base1
, base2
))
13004 else if (DERIVED_FROM_P (base2
, base1
))
13007 if (int diff
= used2
- used1
)
13009 for (i
= 0; i
< len
; ++i
)
13011 conversion
*t1
= cand1
->convs
[i
+ off1
];
13012 conversion
*t2
= cand2
->convs
[i
+ off2
];
13013 if (!same_type_p (t1
->type
, t2
->type
))
13021 /* Check whether we can discard a builtin candidate, either because we
13022 have two identical ones or matching builtin and non-builtin candidates.
13024 (Pedantically in the latter case the builtin which matched the user
13025 function should not be added to the overload set, but we spot it here.
13028 ... the builtin candidates include ...
13029 - do not have the same parameter type list as any non-template
13030 non-member candidate. */
13032 if (identifier_p (cand1
->fn
) || identifier_p (cand2
->fn
))
13034 for (i
= 0; i
< len
; ++i
)
13035 if (!same_type_p (cand1
->convs
[i
]->type
,
13036 cand2
->convs
[i
]->type
))
13038 if (i
== cand1
->num_convs
)
13040 if (cand1
->fn
== cand2
->fn
)
13041 /* Two built-in candidates; arbitrarily pick one. */
13043 else if (identifier_p (cand1
->fn
))
13044 /* cand1 is built-in; prefer cand2. */
13047 /* cand2 is built-in; prefer cand1. */
13052 /* For candidates of a multi-versioned function, make the version with
13053 the highest priority win. This version will be checked for dispatching
13054 first. If this version can be inlined into the caller, the front-end
13055 will simply make a direct call to this function. */
13057 if (TREE_CODE (cand1
->fn
) == FUNCTION_DECL
13058 && DECL_FUNCTION_VERSIONED (cand1
->fn
)
13059 && TREE_CODE (cand2
->fn
) == FUNCTION_DECL
13060 && DECL_FUNCTION_VERSIONED (cand2
->fn
))
13062 tree f1
= TREE_TYPE (cand1
->fn
);
13063 tree f2
= TREE_TYPE (cand2
->fn
);
13064 tree p1
= TYPE_ARG_TYPES (f1
);
13065 tree p2
= TYPE_ARG_TYPES (f2
);
13067 /* Check if cand1->fn and cand2->fn are versions of the same function. It
13068 is possible that cand1->fn and cand2->fn are function versions but of
13069 different functions. Check types to see if they are versions of the same
13071 if (compparms (p1
, p2
)
13072 && same_type_p (TREE_TYPE (f1
), TREE_TYPE (f2
)))
13074 /* Always make the version with the higher priority, more
13075 specialized, win. */
13076 gcc_assert (targetm
.compare_version_priority
);
13077 if (targetm
.compare_version_priority (cand1
->fn
, cand2
->fn
) >= 0)
13084 /* If the two function declarations represent the same function (this can
13085 happen with declarations in multiple scopes and arg-dependent lookup),
13086 arbitrarily choose one. But first make sure the default args we're
13088 if (DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
13089 && equal_functions (cand1
->fn
, cand2
->fn
))
13091 tree parms1
= TYPE_ARG_TYPES (TREE_TYPE (cand1
->fn
));
13092 tree parms2
= TYPE_ARG_TYPES (TREE_TYPE (cand2
->fn
));
13094 gcc_assert (!DECL_CONSTRUCTOR_P (cand1
->fn
));
13096 for (i
= 0; i
< len
; ++i
)
13098 /* Don't crash if the fn is variadic. */
13101 parms1
= TREE_CHAIN (parms1
);
13102 parms2
= TREE_CHAIN (parms2
);
13106 parms1
= TREE_CHAIN (parms1
);
13108 parms2
= TREE_CHAIN (parms2
);
13110 for (; parms1
; ++i
)
13112 if (!cp_tree_equal (TREE_PURPOSE (parms1
),
13113 TREE_PURPOSE (parms2
)))
13117 if (complain
& tf_error
)
13119 auto_diagnostic_group d
;
13120 if (permerror (input_location
,
13121 "default argument mismatch in "
13122 "overload resolution"))
13124 inform (DECL_SOURCE_LOCATION (cand1
->fn
),
13125 " candidate 1: %q#F", cand1
->fn
);
13126 inform (DECL_SOURCE_LOCATION (cand2
->fn
),
13127 " candidate 2: %q#F", cand2
->fn
);
13134 add_warning (cand1
, cand2
);
13137 parms1
= TREE_CHAIN (parms1
);
13138 parms2
= TREE_CHAIN (parms2
);
13146 /* Extension: If the worst conversion for one candidate is better than the
13147 worst conversion for the other, take the first. */
13148 if (!pedantic
&& (complain
& tf_warning_or_error
))
13150 conversion_rank rank1
= cr_identity
, rank2
= cr_identity
;
13151 struct z_candidate
*w
= 0, *l
= 0;
13153 for (i
= 0; i
< len
; ++i
)
13155 if (CONVERSION_RANK (cand1
->convs
[i
+off1
]) > rank1
)
13156 rank1
= CONVERSION_RANK (cand1
->convs
[i
+off1
]);
13157 if (CONVERSION_RANK (cand2
->convs
[i
+ off2
]) > rank2
)
13158 rank2
= CONVERSION_RANK (cand2
->convs
[i
+ off2
]);
13161 winner
= 1, w
= cand1
, l
= cand2
;
13163 winner
= -1, w
= cand2
, l
= cand1
;
13166 /* Don't choose a deleted function over ambiguity. */
13167 if (DECL_P (w
->fn
) && DECL_DELETED_FN (w
->fn
))
13171 auto_diagnostic_group d
;
13172 if (pedwarn (input_location
, 0,
13173 "ISO C++ says that these are ambiguous, even "
13174 "though the worst conversion for the first is "
13175 "better than the worst conversion for the second:"))
13177 print_z_candidate (input_location
, N_("candidate 1:"), w
);
13178 print_z_candidate (input_location
, N_("candidate 2:"), l
);
13182 add_warning (w
, l
);
13187 gcc_assert (!winner
);
13191 /* Given a list of candidates for overloading, find the best one, if any.
13192 This algorithm has a worst case of O(2n) (winner is last), and a best
13193 case of O(n/2) (totally ambiguous); much better than a sorting
13196 static struct z_candidate
*
13197 tourney (struct z_candidate
*candidates
, tsubst_flags_t complain
)
13199 struct z_candidate
*champ
= candidates
, *challenger
;
13201 struct z_candidate
*champ_compared_to_predecessor
= nullptr;
13203 /* Walk through the list once, comparing each current champ to the next
13204 candidate, knocking out a candidate or two with each comparison. */
13206 for (challenger
= champ
->next
; challenger
; )
13208 fate
= joust (champ
, challenger
, 0, complain
);
13210 challenger
= challenger
->next
;
13215 champ
= challenger
->next
;
13218 champ_compared_to_predecessor
= nullptr;
13222 champ_compared_to_predecessor
= champ
;
13223 champ
= challenger
;
13226 challenger
= champ
->next
;
13230 /* Make sure the champ is better than all the candidates it hasn't yet
13231 been compared to. */
13233 for (challenger
= candidates
;
13234 challenger
!= champ
13235 && challenger
!= champ_compared_to_predecessor
;
13236 challenger
= challenger
->next
)
13238 fate
= joust (champ
, challenger
, 0, complain
);
13246 /* Returns nonzero if things of type FROM can be converted to TO. */
13249 can_convert (tree to
, tree from
, tsubst_flags_t complain
)
13251 tree arg
= NULL_TREE
;
13252 /* implicit_conversion only considers user-defined conversions
13253 if it has an expression for the call argument list. */
13254 if (CLASS_TYPE_P (from
) || CLASS_TYPE_P (to
))
13255 arg
= build_stub_object (from
);
13256 return can_convert_arg (to
, from
, arg
, LOOKUP_IMPLICIT
, complain
);
13259 /* Returns nonzero if things of type FROM can be converted to TO with a
13260 standard conversion. */
13263 can_convert_standard (tree to
, tree from
, tsubst_flags_t complain
)
13265 return can_convert_arg (to
, from
, NULL_TREE
, LOOKUP_IMPLICIT
, complain
);
13268 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
13271 can_convert_arg (tree to
, tree from
, tree arg
, int flags
,
13272 tsubst_flags_t complain
)
13277 conversion_obstack_sentinel cos
;
13278 /* We want to discard any access checks done for this test,
13279 as we might not be in the appropriate access context and
13280 we'll do the check again when we actually perform the
13282 push_deferring_access_checks (dk_deferred
);
13284 t
= implicit_conversion (to
, from
, arg
, /*c_cast_p=*/false,
13286 ok_p
= (t
&& !t
->bad_p
);
13288 /* Discard the access checks now. */
13289 pop_deferring_access_checks ();
13294 /* Like can_convert_arg, but allows dubious conversions as well. */
13297 can_convert_arg_bad (tree to
, tree from
, tree arg
, int flags
,
13298 tsubst_flags_t complain
)
13302 conversion_obstack_sentinel cos
;
13303 /* Try to perform the conversion. */
13304 t
= implicit_conversion (to
, from
, arg
, /*c_cast_p=*/false,
13310 /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
13311 resolution FLAGS. */
13314 build_implicit_conv_flags (tree type
, tree expr
, int flags
)
13316 /* In a template, we are only concerned about determining the
13317 type of non-dependent expressions, so we do not have to
13318 perform the actual conversion. But for initializers, we
13319 need to be able to perform it at instantiation
13320 (or instantiate_non_dependent_expr) time. */
13321 expr
= build1 (IMPLICIT_CONV_EXPR
, type
, expr
);
13322 if (!(flags
& LOOKUP_ONLYCONVERTING
))
13323 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr
) = true;
13324 if (flags
& LOOKUP_NO_NARROWING
)
13325 IMPLICIT_CONV_EXPR_BRACED_INIT (expr
) = true;
13329 /* Convert EXPR to TYPE. Return the converted expression.
13331 Note that we allow bad conversions here because by the time we get to
13332 this point we are committed to doing the conversion. If we end up
13333 doing a bad conversion, convert_like will complain. */
13336 perform_implicit_conversion_flags (tree type
, tree expr
,
13337 tsubst_flags_t complain
, int flags
)
13340 location_t loc
= cp_expr_loc_or_input_loc (expr
);
13342 if (TYPE_REF_P (type
))
13343 expr
= mark_lvalue_use (expr
);
13345 expr
= mark_rvalue_use (expr
);
13347 if (error_operand_p (expr
))
13348 return error_mark_node
;
13350 conversion_obstack_sentinel cos
;
13352 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
13353 /*c_cast_p=*/false,
13358 if (complain
& tf_error
)
13359 implicit_conversion_error (loc
, type
, expr
);
13360 expr
= error_mark_node
;
13362 else if (processing_template_decl
&& conv
->kind
!= ck_identity
)
13363 expr
= build_implicit_conv_flags (type
, expr
, flags
);
13366 /* Give a conversion call the same location as expr. */
13367 iloc_sentinel
il (loc
);
13368 expr
= convert_like (conv
, expr
, complain
);
13375 perform_implicit_conversion (tree type
, tree expr
, tsubst_flags_t complain
)
13377 return perform_implicit_conversion_flags (type
, expr
, complain
,
13381 /* Convert EXPR to TYPE (as a direct-initialization) if that is
13382 permitted. If the conversion is valid, the converted expression is
13383 returned. Otherwise, NULL_TREE is returned, except in the case
13384 that TYPE is a class type; in that case, an error is issued. If
13385 C_CAST_P is true, then this direct-initialization is taking
13386 place as part of a static_cast being attempted as part of a C-style
13390 perform_direct_initialization_if_possible (tree type
,
13393 tsubst_flags_t complain
)
13397 if (type
== error_mark_node
|| error_operand_p (expr
))
13398 return error_mark_node
;
13401 If the destination type is a (possibly cv-qualified) class type:
13403 -- If the initialization is direct-initialization ...,
13404 constructors are considered.
13406 -- If overload resolution is successful, the selected constructor
13407 is called to initialize the object, with the initializer expression
13408 or expression-list as its argument(s).
13410 -- Otherwise, if no constructor is viable, the destination type is
13411 a (possibly cv-qualified) aggregate class A, and the initializer is
13412 a parenthesized expression-list, the object is initialized as
13414 if (CLASS_TYPE_P (type
))
13416 releasing_vec
args (make_tree_vector_single (expr
));
13417 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
13418 &args
, type
, LOOKUP_NORMAL
, complain
);
13419 return build_cplus_new (type
, expr
, complain
);
13422 conversion_obstack_sentinel cos
;
13424 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
13426 LOOKUP_NORMAL
, complain
);
13427 if (!conv
|| conv
->bad_p
)
13429 else if (processing_template_decl
&& conv
->kind
!= ck_identity
)
13431 /* In a template, we are only concerned about determining the
13432 type of non-dependent expressions, so we do not have to
13433 perform the actual conversion. But for initializers, we
13434 need to be able to perform it at instantiation
13435 (or instantiate_non_dependent_expr) time. */
13436 expr
= build1 (IMPLICIT_CONV_EXPR
, type
, expr
);
13437 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr
) = true;
13440 expr
= convert_like (conv
, expr
, NULL_TREE
, 0,
13441 /*issue_conversion_warnings=*/false,
13442 c_cast_p
, /*nested_p=*/false, complain
);
13447 /* When initializing a reference that lasts longer than a full-expression,
13448 this special rule applies:
13452 The temporary to which the reference is bound or the temporary
13453 that is the complete object to which the reference is bound
13454 persists for the lifetime of the reference.
13456 The temporaries created during the evaluation of the expression
13457 initializing the reference, except the temporary to which the
13458 reference is bound, are destroyed at the end of the
13459 full-expression in which they are created.
13461 In that case, we store the converted expression into a new
13462 VAR_DECL in a new scope.
13464 However, we want to be careful not to create temporaries when
13465 they are not required. For example, given:
13468 struct D : public B {};
13472 there is no need to copy the return value from "f"; we can just
13473 extend its lifetime. Similarly, given:
13476 struct T { operator S(); };
13480 we can extend the lifetime of the return value of the conversion
13483 The next several functions are involved in this lifetime extension. */
13485 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
13486 reference is being bound to a temporary. Create and return a new
13487 VAR_DECL with the indicated TYPE; this variable will store the value to
13488 which the reference is bound. */
13491 make_temporary_var_for_ref_to_temp (tree decl
, tree type
)
13493 tree var
= create_temporary_var (type
);
13495 /* Register the variable. */
13497 && (TREE_STATIC (decl
) || CP_DECL_THREAD_LOCAL_P (decl
)))
13499 /* Namespace-scope or local static; give it a mangled name. */
13501 /* If an initializer is visible to multiple translation units, those
13502 translation units must agree on the addresses of the
13503 temporaries. Therefore the temporaries must be given a consistent name
13504 and vague linkage. The mangled name of a temporary is the name of the
13505 non-temporary object in whose initializer they appear, prefixed with
13506 GR and suffixed with a sequence number mangled using the usual rules
13507 for a seq-id. Temporaries are numbered with a pre-order, depth-first,
13508 left-to-right walk of the complete initializer. */
13509 copy_linkage (var
, decl
);
13511 tree name
= mangle_ref_init_variable (decl
);
13512 DECL_NAME (var
) = name
;
13513 SET_DECL_ASSEMBLER_NAME (var
, name
);
13516 /* Create a new cleanup level if necessary. */
13517 maybe_push_cleanup_level (type
);
13519 return pushdecl (var
);
13522 /* EXPR is the initializer for a variable DECL of reference or
13523 std::initializer_list type. Create, push and return a new VAR_DECL
13524 for the initializer so that it will live as long as DECL. Any
13525 cleanup for the new variable is returned through CLEANUP, and the
13526 code to initialize the new variable is returned through INITP. */
13529 set_up_extended_ref_temp (tree decl
, tree expr
, vec
<tree
, va_gc
> **cleanups
,
13530 tree
*initp
, tree
*cond_guard
)
13536 /* Create the temporary variable. */
13537 type
= TREE_TYPE (expr
);
13538 var
= make_temporary_var_for_ref_to_temp (decl
, type
);
13539 layout_decl (var
, 0);
13540 /* If the rvalue is the result of a function call it will be
13541 a TARGET_EXPR. If it is some other construct (such as a
13542 member access expression where the underlying object is
13543 itself the result of a function call), turn it into a
13544 TARGET_EXPR here. It is important that EXPR be a
13545 TARGET_EXPR below since otherwise the INIT_EXPR will
13546 attempt to make a bitwise copy of EXPR to initialize
13548 if (TREE_CODE (expr
) != TARGET_EXPR
)
13549 expr
= get_target_expr (expr
);
13552 if (TREE_ADDRESSABLE (expr
))
13553 TREE_ADDRESSABLE (var
) = 1;
13554 if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr
)))
13555 DECL_MERGEABLE (var
) = true;
13558 if (TREE_CODE (decl
) == FIELD_DECL
13559 && extra_warnings
&& !warning_suppressed_p (decl
))
13561 warning (OPT_Wextra
, "a temporary bound to %qD only persists "
13562 "until the constructor exits", decl
);
13563 suppress_warning (decl
);
13566 /* Recursively extend temps in this initializer. */
13567 TARGET_EXPR_INITIAL (expr
)
13568 = extend_ref_init_temps (decl
, TARGET_EXPR_INITIAL (expr
), cleanups
,
13571 /* Any reference temp has a non-trivial initializer. */
13572 DECL_NONTRIVIALLY_INITIALIZED_P (var
) = true;
13574 /* If the initializer is constant, put it in DECL_INITIAL so we get
13575 static initialization and use in constant expressions. */
13576 init
= maybe_constant_init (expr
, var
, /*manifestly_const_eval=*/true);
13577 /* As in store_init_value. */
13578 init
= cp_fully_fold (init
);
13579 if (TREE_CONSTANT (init
))
13581 if (literal_type_p (type
) && CP_TYPE_CONST_NON_VOLATILE_P (type
))
13583 /* 5.19 says that a constant expression can include an
13584 lvalue-rvalue conversion applied to "a glvalue of literal type
13585 that refers to a non-volatile temporary object initialized
13586 with a constant expression". Rather than try to communicate
13587 that this VAR_DECL is a temporary, just mark it constexpr. */
13588 DECL_DECLARED_CONSTEXPR_P (var
) = true;
13589 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var
) = true;
13590 TREE_CONSTANT (var
) = true;
13591 TREE_READONLY (var
) = true;
13593 DECL_INITIAL (var
) = init
;
13597 /* Create the INIT_EXPR that will initialize the temporary
13599 init
= split_nonconstant_init (var
, expr
);
13600 if (at_function_scope_p ())
13602 add_decl_expr (var
);
13604 if (TREE_STATIC (var
))
13605 init
= add_stmt_to_compound (init
, register_dtor_fn (var
));
13608 tree cleanup
= cxx_maybe_build_cleanup (var
, tf_warning_or_error
);
13611 if (cond_guard
&& cleanup
!= error_mark_node
)
13613 if (*cond_guard
== NULL_TREE
)
13615 *cond_guard
= build_local_temp (boolean_type_node
);
13616 add_decl_expr (*cond_guard
);
13617 tree set
= cp_build_modify_expr (UNKNOWN_LOCATION
,
13618 *cond_guard
, NOP_EXPR
,
13619 boolean_false_node
,
13620 tf_warning_or_error
);
13621 finish_expr_stmt (set
);
13623 cleanup
= build3 (COND_EXPR
, void_type_node
,
13624 *cond_guard
, cleanup
, NULL_TREE
);
13626 vec_safe_push (*cleanups
, cleanup
);
13630 /* We must be careful to destroy the temporary only
13631 after its initialization has taken place. If the
13632 initialization throws an exception, then the
13633 destructor should not be run. We cannot simply
13634 transform INIT into something like:
13636 (INIT, ({ CLEANUP_STMT; }))
13638 because emit_local_var always treats the
13639 initializer as a full-expression. Thus, the
13640 destructor would run too early; it would run at the
13641 end of initializing the reference variable, rather
13642 than at the end of the block enclosing the
13643 reference variable.
13645 The solution is to pass back a cleanup expression
13646 which the caller is responsible for attaching to
13647 the statement tree. */
13651 rest_of_decl_compilation (var
, /*toplev=*/1, at_eof
);
13652 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
13654 if (CP_DECL_THREAD_LOCAL_P (var
))
13655 tls_aggregates
= tree_cons (NULL_TREE
, var
,
13658 static_aggregates
= tree_cons (NULL_TREE
, var
,
13659 static_aggregates
);
13662 /* Check whether the dtor is callable. */
13663 cxx_maybe_build_cleanup (var
, tf_warning_or_error
);
13665 /* Avoid -Wunused-variable warning (c++/38958). */
13666 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
13668 TREE_USED (decl
) = DECL_READ_P (decl
) = true;
13674 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
13675 initializing a variable of that TYPE. */
13678 initialize_reference (tree type
, tree expr
,
13679 int flags
, tsubst_flags_t complain
)
13682 location_t loc
= cp_expr_loc_or_input_loc (expr
);
13684 if (type
== error_mark_node
|| error_operand_p (expr
))
13685 return error_mark_node
;
13687 conversion_obstack_sentinel cos
;
13689 conv
= reference_binding (type
, TREE_TYPE (expr
), expr
, /*c_cast_p=*/false,
13691 /* If this conversion failed, we're in C++20, and we have something like
13692 A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
13693 if ((!conv
|| conv
->bad_p
)
13694 && (flags
& LOOKUP_AGGREGATE_PAREN_INIT
))
13696 tree e
= build_constructor_single (init_list_type_node
, NULL_TREE
, expr
);
13697 CONSTRUCTOR_IS_DIRECT_INIT (e
) = true;
13698 CONSTRUCTOR_IS_PAREN_INIT (e
) = true;
13699 conversion
*c
= reference_binding (type
, TREE_TYPE (e
), e
,
13700 /*c_cast_p=*/false, flags
, complain
);
13701 /* If this worked, use it. */
13702 if (c
&& !c
->bad_p
)
13703 expr
= e
, conv
= c
;
13705 if (!conv
|| conv
->bad_p
)
13707 if (complain
& tf_error
)
13710 convert_like (conv
, expr
, complain
);
13711 else if (!CP_TYPE_CONST_P (TREE_TYPE (type
))
13712 && !TYPE_REF_IS_RVALUE (type
)
13713 && !lvalue_p (expr
))
13714 error_at (loc
, "invalid initialization of non-const reference of "
13715 "type %qH from an rvalue of type %qI",
13716 type
, TREE_TYPE (expr
));
13718 error_at (loc
, "invalid initialization of reference of type "
13719 "%qH from expression of type %qI", type
,
13722 return error_mark_node
;
13725 if (conv
->kind
== ck_ref_bind
)
13726 /* Perform the conversion. */
13727 expr
= convert_like (conv
, expr
, complain
);
13728 else if (conv
->kind
== ck_ambig
)
13729 /* We gave an error in build_user_type_conversion_1. */
13730 expr
= error_mark_node
;
13732 gcc_unreachable ();
13737 /* Return true if T is std::pair<const T&, const T&>. */
13740 std_pair_ref_ref_p (tree t
)
13742 /* First, check if we have std::pair. */
13743 if (!NON_UNION_CLASS_TYPE_P (t
)
13744 || !CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
13746 tree tdecl
= TYPE_NAME (TYPE_MAIN_VARIANT (t
));
13747 if (!decl_in_std_namespace_p (tdecl
))
13749 tree name
= DECL_NAME (tdecl
);
13750 if (!name
|| !id_equal (name
, "pair"))
13753 /* Now see if the template arguments are both const T&. */
13754 tree args
= CLASSTYPE_TI_ARGS (t
);
13755 if (TREE_VEC_LENGTH (args
) != 2)
13757 for (int i
= 0; i
< 2; i
++)
13758 if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args
, i
))
13759 || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args
, i
))))
13765 /* Return true if a class CTYPE is either std::reference_wrapper or
13766 std::ref_view, or a reference wrapper class. We consider a class
13767 a reference wrapper class if it has a reference member. We no
13768 longer check that it has a constructor taking the same reference type
13769 since that approach still generated too many false positives. */
13772 class_has_reference_member_p (tree t
)
13774 for (tree fields
= TYPE_FIELDS (t
);
13776 fields
= DECL_CHAIN (fields
))
13777 if (TREE_CODE (fields
) == FIELD_DECL
13778 && !DECL_ARTIFICIAL (fields
)
13779 && TYPE_REF_P (TREE_TYPE (fields
)))
13784 /* A wrapper for the above suitable as a callback for dfs_walk_once. */
13787 class_has_reference_member_p_r (tree binfo
, void *)
13789 return (class_has_reference_member_p (BINFO_TYPE (binfo
))
13790 ? integer_one_node
: NULL_TREE
);
13794 reference_like_class_p (tree ctype
)
13796 if (!CLASS_TYPE_P (ctype
))
13799 /* Also accept a std::pair<const T&, const T&>. */
13800 if (std_pair_ref_ref_p (ctype
))
13803 tree tdecl
= TYPE_NAME (TYPE_MAIN_VARIANT (ctype
));
13804 if (decl_in_std_namespace_p (tdecl
))
13806 tree name
= DECL_NAME (tdecl
);
13808 && (id_equal (name
, "reference_wrapper")
13809 || id_equal (name
, "span")
13810 || id_equal (name
, "ref_view")))
13814 /* Some classes, such as std::tuple, have the reference member in its
13815 (non-direct) base class. */
13816 if (dfs_walk_once (TYPE_BINFO (ctype
), class_has_reference_member_p_r
,
13823 /* Helper for maybe_warn_dangling_reference to find a problematic CALL_EXPR
13824 that initializes the LHS (and at least one of its arguments represents
13825 a temporary, as outlined in maybe_warn_dangling_reference), or NULL_TREE
13826 if none found. For instance:
13828 const S& s = S().self(); // S::self (&TARGET_EXPR <...>)
13829 const int& r = (42, f(1)); // f(1)
13830 const int& t = b ? f(1) : f(2); // f(1)
13831 const int& u = b ? f(1) : f(g); // f(1)
13832 const int& v = b ? f(g) : f(2); // f(2)
13833 const int& w = b ? f(g) : f(g); // NULL_TREE
13834 const int& y = (f(1), 42); // NULL_TREE
13835 const int& z = f(f(1)); // f(f(1))
13837 EXPR is the initializer. If ARG_P is true, we're processing an argument
13838 to a function; the point is to distinguish between, for example,
13840 Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>)
13842 where we shouldn't warn, and
13844 Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>)
13846 where we should warn (Ref is a reference_like_class_p so we see through
13850 do_warn_dangling_reference (tree expr
, bool arg_p
)
13854 if (arg_p
&& expr_represents_temporary_p (expr
))
13856 /* An attempt to reduce the number of -Wdangling-reference
13857 false positives concerning reference wrappers (c++/107532).
13858 When we encounter a reference_like_class_p, we don't warn
13859 just yet; instead, we keep recursing to see if there were
13860 any temporaries behind the reference-wrapper class. */
13862 while (handled_component_p (e
))
13863 e
= TREE_OPERAND (e
, 0);
13864 if (!reference_like_class_p (TREE_TYPE (e
)))
13868 switch (TREE_CODE (expr
))
13872 tree fndecl
= cp_get_callee_fndecl_nofold (expr
);
13874 || warning_suppressed_p (fndecl
, OPT_Wdangling_reference
)
13875 || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl
),
13876 OPT_Wdangling_reference
)
13877 /* Don't emit a false positive for:
13878 std::vector<int> v = ...;
13879 std::vector<int>::const_iterator it = v.begin();
13880 const int &r = *it++;
13881 because R refers to one of the int elements of V, not to
13882 a temporary object. Member operator* may return a reference
13883 but probably not to one of its arguments. */
13884 || (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl
)
13885 && DECL_OVERLOADED_OPERATOR_P (fndecl
)
13886 && DECL_OVERLOADED_OPERATOR_IS (fndecl
, INDIRECT_REF
)))
13889 tree rettype
= TREE_TYPE (TREE_TYPE (fndecl
));
13890 /* If the function doesn't return a reference, don't warn. This
13892 const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
13893 which doesn't dangle: std::min here returns an int.
13895 If the function returns a std::pair<const T&, const T&>, we
13896 warn, to detect e.g.
13897 std::pair<const int&, const int&> v = std::minmax(1, 2);
13898 which also creates a dangling reference, because std::minmax
13899 returns std::pair<const T&, const T&>(b, a). */
13900 if (!(TYPE_REF_OBJ_P (rettype
) || reference_like_class_p (rettype
)))
13903 /* Here we're looking to see if any of the arguments is a temporary
13904 initializing a reference parameter. */
13905 for (int i
= 0; i
< call_expr_nargs (expr
); ++i
)
13907 tree arg
= CALL_EXPR_ARG (expr
, i
);
13908 /* Check that this argument initializes a reference, except for
13909 the argument initializing the object of a member function. */
13910 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl
)
13911 && !TYPE_REF_P (TREE_TYPE (arg
)))
13914 if (TREE_CODE (arg
) == ADDR_EXPR
)
13915 arg
= TREE_OPERAND (arg
, 0);
13916 /* Recurse to see if the argument is a temporary. It could also
13917 be another call taking a temporary and returning it and
13918 initializing this reference parameter. */
13919 if (do_warn_dangling_reference (arg
, /*arg_p=*/true))
13921 /* Don't warn about member function like:
13923 S& s = a.emplace<S>({0}, 0);
13924 which constructs a new object and returns a reference to it, but
13925 we still want to detect:
13926 struct S { const S& self () { return *this; } };
13927 const S& s = S().self();
13928 where 's' dangles. If we've gotten here, the object this function
13929 is invoked on is not a temporary. */
13930 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl
))
13935 case COMPOUND_EXPR
:
13936 return do_warn_dangling_reference (TREE_OPERAND (expr
, 1), arg_p
);
13938 if (tree t
= do_warn_dangling_reference (TREE_OPERAND (expr
, 1), arg_p
))
13940 return do_warn_dangling_reference (TREE_OPERAND (expr
, 2), arg_p
);
13942 return do_warn_dangling_reference (TREE_OPERAND (expr
, 0), arg_p
);
13944 return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr
), arg_p
);
13950 /* Implement -Wdangling-reference, to detect cases like
13953 const int& r = std::max(n - 1, n + 1); // r is dangling
13955 This creates temporaries from the arguments, returns a reference to
13956 one of the temporaries, but both temporaries are destroyed at the end
13957 of the full expression.
13959 This works by checking if a reference is initialized with a function
13960 that returns a reference, and at least one parameter of the function
13961 is a reference that is bound to a temporary. It assumes that such a
13962 function actually returns one of its arguments.
13964 DECL is the reference being initialized, INIT is the initializer. */
13967 maybe_warn_dangling_reference (const_tree decl
, tree init
)
13969 if (!warn_dangling_reference
)
13971 tree type
= TREE_TYPE (decl
);
13972 /* Only warn if what we're initializing has type T&& or const T&, or
13973 std::pair<const T&, const T&>. (A non-const lvalue reference can't
13974 bind to a temporary.) */
13975 if (!((TYPE_REF_OBJ_P (type
)
13976 && (TYPE_REF_IS_RVALUE (type
)
13977 || CP_TYPE_CONST_P (TREE_TYPE (type
))))
13978 || std_pair_ref_ref_p (type
)))
13980 /* Don't suppress the diagnostic just because the call comes from
13981 a system header. If the DECL is not in a system header, or if
13982 -Wsystem-headers was provided, warn. */
13984 = make_temp_override (global_dc
->dc_warn_system_headers
,
13985 (!in_system_header_at (DECL_SOURCE_LOCATION (decl
))
13986 || global_dc
->dc_warn_system_headers
));
13987 if (tree call
= do_warn_dangling_reference (init
, /*arg_p=*/false))
13989 auto_diagnostic_group d
;
13990 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wdangling_reference
,
13991 "possibly dangling reference to a temporary"))
13992 inform (EXPR_LOCATION (call
), "the temporary was destroyed at "
13993 "the end of the full expression %qE", call
);
13997 /* If *P is an xvalue expression, prevent temporary lifetime extension if it
13998 gets used to initialize a reference. */
14001 prevent_lifetime_extension (tree t
)
14004 while (TREE_CODE (*p
) == COMPOUND_EXPR
)
14005 p
= &TREE_OPERAND (*p
, 1);
14006 while (handled_component_p (*p
))
14007 p
= &TREE_OPERAND (*p
, 0);
14008 /* Change a TARGET_EXPR from prvalue to xvalue. */
14009 if (TREE_CODE (*p
) == TARGET_EXPR
)
14010 *p
= build2 (COMPOUND_EXPR
, TREE_TYPE (*p
), *p
,
14011 move (TARGET_EXPR_SLOT (*p
)));
14015 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
14016 which is bound either to a reference or a std::initializer_list. */
14019 extend_ref_init_temps_1 (tree decl
, tree init
, vec
<tree
, va_gc
> **cleanups
,
14022 /* CWG1299 (C++20): The temporary object to which the reference is bound or
14023 the temporary object that is the complete object of a subobject to which
14024 the reference is bound persists for the lifetime of the reference if the
14025 glvalue to which the reference is bound was obtained through one of the
14027 - a temporary materialization conversion ([conv.rval]),
14028 - ( expression ), where expression is one of these expressions,
14029 - subscripting ([expr.sub]) of an array operand, where that operand is one
14030 of these expressions,
14031 - a class member access ([expr.ref]) using the . operator where the left
14032 operand is one of these expressions and the right operand designates a
14033 non-static data member of non-reference type,
14034 - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator
14035 where the left operand is one of these expressions and the right operand
14036 is a pointer to data member of non-reference type,
14037 - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]),
14038 dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast
14039 ([expr.reinterpret.cast]) converting, without a user-defined conversion,
14040 a glvalue operand that is one of these expressions to a glvalue that
14041 refers to the object designated by the operand, or to its complete
14042 object or a subobject thereof,
14043 - a conditional expression ([expr.cond]) that is a glvalue where the
14044 second or third operand is one of these expressions, or
14045 - a comma expression ([expr.comma]) that is a glvalue where the right
14046 operand is one of these expressions. */
14048 /* FIXME several cases are still handled wrong (101572, 81420). */
14053 if (TREE_CODE (sub
) == COMPOUND_EXPR
)
14055 TREE_OPERAND (sub
, 1)
14056 = extend_ref_init_temps_1 (decl
, TREE_OPERAND (sub
, 1), cleanups
,
14060 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
14061 && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions
14062 (TREE_OPERAND (sub
, 1)))))
14064 /* A pointer-to-member operation. */
14065 TREE_OPERAND (sub
, 0)
14066 = extend_ref_init_temps_1 (decl
, TREE_OPERAND (sub
, 0), cleanups
,
14070 if (TREE_CODE (sub
) == COND_EXPR
)
14072 tree cur_cond_guard
= NULL_TREE
;
14073 if (TREE_OPERAND (sub
, 1))
14074 TREE_OPERAND (sub
, 1)
14075 = extend_ref_init_temps_1 (decl
, TREE_OPERAND (sub
, 1), cleanups
,
14077 if (cur_cond_guard
)
14079 tree set
= cp_build_modify_expr (UNKNOWN_LOCATION
, cur_cond_guard
,
14080 NOP_EXPR
, boolean_true_node
,
14081 tf_warning_or_error
);
14082 TREE_OPERAND (sub
, 1)
14083 = cp_build_compound_expr (set
, TREE_OPERAND (sub
, 1),
14084 tf_warning_or_error
);
14086 cur_cond_guard
= NULL_TREE
;
14087 if (TREE_OPERAND (sub
, 2))
14088 TREE_OPERAND (sub
, 2)
14089 = extend_ref_init_temps_1 (decl
, TREE_OPERAND (sub
, 2), cleanups
,
14091 if (cur_cond_guard
)
14093 tree set
= cp_build_modify_expr (UNKNOWN_LOCATION
, cur_cond_guard
,
14094 NOP_EXPR
, boolean_true_node
,
14095 tf_warning_or_error
);
14096 TREE_OPERAND (sub
, 2)
14097 = cp_build_compound_expr (set
, TREE_OPERAND (sub
, 2),
14098 tf_warning_or_error
);
14102 if (TREE_CODE (sub
) != ADDR_EXPR
)
14104 /* Deal with binding to a subobject. */
14105 for (p
= &TREE_OPERAND (sub
, 0);
14106 TREE_CODE (*p
) == COMPONENT_REF
|| TREE_CODE (*p
) == ARRAY_REF
; )
14107 p
= &TREE_OPERAND (*p
, 0);
14108 if (TREE_CODE (*p
) == TARGET_EXPR
)
14110 tree subinit
= NULL_TREE
;
14111 *p
= set_up_extended_ref_temp (decl
, *p
, cleanups
, &subinit
, cond_guard
);
14112 recompute_tree_invariant_for_addr_expr (sub
);
14114 init
= fold_convert (TREE_TYPE (init
), sub
);
14116 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), subinit
, init
);
14121 /* INIT is part of the initializer for DECL. If there are any
14122 reference or initializer lists being initialized, extend their
14123 lifetime to match that of DECL. */
14126 extend_ref_init_temps (tree decl
, tree init
, vec
<tree
, va_gc
> **cleanups
,
14129 tree type
= TREE_TYPE (init
);
14130 if (processing_template_decl
)
14133 maybe_warn_dangling_reference (decl
, init
);
14135 if (TYPE_REF_P (type
))
14136 init
= extend_ref_init_temps_1 (decl
, init
, cleanups
, cond_guard
);
14140 if (TREE_CODE (ctor
) == TARGET_EXPR
)
14141 ctor
= TARGET_EXPR_INITIAL (ctor
);
14142 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
14144 /* [dcl.init] When initializing an aggregate from a parenthesized list
14145 of values... a temporary object bound to a reference does not have
14146 its lifetime extended. */
14147 if (CONSTRUCTOR_IS_PAREN_INIT (ctor
))
14150 if (is_std_init_list (type
))
14152 /* The temporary array underlying a std::initializer_list
14153 is handled like a reference temporary. */
14154 tree array
= CONSTRUCTOR_ELT (ctor
, 0)->value
;
14155 array
= extend_ref_init_temps_1 (decl
, array
, cleanups
,
14157 CONSTRUCTOR_ELT (ctor
, 0)->value
= array
;
14162 constructor_elt
*p
;
14163 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
14164 FOR_EACH_VEC_SAFE_ELT (elts
, i
, p
)
14165 p
->value
= extend_ref_init_temps (decl
, p
->value
, cleanups
,
14168 recompute_constructor_flags (ctor
);
14169 if (decl_maybe_constant_var_p (decl
) && TREE_CONSTANT (ctor
))
14170 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
14177 /* Returns true iff an initializer for TYPE could contain temporaries that
14178 need to be extended because they are bound to references or
14179 std::initializer_list. */
14182 type_has_extended_temps (tree type
)
14184 type
= strip_array_types (type
);
14185 if (TYPE_REF_P (type
))
14187 if (CLASS_TYPE_P (type
))
14189 if (is_std_init_list (type
))
14191 for (tree f
= next_aggregate_field (TYPE_FIELDS (type
));
14192 f
; f
= next_aggregate_field (DECL_CHAIN (f
)))
14193 if (type_has_extended_temps (TREE_TYPE (f
)))
14199 /* Returns true iff TYPE is some variant of std::initializer_list. */
14202 is_std_init_list (tree type
)
14204 if (!TYPE_P (type
))
14206 if (cxx_dialect
== cxx98
)
14208 /* Look through typedefs. */
14209 type
= TYPE_MAIN_VARIANT (type
);
14210 return (CLASS_TYPE_P (type
)
14211 && CP_TYPE_CONTEXT (type
) == std_node
14212 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
)));
14215 /* Returns true iff DECL is a list constructor: i.e. a constructor which
14216 will accept an argument list of a single std::initializer_list<T>. */
14219 is_list_ctor (tree decl
)
14221 tree args
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
14224 if (!args
|| args
== void_list_node
)
14227 arg
= non_reference (TREE_VALUE (args
));
14228 if (!is_std_init_list (arg
))
14231 args
= TREE_CHAIN (args
);
14233 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
14234 /* There are more non-defaulted parms. */
14240 /* We know that can_convert_arg_bad already said "no" when trying to convert
14241 FROM to TO with ARG and FLAGS. Try to figure out if it was because
14242 an explicit conversion function was skipped when looking for a way to
14243 perform the conversion. At this point we've already printed an error. */
14246 maybe_show_nonconverting_candidate (tree to
, tree from
, tree arg
, int flags
)
14248 if (!(flags
& LOOKUP_ONLYCONVERTING
))
14251 conversion_obstack_sentinel cos
;
14252 conversion
*c
= implicit_conversion (to
, from
, arg
, /*c_cast_p=*/false,
14253 flags
& ~LOOKUP_ONLYCONVERTING
, tf_none
);
14254 if (c
&& !c
->bad_p
&& c
->user_conv_p
)
14255 /* Ay, the conversion would have worked in direct-init context. */
14256 for (; c
; c
= next_conversion (c
))
14257 if (c
->kind
== ck_user
14258 && DECL_P (c
->cand
->fn
)
14259 && DECL_NONCONVERTING_P (c
->cand
->fn
))
14260 inform (DECL_SOURCE_LOCATION (c
->cand
->fn
), "explicit conversion "
14261 "function was not considered");
14264 #include "gt-cp-call.h"