1 /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 Copyright (C) 1987-2020 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"
44 #include "gcc-rich-location.h"
46 /* The various kinds of conversion. */
48 enum conversion_kind
{
65 /* The rank of the conversion. Order of the enumerals matters; better
66 conversions should come earlier in the list. */
68 enum conversion_rank
{
79 /* An implicit conversion sequence, in the sense of [over.best.ics].
80 The first conversion to be performed is at the end of the chain.
81 That conversion is always a cr_identity conversion. */
84 /* The kind of conversion represented by this step. */
86 /* The rank of this conversion. */
88 BOOL_BITFIELD user_conv_p
: 1;
89 BOOL_BITFIELD ellipsis_p
: 1;
90 BOOL_BITFIELD this_p
: 1;
91 /* True if this conversion would be permitted with a bending of
92 language standards, e.g. disregarding pointer qualifiers or
93 converting integers to pointers. */
94 BOOL_BITFIELD bad_p
: 1;
95 /* If KIND is ck_ref_bind or ck_base, true to indicate that a
96 temporary should be created to hold the result of the
97 conversion. If KIND is ck_ambig or ck_user, true means force
98 copy-initialization. */
99 BOOL_BITFIELD need_temporary_p
: 1;
100 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
101 from a pointer-to-derived to pointer-to-base is being performed. */
102 BOOL_BITFIELD base_p
: 1;
103 /* If KIND is ck_ref_bind, true when either an lvalue reference is
104 being bound to an lvalue expression or an rvalue reference is
105 being bound to an rvalue expression. If KIND is ck_rvalue or ck_base,
106 true when we are treating an lvalue as an rvalue (12.8p33). If
107 ck_identity, we will be binding a reference directly or decaying to
109 BOOL_BITFIELD rvaluedness_matches_p
: 1;
110 BOOL_BITFIELD check_narrowing
: 1;
111 /* Whether check_narrowing should only check TREE_CONSTANTs; used
112 in build_converted_constant_expr. */
113 BOOL_BITFIELD check_narrowing_const_only
: 1;
114 /* True if this conversion is taking place in a copy-initialization context
115 and we should only consider converting constructors. Only set in
116 ck_base and ck_rvalue. */
117 BOOL_BITFIELD copy_init_p
: 1;
118 /* The type of the expression resulting from the conversion. */
121 /* The next conversion in the chain. Since the conversions are
122 arranged from outermost to innermost, the NEXT conversion will
123 actually be performed before this conversion. This variant is
124 used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor
125 ck_list. Please use the next_conversion function instead
126 of using this field directly. */
128 /* The expression at the beginning of the conversion chain. This
129 variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig.
130 You can use conv_get_original_expr to get this expression. */
132 /* The array of conversions for an initializer_list, so this
133 variant is used only when KIN D is ck_list. */
136 /* The function candidate corresponding to this conversion
137 sequence. This field is only used if KIND is ck_user. */
138 struct z_candidate
*cand
;
141 #define CONVERSION_RANK(NODE) \
142 ((NODE)->bad_p ? cr_bad \
143 : (NODE)->ellipsis_p ? cr_ellipsis \
144 : (NODE)->user_conv_p ? cr_user \
147 #define BAD_CONVERSION_RANK(NODE) \
148 ((NODE)->ellipsis_p ? cr_ellipsis \
149 : (NODE)->user_conv_p ? cr_user \
152 static struct obstack conversion_obstack
;
153 static bool conversion_obstack_initialized
;
154 struct rejection_reason
;
156 static struct z_candidate
* tourney (struct z_candidate
*, tsubst_flags_t
);
157 static int equal_functions (tree
, tree
);
158 static int joust (struct z_candidate
*, struct z_candidate
*, bool,
160 static int compare_ics (conversion
*, conversion
*);
161 static void maybe_warn_class_memaccess (location_t
, tree
,
162 const vec
<tree
, va_gc
> *);
163 static tree
build_over_call (struct z_candidate
*, int, tsubst_flags_t
);
164 static tree
convert_like (conversion
*, tree
, tsubst_flags_t
);
165 static tree
convert_like_with_context (conversion
*, tree
, tree
, int,
167 static void op_error (const op_location_t
&, enum tree_code
, enum tree_code
,
168 tree
, tree
, tree
, bool);
169 static struct z_candidate
*build_user_type_conversion_1 (tree
, tree
, int,
171 static void print_z_candidate (location_t
, const char *, struct z_candidate
*);
172 static void print_z_candidates (location_t
, struct z_candidate
*);
173 static tree
build_this (tree
);
174 static struct z_candidate
*splice_viable (struct z_candidate
*, bool, bool *);
175 static bool any_strictly_viable (struct z_candidate
*);
176 static struct z_candidate
*add_template_candidate
177 (struct z_candidate
**, tree
, tree
, tree
, tree
, const vec
<tree
, va_gc
> *,
178 tree
, tree
, tree
, int, unification_kind_t
, tsubst_flags_t
);
179 static struct z_candidate
*add_template_candidate_real
180 (struct z_candidate
**, tree
, tree
, tree
, tree
, const vec
<tree
, va_gc
> *,
181 tree
, tree
, tree
, int, tree
, unification_kind_t
, tsubst_flags_t
);
182 static bool is_complete (tree
);
183 static struct z_candidate
*add_conv_candidate
184 (struct z_candidate
**, tree
, tree
, const vec
<tree
, va_gc
> *, tree
,
185 tree
, tsubst_flags_t
);
186 static struct z_candidate
*add_function_candidate
187 (struct z_candidate
**, tree
, tree
, tree
, const vec
<tree
, va_gc
> *, tree
,
188 tree
, int, conversion
**, tsubst_flags_t
);
189 static conversion
*implicit_conversion (tree
, tree
, tree
, bool, int,
191 static conversion
*reference_binding (tree
, tree
, tree
, bool, int,
193 static conversion
*build_conv (conversion_kind
, tree
, conversion
*);
194 static conversion
*build_list_conv (tree
, tree
, int, tsubst_flags_t
);
195 static conversion
*next_conversion (conversion
*);
196 static bool is_subseq (conversion
*, conversion
*);
197 static conversion
*maybe_handle_ref_bind (conversion
**);
198 static void maybe_handle_implicit_object (conversion
**);
199 static struct z_candidate
*add_candidate
200 (struct z_candidate
**, tree
, tree
, const vec
<tree
, va_gc
> *, size_t,
201 conversion
**, tree
, tree
, int, struct rejection_reason
*, int);
202 static tree
source_type (conversion
*);
203 static void add_warning (struct z_candidate
*, struct z_candidate
*);
204 static conversion
*direct_reference_binding (tree
, conversion
*);
205 static bool promoted_arithmetic_type_p (tree
);
206 static conversion
*conditional_conversion (tree
, tree
, tsubst_flags_t
);
207 static char *name_as_c_string (tree
, tree
, bool *);
208 static tree
prep_operand (tree
);
209 static void add_candidates (tree
, tree
, const vec
<tree
, va_gc
> *, tree
, tree
,
210 bool, tree
, tree
, int, struct z_candidate
**,
212 static conversion
*merge_conversion_sequences (conversion
*, conversion
*);
213 static tree
build_temp (tree
, tree
, int, diagnostic_t
*, tsubst_flags_t
);
214 static conversion
*build_identity_conv (tree
, tree
);
215 static inline bool conv_binds_to_array_of_unknown_bound (conversion
*);
216 static tree
prevent_lifetime_extension (tree
);
218 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
219 NAME can take many forms... */
222 check_dtor_name (tree basetype
, tree name
)
224 /* Just accept something we've already complained about. */
225 if (name
== error_mark_node
)
228 if (TREE_CODE (name
) == TYPE_DECL
)
229 name
= TREE_TYPE (name
);
230 else if (TYPE_P (name
))
232 else if (identifier_p (name
))
234 if ((MAYBE_CLASS_TYPE_P (basetype
)
235 || TREE_CODE (basetype
) == ENUMERAL_TYPE
)
236 && name
== constructor_name (basetype
))
239 name
= get_type_value (name
);
245 template <class T> struct S { ~S(); };
249 NAME will be a class template. */
250 gcc_assert (DECL_CLASS_TEMPLATE_P (name
));
254 if (!name
|| name
== error_mark_node
)
256 return same_type_p (TYPE_MAIN_VARIANT (basetype
), TYPE_MAIN_VARIANT (name
));
259 /* We want the address of a function or method. We avoid creating a
260 pointer-to-member function. */
263 build_addr_func (tree function
, tsubst_flags_t complain
)
265 tree type
= TREE_TYPE (function
);
267 /* We have to do these by hand to avoid real pointer to member
269 if (TREE_CODE (type
) == METHOD_TYPE
)
271 if (TREE_CODE (function
) == OFFSET_REF
)
273 tree object
= build_address (TREE_OPERAND (function
, 0));
274 return get_member_function_from_ptrfunc (&object
,
275 TREE_OPERAND (function
, 1),
278 function
= build_address (function
);
280 else if (TREE_CODE (function
) == FUNCTION_DECL
281 && DECL_IMMEDIATE_FUNCTION_P (function
))
282 function
= build_address (function
);
284 function
= decay_conversion (function
, complain
, /*reject_builtin=*/false);
289 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
290 POINTER_TYPE to those. Note, pointer to member function types
291 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
292 two variants. build_call_a is the primitive taking an array of
293 arguments, while build_call_n is a wrapper that handles varargs. */
296 build_call_n (tree function
, int n
, ...)
299 return build_call_a (function
, 0, NULL
);
302 tree
*argarray
= XALLOCAVEC (tree
, n
);
307 for (i
= 0; i
< n
; i
++)
308 argarray
[i
] = va_arg (ap
, tree
);
310 return build_call_a (function
, n
, argarray
);
314 /* Update various flags in cfun and the call itself based on what is being
315 called. Split out of build_call_a so that bot_manip can use it too. */
318 set_flags_from_callee (tree call
)
320 /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
321 tree decl
= cp_get_callee_fndecl_nofold (call
);
323 /* We check both the decl and the type; a function may be known not to
324 throw without being declared throw(). */
325 bool nothrow
= decl
&& TREE_NOTHROW (decl
);
326 tree callee
= cp_get_callee (call
);
328 nothrow
|= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee
)));
329 else if (TREE_CODE (call
) == CALL_EXPR
330 && internal_fn_flags (CALL_EXPR_IFN (call
)) & ECF_NOTHROW
)
333 if (cfun
&& cp_function_chain
&& !cp_unevaluated_operand
)
335 if (!nothrow
&& at_function_scope_p ())
336 cp_function_chain
->can_throw
= 1;
338 if (decl
&& TREE_THIS_VOLATILE (decl
))
339 current_function_returns_abnormally
= 1;
342 TREE_NOTHROW (call
) = nothrow
;
346 build_call_a (tree function
, int n
, tree
*argarray
)
353 function
= build_addr_func (function
, tf_warning_or_error
);
355 gcc_assert (TYPE_PTR_P (TREE_TYPE (function
)));
356 fntype
= TREE_TYPE (TREE_TYPE (function
));
357 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype
));
358 result_type
= TREE_TYPE (fntype
);
359 /* An rvalue has no cv-qualifiers. */
360 if (SCALAR_TYPE_P (result_type
) || VOID_TYPE_P (result_type
))
361 result_type
= cv_unqualified (result_type
);
363 function
= build_call_array_loc (input_location
,
364 result_type
, function
, n
, argarray
);
365 set_flags_from_callee (function
);
367 decl
= get_callee_fndecl (function
);
369 if (decl
&& !TREE_USED (decl
))
371 /* We invoke build_call directly for several library
372 functions. These may have been declared normally if
373 we're building libgcc, so we can't just check
375 gcc_assert (DECL_ARTIFICIAL (decl
)
376 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl
)),
381 require_complete_eh_spec_types (fntype
, decl
);
383 TREE_HAS_CONSTRUCTOR (function
) = (decl
&& DECL_CONSTRUCTOR_P (decl
));
385 /* Don't pass empty class objects by value. This is useful
386 for tags in STL, which are used to control overload resolution.
387 We don't need to handle other cases of copying empty classes. */
388 if (!decl
|| !fndecl_built_in_p (decl
))
389 for (i
= 0; i
< n
; i
++)
391 tree arg
= CALL_EXPR_ARG (function
, i
);
392 if (is_empty_class (TREE_TYPE (arg
))
393 && simple_empty_class_p (TREE_TYPE (arg
), arg
, INIT_EXPR
))
395 while (TREE_CODE (arg
) == TARGET_EXPR
)
396 /* We're disconnecting the initializer from its target,
397 don't create a temporary. */
398 arg
= TARGET_EXPR_INITIAL (arg
);
399 tree t
= build0 (EMPTY_CLASS_EXPR
, TREE_TYPE (arg
));
400 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (t
), arg
, t
);
401 CALL_EXPR_ARG (function
, i
) = arg
;
408 /* New overloading code. */
412 struct candidate_warning
{
414 candidate_warning
*next
;
417 /* Information for providing diagnostics about why overloading failed. */
419 enum rejection_reason_code
{
422 rr_explicit_conversion
,
423 rr_template_conversion
,
425 rr_bad_arg_conversion
,
426 rr_template_unification
,
429 rr_constraint_failure
432 struct conversion_info
{
433 /* The index of the argument, 0-based. */
435 /* The actual argument or its type. */
437 /* The type of the parameter. */
439 /* The location of the argument. */
443 struct rejection_reason
{
444 enum rejection_reason_code code
;
446 /* Information about an arity mismatch. */
448 /* The expected number of arguments. */
450 /* The actual number of arguments in the call. */
452 /* Whether the call was a varargs call. */
455 /* Information about an argument conversion mismatch. */
456 struct conversion_info conversion
;
457 /* Same, but for bad argument conversions. */
458 struct conversion_info bad_conversion
;
459 /* Information about template unification failures. These are the
460 parameters passed to fn_type_unification. */
468 unification_kind_t strict
;
470 } template_unification
;
471 /* Information about template instantiation failures. These are the
472 parameters passed to instantiate_template. */
476 } template_instantiation
;
481 /* The FUNCTION_DECL that will be called if this candidate is
482 selected by overload resolution. */
484 /* If not NULL_TREE, the first argument to use when calling this
487 /* The rest of the arguments to use when calling this function. If
488 there are no further arguments this may be NULL or it may be an
490 const vec
<tree
, va_gc
> *args
;
491 /* The implicit conversion sequences for each of the arguments to
494 /* The number of implicit conversion sequences. */
496 /* If FN is a user-defined conversion, the standard conversion
497 sequence from the type returned by FN to the desired destination
499 conversion
*second_conv
;
500 struct rejection_reason
*reason
;
501 /* If FN is a member function, the binfo indicating the path used to
502 qualify the name of FN at the call site. This path is used to
503 determine whether or not FN is accessible if it is selected by
504 overload resolution. The DECL_CONTEXT of FN will always be a
505 (possibly improper) base of this binfo. */
507 /* If FN is a non-static member function, the binfo indicating the
508 subobject to which the `this' pointer should be converted if FN
509 is selected by overload resolution. The type pointed to by
510 the `this' pointer must correspond to the most derived class
511 indicated by the CONVERSION_PATH. */
512 tree conversion_path
;
515 candidate_warning
*warnings
;
519 /* The flags active in add_candidate. */
522 bool rewritten () { return (flags
& LOOKUP_REWRITTEN
); }
523 bool reversed () { return (flags
& LOOKUP_REVERSED
); }
526 /* Returns true iff T is a null pointer constant in the sense of
530 null_ptr_cst_p (tree t
)
532 tree type
= TREE_TYPE (t
);
536 A null pointer constant is an integer literal ([lex.icon]) with value
537 zero or a prvalue of type std::nullptr_t. */
538 if (NULLPTR_TYPE_P (type
))
541 if (cxx_dialect
>= cxx11
)
543 STRIP_ANY_LOCATION_WRAPPER (t
);
545 /* Core issue 903 says only literal 0 is a null pointer constant. */
546 if (TREE_CODE (t
) == INTEGER_CST
547 && !TREE_OVERFLOW (t
)
548 && TREE_CODE (type
) == INTEGER_TYPE
550 && !char_type_p (type
))
553 else if (CP_INTEGRAL_TYPE_P (type
))
555 t
= fold_non_dependent_expr (t
, tf_none
);
557 if (integer_zerop (t
) && !TREE_OVERFLOW (t
))
564 /* Returns true iff T is a null member pointer value (4.11). */
567 null_member_pointer_value_p (tree t
)
569 tree type
= TREE_TYPE (t
);
572 else if (TYPE_PTRMEMFUNC_P (type
))
573 return (TREE_CODE (t
) == CONSTRUCTOR
574 && CONSTRUCTOR_NELTS (t
)
575 && integer_zerop (CONSTRUCTOR_ELT (t
, 0)->value
));
576 else if (TYPE_PTRDATAMEM_P (type
))
577 return integer_all_onesp (t
);
582 /* Returns nonzero if PARMLIST consists of only default parms,
583 ellipsis, and/or undeduced parameter packs. */
586 sufficient_parms_p (const_tree parmlist
)
588 for (; parmlist
&& parmlist
!= void_list_node
;
589 parmlist
= TREE_CHAIN (parmlist
))
590 if (!TREE_PURPOSE (parmlist
)
591 && !PACK_EXPANSION_P (TREE_VALUE (parmlist
)))
596 /* Allocate N bytes of memory from the conversion obstack. The memory
597 is zeroed before being returned. */
600 conversion_obstack_alloc (size_t n
)
603 if (!conversion_obstack_initialized
)
605 gcc_obstack_init (&conversion_obstack
);
606 conversion_obstack_initialized
= true;
608 p
= obstack_alloc (&conversion_obstack
, n
);
613 /* Allocate rejection reasons. */
615 static struct rejection_reason
*
616 alloc_rejection (enum rejection_reason_code code
)
618 struct rejection_reason
*p
;
619 p
= (struct rejection_reason
*) conversion_obstack_alloc (sizeof *p
);
624 static struct rejection_reason
*
625 arity_rejection (tree first_arg
, int expected
, int actual
)
627 struct rejection_reason
*r
= alloc_rejection (rr_arity
);
628 int adjust
= first_arg
!= NULL_TREE
;
629 r
->u
.arity
.expected
= expected
- adjust
;
630 r
->u
.arity
.actual
= actual
- adjust
;
634 static struct rejection_reason
*
635 arg_conversion_rejection (tree first_arg
, int n_arg
, tree from
, tree to
,
638 struct rejection_reason
*r
= alloc_rejection (rr_arg_conversion
);
639 int adjust
= first_arg
!= NULL_TREE
;
640 r
->u
.conversion
.n_arg
= n_arg
- adjust
;
641 r
->u
.conversion
.from
= from
;
642 r
->u
.conversion
.to_type
= to
;
643 r
->u
.conversion
.loc
= loc
;
647 static struct rejection_reason
*
648 bad_arg_conversion_rejection (tree first_arg
, int n_arg
, tree from
, tree to
,
651 struct rejection_reason
*r
= alloc_rejection (rr_bad_arg_conversion
);
652 int adjust
= first_arg
!= NULL_TREE
;
653 r
->u
.bad_conversion
.n_arg
= n_arg
- adjust
;
654 r
->u
.bad_conversion
.from
= from
;
655 r
->u
.bad_conversion
.to_type
= to
;
656 r
->u
.bad_conversion
.loc
= loc
;
660 static struct rejection_reason
*
661 explicit_conversion_rejection (tree from
, tree to
)
663 struct rejection_reason
*r
= alloc_rejection (rr_explicit_conversion
);
664 r
->u
.conversion
.n_arg
= 0;
665 r
->u
.conversion
.from
= from
;
666 r
->u
.conversion
.to_type
= to
;
667 r
->u
.conversion
.loc
= UNKNOWN_LOCATION
;
671 static struct rejection_reason
*
672 template_conversion_rejection (tree from
, tree to
)
674 struct rejection_reason
*r
= alloc_rejection (rr_template_conversion
);
675 r
->u
.conversion
.n_arg
= 0;
676 r
->u
.conversion
.from
= from
;
677 r
->u
.conversion
.to_type
= to
;
678 r
->u
.conversion
.loc
= UNKNOWN_LOCATION
;
682 static struct rejection_reason
*
683 template_unification_rejection (tree tmpl
, tree explicit_targs
, tree targs
,
684 const tree
*args
, unsigned int nargs
,
685 tree return_type
, unification_kind_t strict
,
688 size_t args_n_bytes
= sizeof (*args
) * nargs
;
689 tree
*args1
= (tree
*) conversion_obstack_alloc (args_n_bytes
);
690 struct rejection_reason
*r
= alloc_rejection (rr_template_unification
);
691 r
->u
.template_unification
.tmpl
= tmpl
;
692 r
->u
.template_unification
.explicit_targs
= explicit_targs
;
693 r
->u
.template_unification
.num_targs
= TREE_VEC_LENGTH (targs
);
694 /* Copy args to our own storage. */
695 memcpy (args1
, args
, args_n_bytes
);
696 r
->u
.template_unification
.args
= args1
;
697 r
->u
.template_unification
.nargs
= nargs
;
698 r
->u
.template_unification
.return_type
= return_type
;
699 r
->u
.template_unification
.strict
= strict
;
700 r
->u
.template_unification
.flags
= flags
;
704 static struct rejection_reason
*
705 template_unification_error_rejection (void)
707 return alloc_rejection (rr_template_unification
);
710 static struct rejection_reason
*
711 invalid_copy_with_fn_template_rejection (void)
713 struct rejection_reason
*r
= alloc_rejection (rr_invalid_copy
);
717 static struct rejection_reason
*
718 inherited_ctor_rejection (void)
720 struct rejection_reason
*r
= alloc_rejection (rr_inherited_ctor
);
724 /* Build a constraint failure record. */
726 static struct rejection_reason
*
727 constraint_failure (void)
729 struct rejection_reason
*r
= alloc_rejection (rr_constraint_failure
);
733 /* Dynamically allocate a conversion. */
736 alloc_conversion (conversion_kind kind
)
739 c
= (conversion
*) conversion_obstack_alloc (sizeof (conversion
));
744 /* Make sure that all memory on the conversion obstack has been
748 validate_conversion_obstack (void)
750 if (conversion_obstack_initialized
)
751 gcc_assert ((obstack_next_free (&conversion_obstack
)
752 == obstack_base (&conversion_obstack
)));
755 /* Dynamically allocate an array of N conversions. */
758 alloc_conversions (size_t n
)
760 return (conversion
**) conversion_obstack_alloc (n
* sizeof (conversion
*));
764 build_conv (conversion_kind code
, tree type
, conversion
*from
)
767 conversion_rank rank
= CONVERSION_RANK (from
);
769 /* Note that the caller is responsible for filling in t->cand for
770 user-defined conversions. */
771 t
= alloc_conversion (code
);
795 t
->user_conv_p
= (code
== ck_user
|| from
->user_conv_p
);
796 t
->bad_p
= from
->bad_p
;
801 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
802 specialization of std::initializer_list<T>, if such a conversion is
806 build_list_conv (tree type
, tree ctor
, int flags
, tsubst_flags_t complain
)
808 tree elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type
), 0);
809 unsigned len
= CONSTRUCTOR_NELTS (ctor
);
810 conversion
**subconvs
= alloc_conversions (len
);
815 /* Within a list-initialization we can have more user-defined
817 flags
&= ~LOOKUP_NO_CONVERSION
;
818 /* But no narrowing conversions. */
819 flags
|= LOOKUP_NO_NARROWING
;
821 /* Can't make an array of these types. */
822 if (TYPE_REF_P (elttype
)
823 || TREE_CODE (elttype
) == FUNCTION_TYPE
824 || VOID_TYPE_P (elttype
))
827 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), i
, val
)
830 = implicit_conversion (elttype
, TREE_TYPE (val
), val
,
831 false, flags
, complain
);
838 t
= alloc_conversion (ck_list
);
840 t
->u
.list
= subconvs
;
843 for (i
= 0; i
< len
; ++i
)
845 conversion
*sub
= subconvs
[i
];
846 if (sub
->rank
> t
->rank
)
848 if (sub
->user_conv_p
)
849 t
->user_conv_p
= true;
857 /* Return the next conversion of the conversion chain (if applicable),
858 or NULL otherwise. Please use this function instead of directly
859 accessing fields of struct conversion. */
862 next_conversion (conversion
*conv
)
865 || conv
->kind
== ck_identity
866 || conv
->kind
== ck_ambig
867 || conv
->kind
== ck_list
868 || conv
->kind
== ck_aggr
)
873 /* Strip to the first ck_user, ck_ambig, ck_list, ck_aggr or ck_identity
877 strip_standard_conversion (conversion
*conv
)
880 && conv
->kind
!= ck_user
881 && conv
->kind
!= ck_ambig
882 && conv
->kind
!= ck_list
883 && conv
->kind
!= ck_aggr
884 && conv
->kind
!= ck_identity
)
885 conv
= next_conversion (conv
);
889 /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
890 is a valid aggregate initializer for array type ATYPE. */
893 can_convert_array (tree atype
, tree ctor
, int flags
, tsubst_flags_t complain
)
896 tree elttype
= TREE_TYPE (atype
);
897 for (i
= 0; i
< CONSTRUCTOR_NELTS (ctor
); ++i
)
899 tree val
= CONSTRUCTOR_ELT (ctor
, i
)->value
;
901 if (TREE_CODE (elttype
) == ARRAY_TYPE
902 && TREE_CODE (val
) == CONSTRUCTOR
)
903 ok
= can_convert_array (elttype
, val
, flags
, complain
);
905 ok
= can_convert_arg (elttype
, TREE_TYPE (val
), val
, flags
,
913 /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if
914 FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively
918 field_in_pset (hash_set
<tree
, true> &pset
, tree field
)
920 if (pset
.contains (field
))
922 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
923 for (field
= TYPE_FIELDS (TREE_TYPE (field
));
924 field
; field
= DECL_CHAIN (field
))
926 field
= next_initializable_field (field
);
927 if (field
== NULL_TREE
)
929 if (field_in_pset (pset
, field
))
935 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
936 aggregate class, if such a conversion is possible. */
939 build_aggr_conv (tree type
, tree ctor
, int flags
, tsubst_flags_t complain
)
941 unsigned HOST_WIDE_INT i
= 0;
943 tree field
= next_initializable_field (TYPE_FIELDS (type
));
944 tree empty_ctor
= NULL_TREE
;
945 hash_set
<tree
, true> pset
;
947 /* We already called reshape_init in implicit_conversion. */
949 /* The conversions within the init-list aren't affected by the enclosing
950 context; they're always simple copy-initialization. */
951 flags
= LOOKUP_IMPLICIT
|LOOKUP_NO_NARROWING
;
953 /* For designated initializers, verify that each initializer is convertible
954 to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
955 visited. In the following loop then ignore already visited
957 if (CONSTRUCTOR_IS_DESIGNATED_INIT (ctor
))
960 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor
), i
, idx
, val
)
962 if (idx
&& TREE_CODE (idx
) == FIELD_DECL
)
964 tree ftype
= TREE_TYPE (idx
);
967 if (TREE_CODE (ftype
) == ARRAY_TYPE
968 && TREE_CODE (val
) == CONSTRUCTOR
)
969 ok
= can_convert_array (ftype
, val
, flags
, complain
);
971 ok
= can_convert_arg (ftype
, TREE_TYPE (val
), val
, flags
,
976 /* For unions, there should be just one initializer. */
977 if (TREE_CODE (type
) == UNION_TYPE
)
990 for (; field
; field
= next_initializable_field (DECL_CHAIN (field
)))
992 tree ftype
= TREE_TYPE (field
);
996 if (!pset
.is_empty () && field_in_pset (pset
, field
))
998 if (i
< CONSTRUCTOR_NELTS (ctor
))
1000 val
= CONSTRUCTOR_ELT (ctor
, i
)->value
;
1003 else if (DECL_INITIAL (field
))
1004 val
= get_nsdmi (field
, /*ctor*/false, complain
);
1005 else if (TYPE_REF_P (ftype
))
1006 /* Value-initialization of reference is ill-formed. */
1010 if (empty_ctor
== NULL_TREE
)
1011 empty_ctor
= build_constructor (init_list_type_node
, NULL
);
1015 if (TREE_CODE (ftype
) == ARRAY_TYPE
1016 && TREE_CODE (val
) == CONSTRUCTOR
)
1017 ok
= can_convert_array (ftype
, val
, flags
, complain
);
1019 ok
= can_convert_arg (ftype
, TREE_TYPE (val
), val
, flags
,
1025 if (TREE_CODE (type
) == UNION_TYPE
)
1029 if (i
< CONSTRUCTOR_NELTS (ctor
))
1032 c
= alloc_conversion (ck_aggr
);
1035 c
->user_conv_p
= true;
1036 c
->check_narrowing
= true;
1041 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1042 array type, if such a conversion is possible. */
1045 build_array_conv (tree type
, tree ctor
, int flags
, tsubst_flags_t complain
)
1048 unsigned HOST_WIDE_INT len
= CONSTRUCTOR_NELTS (ctor
);
1049 tree elttype
= TREE_TYPE (type
);
1054 enum conversion_rank rank
= cr_exact
;
1056 /* We might need to propagate the size from the element to the array. */
1057 complete_type (type
);
1059 if (TYPE_DOMAIN (type
)
1060 && !variably_modified_type_p (TYPE_DOMAIN (type
), NULL_TREE
))
1062 unsigned HOST_WIDE_INT alen
= tree_to_uhwi (array_type_nelts_top (type
));
1067 flags
= LOOKUP_IMPLICIT
|LOOKUP_NO_NARROWING
;
1069 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), i
, val
)
1072 = implicit_conversion (elttype
, TREE_TYPE (val
), val
,
1073 false, flags
, complain
);
1077 if (sub
->rank
> rank
)
1079 if (sub
->user_conv_p
)
1085 c
= alloc_conversion (ck_aggr
);
1088 c
->user_conv_p
= user
;
1094 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1095 complex type, if such a conversion is possible. */
1098 build_complex_conv (tree type
, tree ctor
, int flags
,
1099 tsubst_flags_t complain
)
1102 unsigned HOST_WIDE_INT len
= CONSTRUCTOR_NELTS (ctor
);
1103 tree elttype
= TREE_TYPE (type
);
1108 enum conversion_rank rank
= cr_exact
;
1113 flags
= LOOKUP_IMPLICIT
|LOOKUP_NO_NARROWING
;
1115 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), i
, val
)
1118 = implicit_conversion (elttype
, TREE_TYPE (val
), val
,
1119 false, flags
, complain
);
1123 if (sub
->rank
> rank
)
1125 if (sub
->user_conv_p
)
1131 c
= alloc_conversion (ck_aggr
);
1134 c
->user_conv_p
= user
;
1140 /* Build a representation of the identity conversion from EXPR to
1141 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1144 build_identity_conv (tree type
, tree expr
)
1148 c
= alloc_conversion (ck_identity
);
1155 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1156 were multiple user-defined conversions to accomplish the job.
1157 Build a conversion that indicates that ambiguity. */
1160 build_ambiguous_conv (tree type
, tree expr
)
1164 c
= alloc_conversion (ck_ambig
);
1172 strip_top_quals (tree t
)
1174 if (TREE_CODE (t
) == ARRAY_TYPE
)
1176 return cp_build_qualified_type (t
, 0);
1179 /* Returns the standard conversion path (see [conv]) from type FROM to type
1180 TO, if any. For proper handling of null pointer constants, you must
1181 also pass the expression EXPR to convert from. If C_CAST_P is true,
1182 this conversion is coming from a C-style cast. */
1185 standard_conversion (tree to
, tree from
, tree expr
, bool c_cast_p
,
1186 int flags
, tsubst_flags_t complain
)
1188 enum tree_code fcode
, tcode
;
1190 bool fromref
= false;
1193 to
= non_reference (to
);
1194 if (TYPE_REF_P (from
))
1197 from
= TREE_TYPE (from
);
1200 to
= strip_top_quals (to
);
1201 from
= strip_top_quals (from
);
1203 if (expr
&& type_unknown_p (expr
))
1205 if (TYPE_PTRFN_P (to
) || TYPE_PTRMEMFUNC_P (to
))
1207 tsubst_flags_t tflags
= tf_conv
;
1208 expr
= instantiate_type (to
, expr
, tflags
);
1209 if (expr
== error_mark_node
)
1211 from
= TREE_TYPE (expr
);
1213 else if (TREE_CODE (to
) == BOOLEAN_TYPE
)
1215 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1216 expr
= resolve_nondeduced_context (expr
, complain
);
1217 from
= TREE_TYPE (expr
);
1221 fcode
= TREE_CODE (from
);
1222 tcode
= TREE_CODE (to
);
1224 conv
= build_identity_conv (from
, expr
);
1225 if (fcode
== FUNCTION_TYPE
|| fcode
== ARRAY_TYPE
)
1227 from
= type_decays_to (from
);
1228 fcode
= TREE_CODE (from
);
1229 /* Tell convert_like that we're using the address. */
1230 conv
->rvaluedness_matches_p
= true;
1231 conv
= build_conv (ck_lvalue
, from
, conv
);
1233 /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1234 obvalue_p) seems odd, since it's already a prvalue, but that's how we
1235 express the copy constructor call required by copy-initialization. */
1236 else if (fromref
|| (expr
&& obvalue_p (expr
)))
1241 bitfield_type
= is_bitfield_expr_with_lowered_type (expr
);
1244 from
= strip_top_quals (bitfield_type
);
1245 fcode
= TREE_CODE (from
);
1248 conv
= build_conv (ck_rvalue
, from
, conv
);
1249 if (flags
& LOOKUP_PREFER_RVALUE
)
1250 /* Tell convert_like to set LOOKUP_PREFER_RVALUE. */
1251 conv
->rvaluedness_matches_p
= true;
1252 /* If we're performing copy-initialization, remember to skip
1253 explicit constructors. */
1254 if (flags
& LOOKUP_ONLYCONVERTING
)
1255 conv
->copy_init_p
= true;
1258 /* Allow conversion between `__complex__' data types. */
1259 if (tcode
== COMPLEX_TYPE
&& fcode
== COMPLEX_TYPE
)
1261 /* The standard conversion sequence to convert FROM to TO is
1262 the standard conversion sequence to perform componentwise
1264 conversion
*part_conv
= standard_conversion
1265 (TREE_TYPE (to
), TREE_TYPE (from
), NULL_TREE
, c_cast_p
, flags
,
1270 conv
= build_conv (part_conv
->kind
, to
, conv
);
1271 conv
->rank
= part_conv
->rank
;
1279 if (same_type_p (from
, to
))
1281 if (CLASS_TYPE_P (to
) && conv
->kind
== ck_rvalue
)
1282 conv
->type
= qualified_to
;
1287 A null pointer constant can be converted to a pointer type; ... A
1288 null pointer constant of integral type can be converted to an
1289 rvalue of type std::nullptr_t. */
1290 if ((tcode
== POINTER_TYPE
|| TYPE_PTRMEM_P (to
)
1291 || NULLPTR_TYPE_P (to
))
1292 && ((expr
&& null_ptr_cst_p (expr
))
1293 || NULLPTR_TYPE_P (from
)))
1294 conv
= build_conv (ck_std
, to
, conv
);
1295 else if ((tcode
== INTEGER_TYPE
&& fcode
== POINTER_TYPE
)
1296 || (tcode
== POINTER_TYPE
&& fcode
== INTEGER_TYPE
))
1298 /* For backwards brain damage compatibility, allow interconversion of
1299 pointers and integers with a pedwarn. */
1300 conv
= build_conv (ck_std
, to
, conv
);
1303 else if (UNSCOPED_ENUM_P (to
) && fcode
== INTEGER_TYPE
)
1305 /* For backwards brain damage compatibility, allow interconversion of
1306 enums and integers with a pedwarn. */
1307 conv
= build_conv (ck_std
, to
, conv
);
1310 else if ((tcode
== POINTER_TYPE
&& fcode
== POINTER_TYPE
)
1311 || (TYPE_PTRDATAMEM_P (to
) && TYPE_PTRDATAMEM_P (from
)))
1316 if (tcode
== POINTER_TYPE
)
1318 to_pointee
= TREE_TYPE (to
);
1319 from_pointee
= TREE_TYPE (from
);
1321 /* Since this is the target of a pointer, it can't have function
1322 qualifiers, so any TYPE_QUALS must be for attributes const or
1323 noreturn. Strip them. */
1324 if (TREE_CODE (to_pointee
) == FUNCTION_TYPE
1325 && TYPE_QUALS (to_pointee
))
1326 to_pointee
= build_qualified_type (to_pointee
, TYPE_UNQUALIFIED
);
1327 if (TREE_CODE (from_pointee
) == FUNCTION_TYPE
1328 && TYPE_QUALS (from_pointee
))
1329 from_pointee
= build_qualified_type (from_pointee
, TYPE_UNQUALIFIED
);
1333 to_pointee
= TYPE_PTRMEM_POINTED_TO_TYPE (to
);
1334 from_pointee
= TYPE_PTRMEM_POINTED_TO_TYPE (from
);
1337 if (tcode
== POINTER_TYPE
1338 && same_type_ignoring_top_level_qualifiers_p (from_pointee
,
1341 else if (VOID_TYPE_P (to_pointee
)
1342 && !TYPE_PTRDATAMEM_P (from
)
1343 && TREE_CODE (from_pointee
) != FUNCTION_TYPE
)
1345 tree nfrom
= TREE_TYPE (from
);
1346 /* Don't try to apply restrict to void. */
1347 int quals
= cp_type_quals (nfrom
) & ~TYPE_QUAL_RESTRICT
;
1348 from_pointee
= cp_build_qualified_type (void_type_node
, quals
);
1349 from
= build_pointer_type (from_pointee
);
1350 conv
= build_conv (ck_ptr
, from
, conv
);
1352 else if (TYPE_PTRDATAMEM_P (from
))
1354 tree fbase
= TYPE_PTRMEM_CLASS_TYPE (from
);
1355 tree tbase
= TYPE_PTRMEM_CLASS_TYPE (to
);
1357 if (same_type_p (fbase
, tbase
))
1358 /* No base conversion needed. */;
1359 else if (DERIVED_FROM_P (fbase
, tbase
)
1360 && (same_type_ignoring_top_level_qualifiers_p
1361 (from_pointee
, to_pointee
)))
1363 from
= build_ptrmem_type (tbase
, from_pointee
);
1364 conv
= build_conv (ck_pmem
, from
, conv
);
1369 else if (CLASS_TYPE_P (from_pointee
)
1370 && CLASS_TYPE_P (to_pointee
)
1373 An rvalue of type "pointer to cv D," where D is a
1374 class type, can be converted to an rvalue of type
1375 "pointer to cv B," where B is a base class (clause
1376 _class.derived_) of D. If B is an inaccessible
1377 (clause _class.access_) or ambiguous
1378 (_class.member.lookup_) base class of D, a program
1379 that necessitates this conversion is ill-formed.
1380 Therefore, we use DERIVED_FROM_P, and do not check
1381 access or uniqueness. */
1382 && DERIVED_FROM_P (to_pointee
, from_pointee
))
1385 = cp_build_qualified_type (to_pointee
,
1386 cp_type_quals (from_pointee
));
1387 from
= build_pointer_type (from_pointee
);
1388 conv
= build_conv (ck_ptr
, from
, conv
);
1389 conv
->base_p
= true;
1392 if (same_type_p (from
, to
))
1394 else if (c_cast_p
&& comp_ptr_ttypes_const (to
, from
, bounds_either
))
1395 /* In a C-style cast, we ignore CV-qualification because we
1396 are allowed to perform a static_cast followed by a
1398 conv
= build_conv (ck_qual
, to
, conv
);
1399 else if (!c_cast_p
&& comp_ptr_ttypes (to_pointee
, from_pointee
))
1400 conv
= build_conv (ck_qual
, to
, conv
);
1401 else if (expr
&& string_conv_p (to
, expr
, 0))
1402 /* converting from string constant to char *. */
1403 conv
= build_conv (ck_qual
, to
, conv
);
1404 else if (fnptr_conv_p (to
, from
))
1405 conv
= build_conv (ck_fnptr
, to
, conv
);
1406 /* Allow conversions among compatible ObjC pointer types (base
1407 conversions have been already handled above). */
1408 else if (c_dialect_objc ()
1409 && objc_compare_types (to
, from
, -4, NULL_TREE
))
1410 conv
= build_conv (ck_ptr
, to
, conv
);
1411 else if (ptr_reasonably_similar (to_pointee
, from_pointee
))
1413 conv
= build_conv (ck_ptr
, to
, conv
);
1421 else if (TYPE_PTRMEMFUNC_P (to
) && TYPE_PTRMEMFUNC_P (from
))
1423 tree fromfn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from
));
1424 tree tofn
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to
));
1425 tree fbase
= class_of_this_parm (fromfn
);
1426 tree tbase
= class_of_this_parm (tofn
);
1428 if (!DERIVED_FROM_P (fbase
, tbase
))
1431 tree fstat
= static_fn_type (fromfn
);
1432 tree tstat
= static_fn_type (tofn
);
1433 if (same_type_p (tstat
, fstat
)
1434 || fnptr_conv_p (tstat
, fstat
))
1439 if (!same_type_p (fbase
, tbase
))
1441 from
= build_memfn_type (fstat
,
1443 cp_type_quals (tbase
),
1444 type_memfn_rqual (tofn
));
1445 from
= build_ptrmemfunc_type (build_pointer_type (from
));
1446 conv
= build_conv (ck_pmem
, from
, conv
);
1447 conv
->base_p
= true;
1449 if (fnptr_conv_p (tstat
, fstat
))
1450 conv
= build_conv (ck_fnptr
, to
, conv
);
1452 else if (tcode
== BOOLEAN_TYPE
)
1456 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1457 to member type can be converted to a prvalue of type bool. ...
1458 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1459 std::nullptr_t can be converted to a prvalue of type bool; */
1460 if (ARITHMETIC_TYPE_P (from
)
1461 || UNSCOPED_ENUM_P (from
)
1462 || fcode
== POINTER_TYPE
1463 || TYPE_PTRMEM_P (from
)
1464 || NULLPTR_TYPE_P (from
))
1466 conv
= build_conv (ck_std
, to
, conv
);
1467 if (fcode
== POINTER_TYPE
1468 || TYPE_PTRDATAMEM_P (from
)
1469 || (TYPE_PTRMEMFUNC_P (from
)
1470 && conv
->rank
< cr_pbool
)
1471 || NULLPTR_TYPE_P (from
))
1472 conv
->rank
= cr_pbool
;
1473 if (NULLPTR_TYPE_P (from
) && (flags
& LOOKUP_ONLYCONVERTING
))
1475 if (flags
& LOOKUP_NO_NARROWING
)
1476 conv
->check_narrowing
= true;
1482 /* We don't check for ENUMERAL_TYPE here because there are no standard
1483 conversions to enum type. */
1484 /* As an extension, allow conversion to complex type. */
1485 else if (ARITHMETIC_TYPE_P (to
))
1487 if (! (INTEGRAL_CODE_P (fcode
)
1488 || (fcode
== REAL_TYPE
&& !(flags
& LOOKUP_NO_NON_INTEGRAL
)))
1489 || SCOPED_ENUM_P (from
))
1492 /* If we're parsing an enum with no fixed underlying type, we're
1493 dealing with an incomplete type, which renders the conversion
1495 if (!COMPLETE_TYPE_P (from
))
1498 conv
= build_conv (ck_std
, to
, conv
);
1500 tree underlying_type
= NULL_TREE
;
1501 if (TREE_CODE (from
) == ENUMERAL_TYPE
1502 && ENUM_FIXED_UNDERLYING_TYPE_P (from
))
1503 underlying_type
= ENUM_UNDERLYING_TYPE (from
);
1505 /* Give this a better rank if it's a promotion.
1507 To handle CWG 1601, also bump the rank if we are converting
1508 an enumeration with a fixed underlying type to the underlying
1510 if ((same_type_p (to
, type_promotes_to (from
))
1511 || (underlying_type
&& same_type_p (to
, underlying_type
)))
1512 && next_conversion (conv
)->rank
<= cr_promotion
)
1513 conv
->rank
= cr_promotion
;
1515 else if (fcode
== VECTOR_TYPE
&& tcode
== VECTOR_TYPE
1516 && vector_types_convertible_p (from
, to
, false))
1517 return build_conv (ck_std
, to
, conv
);
1518 else if (MAYBE_CLASS_TYPE_P (to
) && MAYBE_CLASS_TYPE_P (from
)
1519 && is_properly_derived_from (from
, to
))
1521 if (conv
->kind
== ck_rvalue
)
1522 conv
= next_conversion (conv
);
1523 conv
= build_conv (ck_base
, to
, conv
);
1524 /* The derived-to-base conversion indicates the initialization
1525 of a parameter with base type from an object of a derived
1526 type. A temporary object is created to hold the result of
1527 the conversion unless we're binding directly to a reference. */
1528 conv
->need_temporary_p
= !(flags
& LOOKUP_NO_TEMP_BIND
);
1529 if (flags
& LOOKUP_PREFER_RVALUE
)
1530 /* Tell convert_like to set LOOKUP_PREFER_RVALUE. */
1531 conv
->rvaluedness_matches_p
= true;
1532 /* If we're performing copy-initialization, remember to skip
1533 explicit constructors. */
1534 if (flags
& LOOKUP_ONLYCONVERTING
)
1535 conv
->copy_init_p
= true;
1540 if (flags
& LOOKUP_NO_NARROWING
)
1541 conv
->check_narrowing
= true;
1546 /* Returns nonzero if T1 is reference-related to T2. */
1549 reference_related_p (tree t1
, tree t2
)
1551 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1554 t1
= TYPE_MAIN_VARIANT (t1
);
1555 t2
= TYPE_MAIN_VARIANT (t2
);
1559 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1560 to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1561 return (similar_type_p (t1
, t2
)
1562 || (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
1563 && DERIVED_FROM_P (t1
, t2
)));
1566 /* Returns nonzero if T1 is reference-compatible with T2. */
1569 reference_compatible_p (tree t1
, tree t2
)
1573 "cv1 T1" is reference compatible with "cv2 T2" if
1574 a prvalue of type "pointer to cv2 T2" can be converted to the type
1575 "pointer to cv1 T1" via a standard conversion sequence. */
1576 tree ptype1
= build_pointer_type (t1
);
1577 tree ptype2
= build_pointer_type (t2
);
1578 conversion
*conv
= standard_conversion (ptype1
, ptype2
, NULL_TREE
,
1579 /*c_cast_p=*/false, 0, tf_none
);
1580 if (!conv
|| conv
->bad_p
)
1585 /* Return true if converting FROM to TO would involve a qualification
1589 involves_qualification_conversion_p (tree to
, tree from
)
1591 /* If we're not convering a pointer to another one, we won't get
1592 a qualification conversion. */
1593 if (!((TYPE_PTR_P (to
) && TYPE_PTR_P (from
))
1594 || (TYPE_PTRDATAMEM_P (to
) && TYPE_PTRDATAMEM_P (from
))))
1597 conversion
*conv
= standard_conversion (to
, from
, NULL_TREE
,
1598 /*c_cast_p=*/false, 0, tf_none
);
1599 for (conversion
*t
= conv
; t
; t
= next_conversion (t
))
1600 if (t
->kind
== ck_qual
)
1606 /* A reference of the indicated TYPE is being bound directly to the
1607 expression represented by the implicit conversion sequence CONV.
1608 Return a conversion sequence for this binding. */
1611 direct_reference_binding (tree type
, conversion
*conv
)
1615 gcc_assert (TYPE_REF_P (type
));
1616 gcc_assert (!TYPE_REF_P (conv
->type
));
1618 t
= TREE_TYPE (type
);
1620 if (conv
->kind
== ck_identity
)
1621 /* Mark the identity conv as to not decay to rvalue. */
1622 conv
->rvaluedness_matches_p
= true;
1626 When a parameter of reference type binds directly
1627 (_dcl.init.ref_) to an argument expression, the implicit
1628 conversion sequence is the identity conversion, unless the
1629 argument expression has a type that is a derived class of the
1630 parameter type, in which case the implicit conversion sequence is
1631 a derived-to-base Conversion.
1633 If the parameter binds directly to the result of applying a
1634 conversion function to the argument expression, the implicit
1635 conversion sequence is a user-defined conversion sequence
1636 (_over.ics.user_), with the second standard conversion sequence
1637 either an identity conversion or, if the conversion function
1638 returns an entity of a type that is a derived class of the
1639 parameter type, a derived-to-base conversion. */
1640 if (is_properly_derived_from (conv
->type
, t
))
1642 /* Represent the derived-to-base conversion. */
1643 conv
= build_conv (ck_base
, t
, conv
);
1644 /* We will actually be binding to the base-class subobject in
1645 the derived class, so we mark this conversion appropriately.
1646 That way, convert_like knows not to generate a temporary. */
1647 conv
->need_temporary_p
= false;
1649 else if (involves_qualification_conversion_p (t
, conv
->type
))
1650 /* Represent the qualification conversion. After DR 2352
1651 #1 and #2 were indistinguishable conversion sequences:
1654 void f(const int* const &); // #2
1655 void g(int* p) { f(p); }
1657 because the types "int *" and "const int *const" are
1658 reference-related and we were binding both directly and they
1659 had the same rank. To break it up, we add a ck_qual under the
1660 ck_ref_bind so that conversion sequence ranking chooses #1. */
1661 conv
= build_conv (ck_qual
, t
, conv
);
1663 return build_conv (ck_ref_bind
, type
, conv
);
1666 /* Returns the conversion path from type FROM to reference type TO for
1667 purposes of reference binding. For lvalue binding, either pass a
1668 reference type to FROM or an lvalue expression to EXPR. If the
1669 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1670 the conversion returned. If C_CAST_P is true, this
1671 conversion is coming from a C-style cast. */
1674 reference_binding (tree rto
, tree rfrom
, tree expr
, bool c_cast_p
, int flags
,
1675 tsubst_flags_t complain
)
1677 conversion
*conv
= NULL
;
1678 tree to
= TREE_TYPE (rto
);
1683 cp_lvalue_kind gl_kind
;
1686 if (TREE_CODE (to
) == FUNCTION_TYPE
&& expr
&& type_unknown_p (expr
))
1688 expr
= instantiate_type (to
, expr
, tf_none
);
1689 if (expr
== error_mark_node
)
1691 from
= TREE_TYPE (expr
);
1694 bool copy_list_init
= false;
1695 if (expr
&& BRACE_ENCLOSED_INITIALIZER_P (expr
))
1697 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
1698 /* DR 1288: Otherwise, if the initializer list has a single element
1699 of type E and ... [T's] referenced type is reference-related to E,
1700 the object or reference is initialized from that element...
1702 ??? With P0388R4, we should bind 't' directly to U{}:
1705 because A[] and A[2] are reference-related. But we don't do it
1706 because grok_reference_init has deduced the array size (to 1), and
1707 A[1] and A[2] aren't reference-related. */
1708 if (CONSTRUCTOR_NELTS (expr
) == 1)
1710 tree elt
= CONSTRUCTOR_ELT (expr
, 0)->value
;
1711 if (error_operand_p (elt
))
1713 tree etype
= TREE_TYPE (elt
);
1714 if (reference_related_p (to
, etype
))
1721 /* Otherwise, if T is a reference type, a prvalue temporary of the type
1722 referenced by T is copy-list-initialized, and the reference is bound
1723 to that temporary. */
1724 copy_list_init
= true;
1728 if (TYPE_REF_P (from
))
1730 from
= TREE_TYPE (from
);
1731 if (!TYPE_REF_IS_RVALUE (rfrom
)
1732 || TREE_CODE (from
) == FUNCTION_TYPE
)
1733 gl_kind
= clk_ordinary
;
1735 gl_kind
= clk_rvalueref
;
1738 gl_kind
= lvalue_kind (expr
);
1739 else if (CLASS_TYPE_P (from
)
1740 || TREE_CODE (from
) == ARRAY_TYPE
)
1741 gl_kind
= clk_class
;
1745 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1746 if ((flags
& LOOKUP_NO_TEMP_BIND
)
1747 && (gl_kind
& clk_class
))
1750 /* Same mask as real_lvalue_p. */
1751 is_lvalue
= gl_kind
&& !(gl_kind
& (clk_rvalueref
|clk_class
));
1754 if ((gl_kind
& clk_bitfield
) != 0)
1755 tfrom
= unlowered_expr_type (expr
);
1757 /* Figure out whether or not the types are reference-related and
1758 reference compatible. We have to do this after stripping
1759 references from FROM. */
1760 related_p
= reference_related_p (to
, tfrom
);
1761 /* If this is a C cast, first convert to an appropriately qualified
1762 type, so that we can later do a const_cast to the desired type. */
1763 if (related_p
&& c_cast_p
1764 && !at_least_as_qualified_p (to
, tfrom
))
1765 to
= cp_build_qualified_type (to
, cp_type_quals (tfrom
));
1766 compatible_p
= reference_compatible_p (to
, tfrom
);
1768 /* Directly bind reference when target expression's type is compatible with
1769 the reference and expression is an lvalue. In DR391, the wording in
1770 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1771 const and rvalue references to rvalues of compatible class type.
1772 We should also do direct bindings for non-class xvalues. */
1773 if ((related_p
|| compatible_p
) && gl_kind
)
1777 If the initializer expression
1779 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1780 is reference-compatible with "cv2 T2,"
1782 the reference is bound directly to the initializer expression
1786 If the initializer expression is an rvalue, with T2 a class type,
1787 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1788 is bound to the object represented by the rvalue or to a sub-object
1789 within that object. */
1791 conv
= build_identity_conv (tfrom
, expr
);
1792 conv
= direct_reference_binding (rto
, conv
);
1794 if (TYPE_REF_P (rfrom
))
1795 /* Handle rvalue reference to function properly. */
1796 conv
->rvaluedness_matches_p
1797 = (TYPE_REF_IS_RVALUE (rto
) == TYPE_REF_IS_RVALUE (rfrom
));
1799 conv
->rvaluedness_matches_p
1800 = (TYPE_REF_IS_RVALUE (rto
) == !is_lvalue
);
1802 if ((gl_kind
& clk_bitfield
) != 0
1803 || ((gl_kind
& clk_packed
) != 0 && !TYPE_PACKED (to
)))
1804 /* For the purposes of overload resolution, we ignore the fact
1805 this expression is a bitfield or packed field. (In particular,
1806 [over.ics.ref] says specifically that a function with a
1807 non-const reference parameter is viable even if the
1808 argument is a bitfield.)
1810 However, when we actually call the function we must create
1811 a temporary to which to bind the reference. If the
1812 reference is volatile, or isn't const, then we cannot make
1813 a temporary, so we just issue an error when the conversion
1815 conv
->need_temporary_p
= true;
1817 /* Don't allow binding of lvalues (other than function lvalues) to
1818 rvalue references. */
1819 if (is_lvalue
&& TYPE_REF_IS_RVALUE (rto
)
1820 && TREE_CODE (to
) != FUNCTION_TYPE
)
1823 /* Nor the reverse. */
1824 if (!is_lvalue
&& !TYPE_REF_IS_RVALUE (rto
)
1825 /* Unless it's really an lvalue. */
1826 && !(cxx_dialect
>= cxx20
1827 && (gl_kind
& clk_implicit_rval
))
1828 && (!CP_TYPE_CONST_NON_VOLATILE_P (to
)
1829 || (flags
& LOOKUP_NO_RVAL_BIND
))
1830 && TREE_CODE (to
) != FUNCTION_TYPE
)
1838 /* [class.conv.fct] A conversion function is never used to convert a
1839 (possibly cv-qualified) object to the (possibly cv-qualified) same
1840 object type (or a reference to it), to a (possibly cv-qualified) base
1841 class of that type (or a reference to it).... */
1842 else if (CLASS_TYPE_P (from
) && !related_p
1843 && !(flags
& LOOKUP_NO_CONVERSION
))
1847 If the initializer expression
1849 -- has a class type (i.e., T2 is a class type) can be
1850 implicitly converted to an lvalue of type "cv3 T3," where
1851 "cv1 T1" is reference-compatible with "cv3 T3". (this
1852 conversion is selected by enumerating the applicable
1853 conversion functions (_over.match.ref_) and choosing the
1854 best one through overload resolution. (_over.match_).
1856 the reference is bound to the lvalue result of the conversion
1857 in the second case. */
1858 z_candidate
*cand
= build_user_type_conversion_1 (rto
, expr
, flags
,
1861 return cand
->second_conv
;
1864 /* From this point on, we conceptually need temporaries, even if we
1865 elide them. Only the cases above are "direct bindings". */
1866 if (flags
& LOOKUP_NO_TEMP_BIND
)
1871 When a parameter of reference type is not bound directly to an
1872 argument expression, the conversion sequence is the one required
1873 to convert the argument expression to the underlying type of the
1874 reference according to _over.best.ics_. Conceptually, this
1875 conversion sequence corresponds to copy-initializing a temporary
1876 of the underlying type with the argument expression. Any
1877 difference in top-level cv-qualification is subsumed by the
1878 initialization itself and does not constitute a conversion. */
1882 Otherwise, the reference shall be an lvalue reference to a
1883 non-volatile const type, or the reference shall be an rvalue
1886 We try below to treat this as a bad conversion to improve diagnostics,
1887 but if TO is an incomplete class, we need to reject this conversion
1888 now to avoid unnecessary instantiation. */
1889 if (!CP_TYPE_CONST_NON_VOLATILE_P (to
) && !TYPE_REF_IS_RVALUE (rto
)
1890 && !COMPLETE_TYPE_P (to
))
1893 /* We're generating a temporary now, but don't bind any more in the
1894 conversion (specifically, don't slice the temporary returned by a
1895 conversion operator). */
1896 flags
|= LOOKUP_NO_TEMP_BIND
;
1898 /* Core issue 899: When [copy-]initializing a temporary to be bound
1899 to the first parameter of a copy constructor (12.8) called with
1900 a single argument in the context of direct-initialization,
1901 explicit conversion functions are also considered.
1903 So don't set LOOKUP_ONLYCONVERTING in that case. */
1904 if (!(flags
& LOOKUP_COPY_PARM
))
1905 flags
|= LOOKUP_ONLYCONVERTING
;
1908 conv
= implicit_conversion (to
, from
, expr
, c_cast_p
,
1913 if (conv
->user_conv_p
)
1916 /* Remember this was copy-list-initialization. */
1917 conv
->need_temporary_p
= true;
1919 /* If initializing the temporary used a conversion function,
1920 recalculate the second conversion sequence. */
1921 for (conversion
*t
= conv
; t
; t
= next_conversion (t
))
1922 if (t
->kind
== ck_user
1923 && DECL_CONV_FN_P (t
->cand
->fn
))
1925 tree ftype
= TREE_TYPE (TREE_TYPE (t
->cand
->fn
));
1926 /* A prvalue of non-class type is cv-unqualified. */
1927 if (!TYPE_REF_P (ftype
) && !CLASS_TYPE_P (ftype
))
1928 ftype
= cv_unqualified (ftype
);
1929 int sflags
= (flags
|LOOKUP_NO_CONVERSION
)&~LOOKUP_NO_TEMP_BIND
;
1930 conversion
*new_second
1931 = reference_binding (rto
, ftype
, NULL_TREE
, c_cast_p
,
1935 return merge_conversion_sequences (t
, new_second
);
1939 conv
= build_conv (ck_ref_bind
, rto
, conv
);
1940 /* This reference binding, unlike those above, requires the
1941 creation of a temporary. */
1942 conv
->need_temporary_p
= true;
1943 conv
->rvaluedness_matches_p
= TYPE_REF_IS_RVALUE (rto
);
1947 Otherwise, the reference shall be an lvalue reference to a
1948 non-volatile const type, or the reference shall be an rvalue
1950 if (!CP_TYPE_CONST_NON_VOLATILE_P (to
) && !TYPE_REF_IS_RVALUE (rto
))
1955 Otherwise, a temporary of type "cv1 T1" is created and
1956 initialized from the initializer expression using the rules for a
1957 non-reference copy initialization. If T1 is reference-related to
1958 T2, cv1 must be the same cv-qualification as, or greater
1959 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1960 if (related_p
&& !at_least_as_qualified_p (to
, from
))
1966 /* Returns the implicit conversion sequence (see [over.ics]) from type
1967 FROM to type TO. The optional expression EXPR may affect the
1968 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
1969 true, this conversion is coming from a C-style cast. */
1972 implicit_conversion (tree to
, tree from
, tree expr
, bool c_cast_p
,
1973 int flags
, tsubst_flags_t complain
)
1977 if (from
== error_mark_node
|| to
== error_mark_node
1978 || expr
== error_mark_node
)
1981 /* Other flags only apply to the primary function in overload
1982 resolution, or after we've chosen one. */
1983 flags
&= (LOOKUP_ONLYCONVERTING
|LOOKUP_NO_CONVERSION
|LOOKUP_COPY_PARM
1984 |LOOKUP_NO_TEMP_BIND
|LOOKUP_NO_RVAL_BIND
|LOOKUP_PREFER_RVALUE
1985 |LOOKUP_NO_NARROWING
|LOOKUP_PROTECT
|LOOKUP_NO_NON_INTEGRAL
);
1987 /* FIXME: actually we don't want warnings either, but we can't just
1988 have 'complain &= ~(tf_warning|tf_error)' because it would cause
1989 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
1990 We really ought not to issue that warning until we've committed
1991 to that conversion. */
1992 complain
&= ~tf_error
;
1994 /* Call reshape_init early to remove redundant braces. */
1995 if (expr
&& BRACE_ENCLOSED_INITIALIZER_P (expr
)
1996 && CLASS_TYPE_P (to
)
1997 && COMPLETE_TYPE_P (complete_type (to
))
1998 && !CLASSTYPE_NON_AGGREGATE (to
))
2000 expr
= reshape_init (to
, expr
, complain
);
2001 if (expr
== error_mark_node
)
2003 from
= TREE_TYPE (expr
);
2006 if (TYPE_REF_P (to
))
2007 conv
= reference_binding (to
, from
, expr
, c_cast_p
, flags
, complain
);
2009 conv
= standard_conversion (to
, from
, expr
, c_cast_p
, flags
, complain
);
2014 if (expr
&& BRACE_ENCLOSED_INITIALIZER_P (expr
))
2016 if (is_std_init_list (to
) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr
))
2017 return build_list_conv (to
, expr
, flags
, complain
);
2019 /* As an extension, allow list-initialization of _Complex. */
2020 if (TREE_CODE (to
) == COMPLEX_TYPE
2021 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr
))
2023 conv
= build_complex_conv (to
, expr
, flags
, complain
);
2028 /* Allow conversion from an initializer-list with one element to a
2030 if (SCALAR_TYPE_P (to
))
2032 int nelts
= CONSTRUCTOR_NELTS (expr
);
2036 elt
= build_value_init (to
, tf_none
);
2037 else if (nelts
== 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr
))
2038 elt
= CONSTRUCTOR_ELT (expr
, 0)->value
;
2040 elt
= error_mark_node
;
2042 conv
= implicit_conversion (to
, TREE_TYPE (elt
), elt
,
2043 c_cast_p
, flags
, complain
);
2046 conv
->check_narrowing
= true;
2047 if (BRACE_ENCLOSED_INITIALIZER_P (elt
))
2048 /* Too many levels of braces, i.e. '{{1}}'. */
2053 else if (TREE_CODE (to
) == ARRAY_TYPE
)
2054 return build_array_conv (to
, expr
, flags
, complain
);
2057 if (expr
!= NULL_TREE
2058 && (MAYBE_CLASS_TYPE_P (from
)
2059 || MAYBE_CLASS_TYPE_P (to
))
2060 && (flags
& LOOKUP_NO_CONVERSION
) == 0)
2062 struct z_candidate
*cand
;
2064 if (CLASS_TYPE_P (to
)
2065 && BRACE_ENCLOSED_INITIALIZER_P (expr
)
2066 && !CLASSTYPE_NON_AGGREGATE (complete_type (to
)))
2067 return build_aggr_conv (to
, expr
, flags
, complain
);
2069 cand
= build_user_type_conversion_1 (to
, expr
, flags
, complain
);
2072 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
2073 && CONSTRUCTOR_NELTS (expr
) == 1
2074 && !is_list_ctor (cand
->fn
))
2076 /* "If C is not an initializer-list constructor and the
2077 initializer list has a single element of type cv U, where U is
2078 X or a class derived from X, the implicit conversion sequence
2079 has Exact Match rank if U is X, or Conversion rank if U is
2081 tree elt
= CONSTRUCTOR_ELT (expr
, 0)->value
;
2082 tree elttype
= TREE_TYPE (elt
);
2083 if (reference_related_p (to
, elttype
))
2084 return implicit_conversion (to
, elttype
, elt
,
2085 c_cast_p
, flags
, complain
);
2087 conv
= cand
->second_conv
;
2090 /* We used to try to bind a reference to a temporary here, but that
2091 is now handled after the recursive call to this function at the end
2092 of reference_binding. */
2099 /* Like implicit_conversion, but return NULL if the conversion is bad.
2101 This is not static so that check_non_deducible_conversion can call it within
2102 add_template_candidate_real as part of overload resolution; it should not be
2103 called outside of overload resolution. */
2106 good_conversion (tree to
, tree from
, tree expr
,
2107 int flags
, tsubst_flags_t complain
)
2109 conversion
*c
= implicit_conversion (to
, from
, expr
, /*cast*/false,
2116 /* Add a new entry to the list of candidates. Used by the add_*_candidate
2117 functions. ARGS will not be changed until a single candidate is
2120 static struct z_candidate
*
2121 add_candidate (struct z_candidate
**candidates
,
2122 tree fn
, tree first_arg
, const vec
<tree
, va_gc
> *args
,
2123 size_t num_convs
, conversion
**convs
,
2124 tree access_path
, tree conversion_path
,
2125 int viable
, struct rejection_reason
*reason
,
2128 struct z_candidate
*cand
= (struct z_candidate
*)
2129 conversion_obstack_alloc (sizeof (struct z_candidate
));
2132 cand
->first_arg
= first_arg
;
2134 cand
->convs
= convs
;
2135 cand
->num_convs
= num_convs
;
2136 cand
->access_path
= access_path
;
2137 cand
->conversion_path
= conversion_path
;
2138 cand
->viable
= viable
;
2139 cand
->reason
= reason
;
2140 cand
->next
= *candidates
;
2141 cand
->flags
= flags
;
2144 if (convs
&& cand
->reversed ())
2145 /* Swap the conversions for comparison in joust; we'll swap them back
2146 before build_over_call. */
2147 std::swap (convs
[0], convs
[1]);
2152 /* Return the number of remaining arguments in the parameter list
2153 beginning with ARG. */
2156 remaining_arguments (tree arg
)
2160 for (n
= 0; arg
!= NULL_TREE
&& arg
!= void_list_node
;
2161 arg
= TREE_CHAIN (arg
))
2167 /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2168 to the first parameter of a constructor where the parameter is of type
2169 "reference to possibly cv-qualified T" and the constructor is called with a
2170 single argument in the context of direct-initialization of an object of type
2171 "cv2 T", explicit conversion functions are also considered.
2173 So set LOOKUP_COPY_PARM to let reference_binding know that
2174 it's being called in that context. */
2177 conv_flags (int i
, int nargs
, tree fn
, tree arg
, int flags
)
2181 if (i
== 0 && nargs
== 1 && DECL_CONSTRUCTOR_P (fn
)
2182 && (t
= FUNCTION_FIRST_USER_PARMTYPE (fn
))
2183 && (same_type_ignoring_top_level_qualifiers_p
2184 (non_reference (TREE_VALUE (t
)), DECL_CONTEXT (fn
))))
2186 if (!(flags
& LOOKUP_ONLYCONVERTING
))
2187 lflags
|= LOOKUP_COPY_PARM
;
2188 if ((flags
& LOOKUP_LIST_INIT_CTOR
)
2189 && BRACE_ENCLOSED_INITIALIZER_P (arg
))
2190 lflags
|= LOOKUP_NO_CONVERSION
;
2193 lflags
|= LOOKUP_ONLYCONVERTING
;
2198 /* Create an overload candidate for the function or method FN called
2199 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2200 FLAGS is passed on to implicit_conversion.
2202 This does not change ARGS.
2204 CTYPE, if non-NULL, is the type we want to pretend this function
2205 comes from for purposes of overload resolution. */
2207 static struct z_candidate
*
2208 add_function_candidate (struct z_candidate
**candidates
,
2209 tree fn
, tree ctype
, tree first_arg
,
2210 const vec
<tree
, va_gc
> *args
, tree access_path
,
2211 tree conversion_path
, int flags
,
2213 tsubst_flags_t complain
)
2215 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2218 tree orig_first_arg
= first_arg
;
2221 struct rejection_reason
*reason
= NULL
;
2223 /* At this point we should not see any functions which haven't been
2224 explicitly declared, except for friend functions which will have
2225 been found using argument dependent lookup. */
2226 gcc_assert (!DECL_ANTICIPATED (fn
) || DECL_HIDDEN_FRIEND_P (fn
));
2228 /* The `this', `in_chrg' and VTT arguments to constructors are not
2229 considered in overload resolution. */
2230 if (DECL_CONSTRUCTOR_P (fn
))
2232 if (ctor_omit_inherited_parms (fn
))
2233 /* Bring back parameters omitted from an inherited ctor. */
2234 parmlist
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn
));
2236 parmlist
= skip_artificial_parms_for (fn
, parmlist
);
2237 skip
= num_artificial_parms_for (fn
);
2238 if (skip
> 0 && first_arg
!= NULL_TREE
)
2241 first_arg
= NULL_TREE
;
2247 len
= vec_safe_length (args
) - skip
+ (first_arg
!= NULL_TREE
? 1 : 0);
2249 convs
= alloc_conversions (len
);
2251 /* 13.3.2 - Viable functions [over.match.viable]
2252 First, to be a viable function, a candidate function shall have enough
2253 parameters to agree in number with the arguments in the list.
2255 We need to check this first; otherwise, checking the ICSes might cause
2256 us to produce an ill-formed template instantiation. */
2258 parmnode
= parmlist
;
2259 for (i
= 0; i
< len
; ++i
)
2261 if (parmnode
== NULL_TREE
|| parmnode
== void_list_node
)
2263 parmnode
= TREE_CHAIN (parmnode
);
2266 if ((i
< len
&& parmnode
)
2267 || !sufficient_parms_p (parmnode
))
2269 int remaining
= remaining_arguments (parmnode
);
2271 reason
= arity_rejection (first_arg
, i
+ remaining
, len
);
2274 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2275 parameter of type "reference to cv C" (including such a constructor
2276 instantiated from a template) is excluded from the set of candidate
2277 functions when used to construct an object of type D with an argument list
2278 containing a single argument if C is reference-related to D. */
2279 if (viable
&& len
== 1 && parmlist
&& DECL_CONSTRUCTOR_P (fn
)
2280 && flag_new_inheriting_ctors
2281 && DECL_INHERITED_CTOR (fn
))
2283 tree ptype
= non_reference (TREE_VALUE (parmlist
));
2284 tree dtype
= DECL_CONTEXT (fn
);
2285 tree btype
= DECL_INHERITED_CTOR_BASE (fn
);
2286 if (reference_related_p (ptype
, dtype
)
2287 && reference_related_p (btype
, ptype
))
2290 reason
= inherited_ctor_rejection ();
2294 /* Second, for a function to be viable, its constraints must be
2296 if (flag_concepts
&& viable
&& !constraints_satisfied_p (fn
))
2298 reason
= constraint_failure ();
2302 /* When looking for a function from a subobject from an implicit
2303 copy/move constructor/operator=, don't consider anything that takes (a
2304 reference to) an unrelated type. See c++/44909 and core 1092. */
2305 if (viable
&& parmlist
&& (flags
& LOOKUP_DEFAULTED
))
2307 if (DECL_CONSTRUCTOR_P (fn
))
2309 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
2310 && DECL_OVERLOADED_OPERATOR_IS (fn
, NOP_EXPR
))
2316 parmnode
= chain_index (i
-1, parmlist
);
2317 if (!reference_related_p (non_reference (TREE_VALUE (parmnode
)),
2322 /* This only applies at the top level. */
2323 flags
&= ~LOOKUP_DEFAULTED
;
2329 /* Third, for F to be a viable function, there shall exist for each
2330 argument an implicit conversion sequence that converts that argument
2331 to the corresponding parameter of F. */
2333 parmnode
= parmlist
;
2335 for (i
= 0; i
< len
; ++i
)
2337 tree argtype
, to_type
;
2342 if (parmnode
== void_list_node
)
2347 /* Already set during deduction. */
2348 parmnode
= TREE_CHAIN (parmnode
);
2352 if (i
== 0 && first_arg
!= NULL_TREE
)
2355 arg
= CONST_CAST_TREE (
2356 (*args
)[i
+ skip
- (first_arg
!= NULL_TREE
? 1 : 0)]);
2357 argtype
= lvalue_type (arg
);
2359 is_this
= (i
== 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2360 && ! DECL_CONSTRUCTOR_P (fn
));
2364 tree parmtype
= TREE_VALUE (parmnode
);
2366 parmnode
= TREE_CHAIN (parmnode
);
2368 /* The type of the implicit object parameter ('this') for
2369 overload resolution is not always the same as for the
2370 function itself; conversion functions are considered to
2371 be members of the class being converted, and functions
2372 introduced by a using-declaration are considered to be
2373 members of the class that uses them.
2375 Since build_over_call ignores the ICS for the `this'
2376 parameter, we can just change the parm type. */
2377 if (ctype
&& is_this
)
2379 parmtype
= cp_build_qualified_type
2380 (ctype
, cp_type_quals (TREE_TYPE (parmtype
)));
2381 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn
)))
2383 /* If the function has a ref-qualifier, the implicit
2384 object parameter has reference type. */
2385 bool rv
= FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn
));
2386 parmtype
= cp_build_reference_type (parmtype
, rv
);
2387 /* The special handling of 'this' conversions in compare_ics
2388 does not apply if there is a ref-qualifier. */
2393 parmtype
= build_pointer_type (parmtype
);
2394 /* We don't use build_this here because we don't want to
2395 capture the object argument until we've chosen a
2396 non-static member function. */
2397 arg
= build_address (arg
);
2398 argtype
= lvalue_type (arg
);
2402 int lflags
= conv_flags (i
, len
-skip
, fn
, arg
, flags
);
2404 t
= implicit_conversion (parmtype
, argtype
, arg
,
2405 /*c_cast_p=*/false, lflags
, complain
);
2410 t
= build_identity_conv (argtype
, arg
);
2411 t
->ellipsis_p
= true;
2422 reason
= arg_conversion_rejection (first_arg
, i
, argtype
, to_type
,
2423 EXPR_LOCATION (arg
));
2430 reason
= bad_arg_conversion_rejection (first_arg
, i
, arg
, to_type
,
2431 EXPR_LOCATION (arg
));
2437 return add_candidate (candidates
, fn
, orig_first_arg
, args
, len
, convs
,
2438 access_path
, conversion_path
, viable
, reason
, flags
);
2441 /* Create an overload candidate for the conversion function FN which will
2442 be invoked for expression OBJ, producing a pointer-to-function which
2443 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2444 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2445 passed on to implicit_conversion.
2447 Actually, we don't really care about FN; we care about the type it
2448 converts to. There may be multiple conversion functions that will
2449 convert to that type, and we rely on build_user_type_conversion_1 to
2450 choose the best one; so when we create our candidate, we record the type
2451 instead of the function. */
2453 static struct z_candidate
*
2454 add_conv_candidate (struct z_candidate
**candidates
, tree fn
, tree obj
,
2455 const vec
<tree
, va_gc
> *arglist
,
2456 tree access_path
, tree conversion_path
,
2457 tsubst_flags_t complain
)
2459 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
2460 int i
, len
, viable
, flags
;
2461 tree parmlist
, parmnode
;
2463 struct rejection_reason
*reason
;
2465 for (parmlist
= totype
; TREE_CODE (parmlist
) != FUNCTION_TYPE
; )
2466 parmlist
= TREE_TYPE (parmlist
);
2467 parmlist
= TYPE_ARG_TYPES (parmlist
);
2469 len
= vec_safe_length (arglist
) + 1;
2470 convs
= alloc_conversions (len
);
2471 parmnode
= parmlist
;
2473 flags
= LOOKUP_IMPLICIT
;
2476 /* Don't bother looking up the same type twice. */
2477 if (*candidates
&& (*candidates
)->fn
== totype
)
2480 for (i
= 0; i
< len
; ++i
)
2482 tree arg
, argtype
, convert_type
= NULL_TREE
;
2488 arg
= (*arglist
)[i
- 1];
2489 argtype
= lvalue_type (arg
);
2493 t
= build_identity_conv (argtype
, NULL_TREE
);
2494 t
= build_conv (ck_user
, totype
, t
);
2495 /* Leave the 'cand' field null; we'll figure out the conversion in
2496 convert_like if this candidate is chosen. */
2497 convert_type
= totype
;
2499 else if (parmnode
== void_list_node
)
2503 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
,
2504 /*c_cast_p=*/false, flags
, complain
);
2505 convert_type
= TREE_VALUE (parmnode
);
2509 t
= build_identity_conv (argtype
, arg
);
2510 t
->ellipsis_p
= true;
2511 convert_type
= argtype
;
2521 reason
= bad_arg_conversion_rejection (NULL_TREE
, i
, arg
, convert_type
,
2522 EXPR_LOCATION (arg
));
2529 parmnode
= TREE_CHAIN (parmnode
);
2533 || ! sufficient_parms_p (parmnode
))
2535 int remaining
= remaining_arguments (parmnode
);
2537 reason
= arity_rejection (NULL_TREE
, i
+ remaining
, len
);
2540 return add_candidate (candidates
, totype
, obj
, arglist
, len
, convs
,
2541 access_path
, conversion_path
, viable
, reason
, flags
);
2545 build_builtin_candidate (struct z_candidate
**candidates
, tree fnname
,
2546 tree type1
, tree type2
, const vec
<tree
,va_gc
> &args
,
2547 tree
*argtypes
, int flags
, tsubst_flags_t complain
)
2554 struct rejection_reason
*reason
= NULL
;
2559 num_convs
= args
.length ();
2560 convs
= alloc_conversions (num_convs
);
2562 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2563 conversion ops are allowed. We handle that here by just checking for
2564 boolean_type_node because other operators don't ask for it. COND_EXPR
2565 also does contextual conversion to bool for the first operand, but we
2566 handle that in build_conditional_expr, and type1 here is operand 2. */
2567 if (type1
!= boolean_type_node
)
2568 flags
|= LOOKUP_ONLYCONVERTING
;
2570 for (unsigned i
= 0; i
< 2 && i
< num_convs
; ++i
)
2572 t
= implicit_conversion (types
[i
], argtypes
[i
], args
[i
],
2573 /*c_cast_p=*/false, flags
, complain
);
2577 /* We need something for printing the candidate. */
2578 t
= build_identity_conv (types
[i
], NULL_TREE
);
2579 reason
= arg_conversion_rejection (NULL_TREE
, i
, argtypes
[i
],
2580 types
[i
], EXPR_LOCATION (args
[i
]));
2585 reason
= bad_arg_conversion_rejection (NULL_TREE
, i
, args
[i
],
2587 EXPR_LOCATION (args
[i
]));
2592 /* For COND_EXPR we rearranged the arguments; undo that now. */
2595 convs
[2] = convs
[1];
2596 convs
[1] = convs
[0];
2597 t
= implicit_conversion (boolean_type_node
, argtypes
[2], args
[2],
2598 /*c_cast_p=*/false, flags
,
2605 reason
= arg_conversion_rejection (NULL_TREE
, 0, argtypes
[2],
2607 EXPR_LOCATION (args
[2]));
2611 add_candidate (candidates
, fnname
, /*first_arg=*/NULL_TREE
, /*args=*/NULL
,
2613 /*access_path=*/NULL_TREE
,
2614 /*conversion_path=*/NULL_TREE
,
2615 viable
, reason
, flags
);
2619 is_complete (tree t
)
2621 return COMPLETE_TYPE_P (complete_type (t
));
2624 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2627 promoted_arithmetic_type_p (tree type
)
2631 In this section, the term promoted integral type is used to refer
2632 to those integral types which are preserved by integral promotion
2633 (including e.g. int and long but excluding e.g. char).
2634 Similarly, the term promoted arithmetic type refers to promoted
2635 integral types plus floating types. */
2636 return ((CP_INTEGRAL_TYPE_P (type
)
2637 && same_type_p (type_promotes_to (type
), type
))
2638 || TREE_CODE (type
) == REAL_TYPE
);
2641 /* Create any builtin operator overload candidates for the operator in
2642 question given the converted operand types TYPE1 and TYPE2. The other
2643 args are passed through from add_builtin_candidates to
2644 build_builtin_candidate.
2646 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2647 If CODE is requires candidates operands of the same type of the kind
2648 of which TYPE1 and TYPE2 are, we add both candidates
2649 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2652 add_builtin_candidate (struct z_candidate
**candidates
, enum tree_code code
,
2653 enum tree_code code2
, tree fnname
, tree type1
,
2654 tree type2
, vec
<tree
,va_gc
> &args
, tree
*argtypes
,
2655 int flags
, tsubst_flags_t complain
)
2659 case POSTINCREMENT_EXPR
:
2660 case POSTDECREMENT_EXPR
:
2661 args
[1] = integer_zero_node
;
2662 type2
= integer_type_node
;
2671 /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2672 and VQ is either volatile or empty, there exist candidate operator
2673 functions of the form
2674 VQ T& operator++(VQ T&);
2675 T operator++(VQ T&, int);
2676 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2677 and VQ is either volatile or empty, there exist candidate operator
2678 functions of the form
2679 VQ T& operator--(VQ T&);
2680 T operator--(VQ T&, int);
2681 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2682 type, and VQ is either volatile or empty, there exist candidate operator
2683 functions of the form
2684 T*VQ& operator++(T*VQ&);
2685 T*VQ& operator--(T*VQ&);
2686 T* operator++(T*VQ&, int);
2687 T* operator--(T*VQ&, int); */
2689 case POSTDECREMENT_EXPR
:
2690 case PREDECREMENT_EXPR
:
2691 if (TREE_CODE (type1
) == BOOLEAN_TYPE
)
2694 case POSTINCREMENT_EXPR
:
2695 case PREINCREMENT_EXPR
:
2696 /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2698 if (TREE_CODE (type1
) == BOOLEAN_TYPE
&& cxx_dialect
>= cxx17
)
2700 if (ARITHMETIC_TYPE_P (type1
) || TYPE_PTROB_P (type1
))
2702 type1
= build_reference_type (type1
);
2707 /* 7 For every cv-qualified or cv-unqualified object type T, there
2708 exist candidate operator functions of the form
2713 8 For every function type T that does not have cv-qualifiers or
2714 a ref-qualifier, there exist candidate operator functions of the form
2715 T& operator*(T*); */
2718 if (TYPE_PTR_P (type1
)
2719 && (TYPE_PTROB_P (type1
)
2720 || TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
))
2724 /* 9 For every type T, there exist candidate operator functions of the form
2727 10 For every floating-point or promoted integral type T, there exist
2728 candidate operator functions of the form
2732 case UNARY_PLUS_EXPR
: /* unary + */
2733 if (TYPE_PTR_P (type1
))
2737 if (ARITHMETIC_TYPE_P (type1
))
2741 /* 11 For every promoted integral type T, there exist candidate operator
2742 functions of the form
2746 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
))
2750 /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
2751 is the same type as C2 or is a derived class of C2, and T is an object
2752 type or a function type there exist candidate operator functions of the
2754 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2755 where CV12 is the union of CV1 and CV2. */
2758 if (TYPE_PTR_P (type1
) && TYPE_PTRMEM_P (type2
))
2760 tree c1
= TREE_TYPE (type1
);
2761 tree c2
= TYPE_PTRMEM_CLASS_TYPE (type2
);
2763 if (MAYBE_CLASS_TYPE_P (c1
) && DERIVED_FROM_P (c2
, c1
)
2764 && (TYPE_PTRMEMFUNC_P (type2
)
2765 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2
))))
2770 /* 13 For every pair of types L and R, where each of L and R is a floating-point
2771 or promoted integral type, there exist candidate operator functions of the
2777 bool operator<(L, R);
2778 bool operator>(L, R);
2779 bool operator<=(L, R);
2780 bool operator>=(L, R);
2781 bool operator==(L, R);
2782 bool operator!=(L, R);
2783 where LR is the result of the usual arithmetic conversions between
2786 14 For every integral type T there exists a candidate operator function of
2789 std::strong_ordering operator<=>(T, T);
2791 15 For every pair of floating-point types L and R, there exists a candidate
2792 operator function of the form
2794 std::partial_ordering operator<=>(L, R);
2796 16 For every cv-qualified or cv-unqualified object type T there exist
2797 candidate operator functions of the form
2798 T* operator+(T*, std::ptrdiff_t);
2799 T& operator[](T*, std::ptrdiff_t);
2800 T* operator-(T*, std::ptrdiff_t);
2801 T* operator+(std::ptrdiff_t, T*);
2802 T& operator[](std::ptrdiff_t, T*);
2804 17 For every T, where T is a pointer to object type, there exist candidate
2805 operator functions of the form
2806 std::ptrdiff_t operator-(T, T);
2808 18 For every T, where T is an enumeration type or a pointer type, there
2809 exist candidate operator functions of the form
2810 bool operator<(T, T);
2811 bool operator>(T, T);
2812 bool operator<=(T, T);
2813 bool operator>=(T, T);
2814 bool operator==(T, T);
2815 bool operator!=(T, T);
2816 R operator<=>(T, T);
2818 where R is the result type specified in [expr.spaceship].
2820 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
2821 there exist candidate operator functions of the form
2822 bool operator==(T, T);
2823 bool operator!=(T, T); */
2826 if (TYPE_PTROB_P (type1
) && TYPE_PTROB_P (type2
))
2828 if (TYPE_PTROB_P (type1
)
2829 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2831 type2
= ptrdiff_type_node
;
2836 case TRUNC_DIV_EXPR
:
2837 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2841 /* This isn't exactly what's specified above for operator<=>, but it's
2842 close enough. In particular, we don't care about the return type
2843 specified above; it doesn't participate in overload resolution and it
2844 doesn't affect the semantics of the built-in operator. */
2845 case SPACESHIP_EXPR
:
2848 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
2849 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
)))
2851 if (NULLPTR_TYPE_P (type1
) && NULLPTR_TYPE_P (type2
))
2853 if (TYPE_PTRMEM_P (type1
) && null_ptr_cst_p (args
[1]))
2858 if (TYPE_PTRMEM_P (type2
) && null_ptr_cst_p (args
[0]))
2870 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2872 if (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
2874 if (TREE_CODE (type1
) == ENUMERAL_TYPE
2875 && TREE_CODE (type2
) == ENUMERAL_TYPE
)
2877 if (TYPE_PTR_P (type1
)
2878 && null_ptr_cst_p (args
[1]))
2883 if (null_ptr_cst_p (args
[0])
2884 && TYPE_PTR_P (type2
))
2892 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2896 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
) && TYPE_PTROB_P (type2
))
2898 type1
= ptrdiff_type_node
;
2901 if (TYPE_PTROB_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2903 type2
= ptrdiff_type_node
;
2908 /* 18For every pair of promoted integral types L and R, there exist candi-
2909 date operator functions of the form
2916 where LR is the result of the usual arithmetic conversions between
2919 case TRUNC_MOD_EXPR
:
2925 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2929 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2930 type, VQ is either volatile or empty, and R is a promoted arithmetic
2931 type, there exist candidate operator functions of the form
2932 VQ L& operator=(VQ L&, R);
2933 VQ L& operator*=(VQ L&, R);
2934 VQ L& operator/=(VQ L&, R);
2935 VQ L& operator+=(VQ L&, R);
2936 VQ L& operator-=(VQ L&, R);
2938 20For every pair T, VQ), where T is any type and VQ is either volatile
2939 or empty, there exist candidate operator functions of the form
2940 T*VQ& operator=(T*VQ&, T*);
2942 21For every pair T, VQ), where T is a pointer to member type and VQ is
2943 either volatile or empty, there exist candidate operator functions of
2945 VQ T& operator=(VQ T&, T);
2947 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2948 unqualified complete object type, VQ is either volatile or empty, and
2949 I is a promoted integral type, there exist candidate operator func-
2951 T*VQ& operator+=(T*VQ&, I);
2952 T*VQ& operator-=(T*VQ&, I);
2954 23For every triple L, VQ, R), where L is an integral or enumeration
2955 type, VQ is either volatile or empty, and R is a promoted integral
2956 type, there exist candidate operator functions of the form
2958 VQ L& operator%=(VQ L&, R);
2959 VQ L& operator<<=(VQ L&, R);
2960 VQ L& operator>>=(VQ L&, R);
2961 VQ L& operator&=(VQ L&, R);
2962 VQ L& operator^=(VQ L&, R);
2963 VQ L& operator|=(VQ L&, R); */
2970 if (TYPE_PTROB_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2972 type2
= ptrdiff_type_node
;
2977 case TRUNC_DIV_EXPR
:
2978 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2982 case TRUNC_MOD_EXPR
:
2988 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2993 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2995 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
2996 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
2997 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
2998 || ((TYPE_PTRMEMFUNC_P (type1
)
2999 || TYPE_PTR_P (type1
))
3000 && null_ptr_cst_p (args
[1])))
3010 type1
= build_reference_type (type1
);
3016 For every pair of promoted arithmetic types L and R, there
3017 exist candidate operator functions of the form
3019 LR operator?(bool, L, R);
3021 where LR is the result of the usual arithmetic conversions
3022 between types L and R.
3024 For every type T, where T is a pointer or pointer-to-member
3025 type, there exist candidate operator functions of the form T
3026 operator?(bool, T, T); */
3028 if (promoted_arithmetic_type_p (type1
)
3029 && promoted_arithmetic_type_p (type2
))
3033 /* Otherwise, the types should be pointers. */
3034 if (!TYPE_PTR_OR_PTRMEM_P (type1
) || !TYPE_PTR_OR_PTRMEM_P (type2
))
3037 /* We don't check that the two types are the same; the logic
3038 below will actually create two candidates; one in which both
3039 parameter types are TYPE1, and one in which both parameter
3045 if (ARITHMETIC_TYPE_P (type1
))
3053 /* Make sure we don't create builtin candidates with dependent types. */
3054 bool u1
= uses_template_parms (type1
);
3055 bool u2
= type2
? uses_template_parms (type2
) : false;
3058 /* Try to recover if one of the types is non-dependent. But if
3059 there's only one type, there's nothing we can do. */
3062 /* And we lose if both are dependent. */
3065 /* Or if they have different forms. */
3066 if (TREE_CODE (type1
) != TREE_CODE (type2
))
3075 /* If we're dealing with two pointer types or two enumeral types,
3076 we need candidates for both of them. */
3077 if (type2
&& !same_type_p (type1
, type2
)
3078 && TREE_CODE (type1
) == TREE_CODE (type2
)
3079 && (TYPE_REF_P (type1
)
3080 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
3081 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
3082 || TYPE_PTRMEMFUNC_P (type1
)
3083 || MAYBE_CLASS_TYPE_P (type1
)
3084 || TREE_CODE (type1
) == ENUMERAL_TYPE
))
3086 if (TYPE_PTR_OR_PTRMEM_P (type1
))
3088 tree cptype
= composite_pointer_type (input_location
,
3094 if (cptype
!= error_mark_node
)
3096 build_builtin_candidate
3097 (candidates
, fnname
, cptype
, cptype
, args
, argtypes
,
3103 build_builtin_candidate
3104 (candidates
, fnname
, type1
, type1
, args
, argtypes
, flags
, complain
);
3105 build_builtin_candidate
3106 (candidates
, fnname
, type2
, type2
, args
, argtypes
, flags
, complain
);
3110 build_builtin_candidate
3111 (candidates
, fnname
, type1
, type2
, args
, argtypes
, flags
, complain
);
3115 type_decays_to (tree type
)
3117 if (TREE_CODE (type
) == ARRAY_TYPE
)
3118 return build_pointer_type (TREE_TYPE (type
));
3119 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3120 return build_pointer_type (type
);
3124 /* There are three conditions of builtin candidates:
3126 1) bool-taking candidates. These are the same regardless of the input.
3127 2) pointer-pair taking candidates. These are generated for each type
3128 one of the input types converts to.
3129 3) arithmetic candidates. According to the standard, we should generate
3130 all of these, but I'm trying not to...
3132 Here we generate a superset of the possible candidates for this particular
3133 case. That is a subset of the full set the standard defines, plus some
3134 other cases which the standard disallows. add_builtin_candidate will
3135 filter out the invalid set. */
3138 add_builtin_candidates (struct z_candidate
**candidates
, enum tree_code code
,
3139 enum tree_code code2
, tree fnname
,
3140 vec
<tree
, va_gc
> *argv
,
3141 int flags
, tsubst_flags_t complain
)
3145 tree type
, argtypes
[3], t
;
3146 /* TYPES[i] is the set of possible builtin-operator parameter types
3147 we will consider for the Ith argument. */
3148 vec
<tree
, va_gc
> *types
[2];
3150 vec
<tree
, va_gc
> &args
= *argv
;
3151 unsigned len
= args
.length ();
3153 for (unsigned i
= 0; i
< len
; ++i
)
3156 argtypes
[i
] = unlowered_expr_type (args
[i
]);
3158 argtypes
[i
] = NULL_TREE
;
3163 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3164 and VQ is either volatile or empty, there exist candidate operator
3165 functions of the form
3166 VQ T& operator++(VQ T&); */
3168 case POSTINCREMENT_EXPR
:
3169 case PREINCREMENT_EXPR
:
3170 case POSTDECREMENT_EXPR
:
3171 case PREDECREMENT_EXPR
:
3176 /* 24There also exist candidate operator functions of the form
3177 bool operator!(bool);
3178 bool operator&&(bool, bool);
3179 bool operator||(bool, bool); */
3181 case TRUTH_NOT_EXPR
:
3182 build_builtin_candidate
3183 (candidates
, fnname
, boolean_type_node
,
3184 NULL_TREE
, args
, argtypes
, flags
, complain
);
3187 case TRUTH_ORIF_EXPR
:
3188 case TRUTH_ANDIF_EXPR
:
3189 build_builtin_candidate
3190 (candidates
, fnname
, boolean_type_node
,
3191 boolean_type_node
, args
, argtypes
, flags
, complain
);
3207 case SPACESHIP_EXPR
:
3215 types
[0] = make_tree_vector ();
3216 types
[1] = make_tree_vector ();
3220 for (unsigned i
= 0; i
< len
; ++i
)
3222 if (MAYBE_CLASS_TYPE_P (argtypes
[i
]))
3226 if (i
== 0 && code
== MODIFY_EXPR
&& code2
== NOP_EXPR
)
3229 convs
= lookup_conversions (argtypes
[i
]);
3231 if (code
== COND_EXPR
)
3233 if (lvalue_p (args
[i
]))
3234 vec_safe_push (types
[i
], build_reference_type (argtypes
[i
]));
3236 vec_safe_push (types
[i
], TYPE_MAIN_VARIANT (argtypes
[i
]));
3242 for (; convs
; convs
= TREE_CHAIN (convs
))
3244 type
= TREE_TYPE (convs
);
3247 && (!TYPE_REF_P (type
)
3248 || CP_TYPE_CONST_P (TREE_TYPE (type
))))
3251 if (code
== COND_EXPR
&& TYPE_REF_P (type
))
3252 vec_safe_push (types
[i
], type
);
3254 type
= non_reference (type
);
3255 if (i
!= 0 || ! ref1
)
3257 type
= cv_unqualified (type_decays_to (type
));
3258 if (enum_p
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
3259 vec_safe_push (types
[i
], type
);
3260 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
3261 type
= type_promotes_to (type
);
3264 if (! vec_member (type
, types
[i
]))
3265 vec_safe_push (types
[i
], type
);
3270 if (code
== COND_EXPR
&& lvalue_p (args
[i
]))
3271 vec_safe_push (types
[i
], build_reference_type (argtypes
[i
]));
3272 type
= non_reference (argtypes
[i
]);
3273 if (i
!= 0 || ! ref1
)
3275 type
= cv_unqualified (type_decays_to (type
));
3276 if (enum_p
&& UNSCOPED_ENUM_P (type
))
3277 vec_safe_push (types
[i
], type
);
3278 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
3279 type
= type_promotes_to (type
);
3281 vec_safe_push (types
[i
], type
);
3285 /* Run through the possible parameter types of both arguments,
3286 creating candidates with those parameter types. */
3287 FOR_EACH_VEC_ELT_REVERSE (*(types
[0]), ix
, t
)
3292 if (!types
[1]->is_empty ())
3293 FOR_EACH_VEC_ELT_REVERSE (*(types
[1]), jx
, u
)
3294 add_builtin_candidate
3295 (candidates
, code
, code2
, fnname
, t
,
3296 u
, args
, argtypes
, flags
, complain
);
3298 add_builtin_candidate
3299 (candidates
, code
, code2
, fnname
, t
,
3300 NULL_TREE
, args
, argtypes
, flags
, complain
);
3303 release_tree_vector (types
[0]);
3304 release_tree_vector (types
[1]);
3308 /* If TMPL can be successfully instantiated as indicated by
3309 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3311 TMPL is the template. EXPLICIT_TARGS are any explicit template
3312 arguments. ARGLIST is the arguments provided at the call-site.
3313 This does not change ARGLIST. The RETURN_TYPE is the desired type
3314 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3315 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3316 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
3318 static struct z_candidate
*
3319 add_template_candidate_real (struct z_candidate
**candidates
, tree tmpl
,
3320 tree ctype
, tree explicit_targs
, tree first_arg
,
3321 const vec
<tree
, va_gc
> *arglist
, tree return_type
,
3322 tree access_path
, tree conversion_path
,
3323 int flags
, tree obj
, unification_kind_t strict
,
3324 tsubst_flags_t complain
)
3326 int ntparms
= DECL_NTPARMS (tmpl
);
3327 tree targs
= make_tree_vec (ntparms
);
3328 unsigned int len
= vec_safe_length (arglist
);
3329 unsigned int nargs
= (first_arg
== NULL_TREE
? 0 : 1) + len
;
3330 unsigned int skip_without_in_chrg
= 0;
3331 tree first_arg_without_in_chrg
= first_arg
;
3332 tree
*args_without_in_chrg
;
3333 unsigned int nargs_without_in_chrg
;
3334 unsigned int ia
, ix
;
3336 struct z_candidate
*cand
;
3338 struct rejection_reason
*reason
= NULL
;
3340 conversion
**convs
= NULL
;
3342 /* We don't do deduction on the in-charge parameter, the VTT
3343 parameter or 'this'. */
3344 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl
))
3346 if (first_arg_without_in_chrg
!= NULL_TREE
)
3347 first_arg_without_in_chrg
= NULL_TREE
;
3348 else if (return_type
&& strict
== DEDUCE_CALL
)
3349 /* We're deducing for a call to the result of a template conversion
3350 function, so the args don't contain 'this'; leave them alone. */;
3352 ++skip_without_in_chrg
;
3355 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl
)
3356 || DECL_BASE_CONSTRUCTOR_P (tmpl
))
3357 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl
)))
3359 if (first_arg_without_in_chrg
!= NULL_TREE
)
3360 first_arg_without_in_chrg
= NULL_TREE
;
3362 ++skip_without_in_chrg
;
3365 if (len
< skip_without_in_chrg
)
3368 if (DECL_CONSTRUCTOR_P (tmpl
) && nargs
== 2
3369 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg
),
3370 TREE_TYPE ((*arglist
)[0])))
3372 /* 12.8/6 says, "A declaration of a constructor for a class X is
3373 ill-formed if its first parameter is of type (optionally cv-qualified)
3374 X and either there are no other parameters or else all other
3375 parameters have default arguments. A member function template is never
3376 instantiated to produce such a constructor signature."
3378 So if we're trying to copy an object of the containing class, don't
3379 consider a template constructor that has a first parameter type that
3380 is just a template parameter, as we would deduce a signature that we
3381 would then reject in the code below. */
3382 if (tree firstparm
= FUNCTION_FIRST_USER_PARMTYPE (tmpl
))
3384 firstparm
= TREE_VALUE (firstparm
);
3385 if (PACK_EXPANSION_P (firstparm
))
3386 firstparm
= PACK_EXPANSION_PATTERN (firstparm
);
3387 if (TREE_CODE (firstparm
) == TEMPLATE_TYPE_PARM
)
3389 gcc_assert (!explicit_targs
);
3390 reason
= invalid_copy_with_fn_template_rejection ();
3396 nargs_without_in_chrg
= ((first_arg_without_in_chrg
!= NULL_TREE
? 1 : 0)
3397 + (len
- skip_without_in_chrg
));
3398 args_without_in_chrg
= XALLOCAVEC (tree
, nargs_without_in_chrg
);
3400 if (first_arg_without_in_chrg
!= NULL_TREE
)
3402 args_without_in_chrg
[ia
] = first_arg_without_in_chrg
;
3405 for (ix
= skip_without_in_chrg
;
3406 vec_safe_iterate (arglist
, ix
, &arg
);
3409 args_without_in_chrg
[ia
] = arg
;
3412 gcc_assert (ia
== nargs_without_in_chrg
);
3414 errs
= errorcount
+sorrycount
;
3416 convs
= alloc_conversions (nargs
);
3417 fn
= fn_type_unification (tmpl
, explicit_targs
, targs
,
3418 args_without_in_chrg
,
3419 nargs_without_in_chrg
,
3420 return_type
, strict
, flags
, convs
,
3421 false, complain
& tf_decltype
);
3423 if (fn
== error_mark_node
)
3425 /* Don't repeat unification later if it already resulted in errors. */
3426 if (errorcount
+sorrycount
== errs
)
3427 reason
= template_unification_rejection (tmpl
, explicit_targs
,
3428 targs
, args_without_in_chrg
,
3429 nargs_without_in_chrg
,
3430 return_type
, strict
, flags
);
3432 reason
= template_unification_error_rejection ();
3436 /* Now the explicit specifier might have been deduced; check if this
3437 declaration is explicit. If it is and we're ignoring non-converting
3438 constructors, don't add this function to the set of candidates. */
3439 if ((flags
& LOOKUP_ONLYCONVERTING
) && DECL_NONCONVERTING_P (fn
))
3442 if (DECL_CONSTRUCTOR_P (fn
) && nargs
== 2)
3444 tree arg_types
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
3445 if (arg_types
&& same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types
)),
3448 /* We're trying to produce a constructor with a prohibited signature,
3449 as discussed above; handle here any cases we didn't catch then,
3451 reason
= invalid_copy_with_fn_template_rejection ();
3456 if (obj
!= NULL_TREE
)
3457 /* Aha, this is a conversion function. */
3458 cand
= add_conv_candidate (candidates
, fn
, obj
, arglist
,
3459 access_path
, conversion_path
, complain
);
3461 cand
= add_function_candidate (candidates
, fn
, ctype
,
3462 first_arg
, arglist
, access_path
,
3463 conversion_path
, flags
, convs
, complain
);
3464 if (DECL_TI_TEMPLATE (fn
) != tmpl
)
3465 /* This situation can occur if a member template of a template
3466 class is specialized. Then, instantiate_template might return
3467 an instantiation of the specialization, in which case the
3468 DECL_TI_TEMPLATE field will point at the original
3469 specialization. For example:
3471 template <class T> struct S { template <class U> void f(U);
3472 template <> void f(int) {}; };
3476 Here, TMPL will be template <class U> S<double>::f(U).
3477 And, instantiate template will give us the specialization
3478 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3479 for this will point at template <class T> template <> S<T>::f(int),
3480 so that we can find the definition. For the purposes of
3481 overload resolution, however, we want the original TMPL. */
3482 cand
->template_decl
= build_template_info (tmpl
, targs
);
3484 cand
->template_decl
= DECL_TEMPLATE_INFO (fn
);
3485 cand
->explicit_targs
= explicit_targs
;
3489 return add_candidate (candidates
, tmpl
, first_arg
, arglist
, nargs
, NULL
,
3490 access_path
, conversion_path
, 0, reason
, flags
);
3494 static struct z_candidate
*
3495 add_template_candidate (struct z_candidate
**candidates
, tree tmpl
, tree ctype
,
3496 tree explicit_targs
, tree first_arg
,
3497 const vec
<tree
, va_gc
> *arglist
, tree return_type
,
3498 tree access_path
, tree conversion_path
, int flags
,
3499 unification_kind_t strict
, tsubst_flags_t complain
)
3502 add_template_candidate_real (candidates
, tmpl
, ctype
,
3503 explicit_targs
, first_arg
, arglist
,
3504 return_type
, access_path
, conversion_path
,
3505 flags
, NULL_TREE
, strict
, complain
);
3508 /* Create an overload candidate for the conversion function template TMPL,
3509 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3510 pointer-to-function which will in turn be called with the argument list
3511 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3512 passed on to implicit_conversion. */
3514 static struct z_candidate
*
3515 add_template_conv_candidate (struct z_candidate
**candidates
, tree tmpl
,
3517 const vec
<tree
, va_gc
> *arglist
,
3518 tree return_type
, tree access_path
,
3519 tree conversion_path
, tsubst_flags_t complain
)
3521 /* Making this work broke PR 71117 and 85118, so until the committee resolves
3522 core issue 2189, let's disable this candidate if there are any call
3528 add_template_candidate_real (candidates
, tmpl
, NULL_TREE
, NULL_TREE
,
3529 NULL_TREE
, arglist
, return_type
, access_path
,
3530 conversion_path
, 0, obj
, DEDUCE_CALL
,
3534 /* The CANDS are the set of candidates that were considered for
3535 overload resolution. Return the set of viable candidates, or CANDS
3536 if none are viable. If any of the candidates were viable, set
3537 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3538 considered viable only if it is strictly viable. */
3540 static struct z_candidate
*
3541 splice_viable (struct z_candidate
*cands
,
3545 struct z_candidate
*viable
;
3546 struct z_candidate
**last_viable
;
3547 struct z_candidate
**cand
;
3548 bool found_strictly_viable
= false;
3550 /* Be strict inside templates, since build_over_call won't actually
3551 do the conversions to get pedwarns. */
3552 if (processing_template_decl
)
3556 last_viable
= &viable
;
3557 *any_viable_p
= false;
3562 struct z_candidate
*c
= *cand
;
3564 && (c
->viable
== 1 || TREE_CODE (c
->fn
) == TEMPLATE_DECL
))
3566 /* Be strict in the presence of a viable candidate. Also if
3567 there are template candidates, so that we get deduction errors
3568 for them instead of silently preferring a bad conversion. */
3570 if (viable
&& !found_strictly_viable
)
3572 /* Put any spliced near matches back onto the main list so
3573 that we see them if there is no strict match. */
3574 *any_viable_p
= false;
3575 *last_viable
= cands
;
3578 last_viable
= &viable
;
3582 if (strict_p
? c
->viable
== 1 : c
->viable
)
3587 last_viable
= &c
->next
;
3588 *any_viable_p
= true;
3590 found_strictly_viable
= true;
3596 return viable
? viable
: cands
;
3600 any_strictly_viable (struct z_candidate
*cands
)
3602 for (; cands
; cands
= cands
->next
)
3603 if (cands
->viable
== 1)
3608 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3609 words, it is about to become the "this" pointer for a member
3610 function call. Take the address of the object. */
3613 build_this (tree obj
)
3615 /* In a template, we are only concerned about the type of the
3616 expression, so we can take a shortcut. */
3617 if (processing_template_decl
)
3618 return build_address (obj
);
3620 return cp_build_addr_expr (obj
, tf_warning_or_error
);
3623 /* Returns true iff functions are equivalent. Equivalent functions are
3624 not '==' only if one is a function-local extern function or if
3625 both are extern "C". */
3628 equal_functions (tree fn1
, tree fn2
)
3630 if (TREE_CODE (fn1
) != TREE_CODE (fn2
))
3632 if (TREE_CODE (fn1
) == TEMPLATE_DECL
)
3634 if (DECL_LOCAL_FUNCTION_P (fn1
) || DECL_LOCAL_FUNCTION_P (fn2
)
3635 || DECL_EXTERN_C_FUNCTION_P (fn1
))
3636 return decls_match (fn1
, fn2
);
3640 /* Print information about a candidate FN being rejected due to INFO. */
3643 print_conversion_rejection (location_t loc
, struct conversion_info
*info
,
3646 tree from
= info
->from
;
3648 from
= lvalue_type (from
);
3649 if (info
->n_arg
== -1)
3651 /* Conversion of implicit `this' argument failed. */
3652 if (!TYPE_P (info
->from
))
3653 /* A bad conversion for 'this' must be discarding cv-quals. */
3654 inform (loc
, " passing %qT as %<this%> "
3655 "argument discards qualifiers",
3658 inform (loc
, " no known conversion for implicit "
3659 "%<this%> parameter from %qH to %qI",
3660 from
, info
->to_type
);
3662 else if (!TYPE_P (info
->from
))
3664 if (info
->n_arg
>= 0)
3665 inform (loc
, " conversion of argument %d would be ill-formed:",
3667 perform_implicit_conversion (info
->to_type
, info
->from
,
3668 tf_warning_or_error
);
3670 else if (info
->n_arg
== -2)
3671 /* Conversion of conversion function return value failed. */
3672 inform (loc
, " no known conversion from %qH to %qI",
3673 from
, info
->to_type
);
3676 if (TREE_CODE (fn
) == FUNCTION_DECL
)
3677 loc
= get_fndecl_argument_location (fn
, info
->n_arg
);
3678 inform (loc
, " no known conversion for argument %d from %qH to %qI",
3679 info
->n_arg
+ 1, from
, info
->to_type
);
3683 /* Print information about a candidate with WANT parameters and we found
3687 print_arity_information (location_t loc
, unsigned int have
, unsigned int want
)
3689 inform_n (loc
, want
,
3690 " candidate expects %d argument, %d provided",
3691 " candidate expects %d arguments, %d provided",
3695 /* Print information about one overload candidate CANDIDATE. MSGSTR
3696 is the text to print before the candidate itself.
3698 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3699 to have been run through gettext by the caller. This wart makes
3700 life simpler in print_z_candidates and for the translators. */
3703 print_z_candidate (location_t loc
, const char *msgstr
,
3704 struct z_candidate
*candidate
)
3706 const char *msg
= (msgstr
== NULL
3708 : ACONCAT ((_(msgstr
), " ", NULL
)));
3709 tree fn
= candidate
->fn
;
3710 if (flag_new_inheriting_ctors
)
3711 fn
= strip_inheriting_ctors (fn
);
3712 location_t cloc
= location_of (fn
);
3714 if (identifier_p (fn
))
3717 if (candidate
->num_convs
== 3)
3718 inform (cloc
, "%s%<%D(%T, %T, %T)%> (built-in)", msg
, fn
,
3719 candidate
->convs
[0]->type
,
3720 candidate
->convs
[1]->type
,
3721 candidate
->convs
[2]->type
);
3722 else if (candidate
->num_convs
== 2)
3723 inform (cloc
, "%s%<%D(%T, %T)%> (built-in)", msg
, fn
,
3724 candidate
->convs
[0]->type
,
3725 candidate
->convs
[1]->type
);
3727 inform (cloc
, "%s%<%D(%T)%> (built-in)", msg
, fn
,
3728 candidate
->convs
[0]->type
);
3730 else if (TYPE_P (fn
))
3731 inform (cloc
, "%s%qT (conversion)", msg
, fn
);
3732 else if (candidate
->viable
== -1)
3733 inform (cloc
, "%s%#qD (near match)", msg
, fn
);
3734 else if (DECL_DELETED_FN (fn
))
3735 inform (cloc
, "%s%#qD (deleted)", msg
, fn
);
3736 else if (candidate
->reversed ())
3737 inform (cloc
, "%s%#qD (reversed)", msg
, fn
);
3738 else if (candidate
->rewritten ())
3739 inform (cloc
, "%s%#qD (rewritten)", msg
, fn
);
3741 inform (cloc
, "%s%#qD", msg
, fn
);
3742 if (fn
!= candidate
->fn
)
3744 cloc
= location_of (candidate
->fn
);
3745 inform (cloc
, " inherited here");
3747 /* Give the user some information about why this candidate failed. */
3748 if (candidate
->reason
!= NULL
)
3750 struct rejection_reason
*r
= candidate
->reason
;
3755 print_arity_information (cloc
, r
->u
.arity
.actual
,
3756 r
->u
.arity
.expected
);
3758 case rr_arg_conversion
:
3759 print_conversion_rejection (cloc
, &r
->u
.conversion
, fn
);
3761 case rr_bad_arg_conversion
:
3762 print_conversion_rejection (cloc
, &r
->u
.bad_conversion
, fn
);
3764 case rr_explicit_conversion
:
3765 inform (cloc
, " return type %qT of explicit conversion function "
3766 "cannot be converted to %qT with a qualification "
3767 "conversion", r
->u
.conversion
.from
,
3768 r
->u
.conversion
.to_type
);
3770 case rr_template_conversion
:
3771 inform (cloc
, " conversion from return type %qT of template "
3772 "conversion function specialization to %qT is not an "
3773 "exact match", r
->u
.conversion
.from
,
3774 r
->u
.conversion
.to_type
);
3776 case rr_template_unification
:
3777 /* We use template_unification_error_rejection if unification caused
3778 actual non-SFINAE errors, in which case we don't need to repeat
3780 if (r
->u
.template_unification
.tmpl
== NULL_TREE
)
3782 inform (cloc
, " substitution of deduced template arguments "
3783 "resulted in errors seen above");
3786 /* Re-run template unification with diagnostics. */
3787 inform (cloc
, " template argument deduction/substitution failed:");
3788 fn_type_unification (r
->u
.template_unification
.tmpl
,
3789 r
->u
.template_unification
.explicit_targs
,
3791 (r
->u
.template_unification
.num_targs
)),
3792 r
->u
.template_unification
.args
,
3793 r
->u
.template_unification
.nargs
,
3794 r
->u
.template_unification
.return_type
,
3795 r
->u
.template_unification
.strict
,
3796 r
->u
.template_unification
.flags
,
3799 case rr_invalid_copy
:
3801 " a constructor taking a single argument of its own "
3802 "class type is invalid");
3804 case rr_constraint_failure
:
3805 diagnose_constraints (cloc
, fn
, NULL_TREE
);
3807 case rr_inherited_ctor
:
3808 inform (cloc
, " an inherited constructor is not a candidate for "
3809 "initialization from an expression of the same or derived "
3814 /* This candidate didn't have any issues or we failed to
3815 handle a particular code. Either way... */
3822 print_z_candidates (location_t loc
, struct z_candidate
*candidates
)
3824 struct z_candidate
*cand1
;
3825 struct z_candidate
**cand2
;
3830 /* Remove non-viable deleted candidates. */
3832 for (cand2
= &cand1
; *cand2
; )
3834 if (TREE_CODE ((*cand2
)->fn
) == FUNCTION_DECL
3835 && !(*cand2
)->viable
3836 && DECL_DELETED_FN ((*cand2
)->fn
))
3837 *cand2
= (*cand2
)->next
;
3839 cand2
= &(*cand2
)->next
;
3841 /* ...if there are any non-deleted ones. */
3845 /* There may be duplicates in the set of candidates. We put off
3846 checking this condition as long as possible, since we have no way
3847 to eliminate duplicates from a set of functions in less than n^2
3848 time. Now we are about to emit an error message, so it is more
3849 permissible to go slowly. */
3850 for (cand1
= candidates
; cand1
; cand1
= cand1
->next
)
3852 tree fn
= cand1
->fn
;
3853 /* Skip builtin candidates and conversion functions. */
3856 cand2
= &cand1
->next
;
3859 if (DECL_P ((*cand2
)->fn
)
3860 && equal_functions (fn
, (*cand2
)->fn
))
3861 *cand2
= (*cand2
)->next
;
3863 cand2
= &(*cand2
)->next
;
3867 for (; candidates
; candidates
= candidates
->next
)
3868 print_z_candidate (loc
, N_("candidate:"), candidates
);
3871 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3872 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3873 the result of the conversion function to convert it to the final
3874 desired type. Merge the two sequences into a single sequence,
3875 and return the merged sequence. */
3878 merge_conversion_sequences (conversion
*user_seq
, conversion
*std_seq
)
3881 bool bad
= user_seq
->bad_p
;
3883 gcc_assert (user_seq
->kind
== ck_user
);
3885 /* Find the end of the second conversion sequence. */
3886 for (t
= &std_seq
; (*t
)->kind
!= ck_identity
; t
= &((*t
)->u
.next
))
3888 /* The entire sequence is a user-conversion sequence. */
3889 (*t
)->user_conv_p
= true;
3894 if ((*t
)->rvaluedness_matches_p
)
3895 /* We're binding a reference directly to the result of the conversion.
3896 build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
3897 type, but we want it back. */
3898 user_seq
->type
= TREE_TYPE (TREE_TYPE (user_seq
->cand
->fn
));
3900 /* Replace the identity conversion with the user conversion
3907 /* Handle overload resolution for initializing an object of class type from
3908 an initializer list. First we look for a suitable constructor that
3909 takes a std::initializer_list; if we don't find one, we then look for a
3910 non-list constructor.
3912 Parameters are as for add_candidates, except that the arguments are in
3913 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3914 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3917 add_list_candidates (tree fns
, tree first_arg
,
3918 const vec
<tree
, va_gc
> *args
, tree totype
,
3919 tree explicit_targs
, bool template_only
,
3920 tree conversion_path
, tree access_path
,
3922 struct z_candidate
**candidates
,
3923 tsubst_flags_t complain
)
3925 gcc_assert (*candidates
== NULL
);
3927 /* We're looking for a ctor for list-initialization. */
3928 flags
|= LOOKUP_LIST_INIT_CTOR
;
3929 /* And we don't allow narrowing conversions. We also use this flag to
3930 avoid the copy constructor call for copy-list-initialization. */
3931 flags
|= LOOKUP_NO_NARROWING
;
3933 unsigned nart
= num_artificial_parms_for (OVL_FIRST (fns
)) - 1;
3934 tree init_list
= (*args
)[nart
];
3936 /* Always use the default constructor if the list is empty (DR 990). */
3937 if (CONSTRUCTOR_NELTS (init_list
) == 0
3938 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype
))
3940 /* If the class has a list ctor, try passing the list as a single
3941 argument first, but only consider list ctors. */
3942 else if (TYPE_HAS_LIST_CTOR (totype
))
3944 flags
|= LOOKUP_LIST_ONLY
;
3945 add_candidates (fns
, first_arg
, args
, NULL_TREE
,
3946 explicit_targs
, template_only
, conversion_path
,
3947 access_path
, flags
, candidates
, complain
);
3948 if (any_strictly_viable (*candidates
))
3951 else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list
)
3952 && !CP_AGGREGATE_TYPE_P (totype
))
3954 if (complain
& tf_error
)
3955 error ("designated initializers cannot be used with a "
3956 "non-aggregate type %qT", totype
);
3960 /* Expand the CONSTRUCTOR into a new argument vec. */
3961 vec
<tree
, va_gc
> *new_args
;
3962 vec_alloc (new_args
, nart
+ CONSTRUCTOR_NELTS (init_list
));
3963 for (unsigned i
= 0; i
< nart
; ++i
)
3964 new_args
->quick_push ((*args
)[i
]);
3965 for (unsigned i
= 0; i
< CONSTRUCTOR_NELTS (init_list
); ++i
)
3966 new_args
->quick_push (CONSTRUCTOR_ELT (init_list
, i
)->value
);
3968 /* We aren't looking for list-ctors anymore. */
3969 flags
&= ~LOOKUP_LIST_ONLY
;
3970 /* We allow more user-defined conversions within an init-list. */
3971 flags
&= ~LOOKUP_NO_CONVERSION
;
3973 add_candidates (fns
, first_arg
, new_args
, NULL_TREE
,
3974 explicit_targs
, template_only
, conversion_path
,
3975 access_path
, flags
, candidates
, complain
);
3978 /* Returns the best overload candidate to perform the requested
3979 conversion. This function is used for three the overloading situations
3980 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3981 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3982 per [dcl.init.ref], so we ignore temporary bindings. */
3984 static struct z_candidate
*
3985 build_user_type_conversion_1 (tree totype
, tree expr
, int flags
,
3986 tsubst_flags_t complain
)
3988 struct z_candidate
*candidates
, *cand
;
3990 tree ctors
= NULL_TREE
;
3991 tree conv_fns
= NULL_TREE
;
3992 conversion
*conv
= NULL
;
3993 tree first_arg
= NULL_TREE
;
3994 vec
<tree
, va_gc
> *args
= NULL
;
4001 fromtype
= TREE_TYPE (expr
);
4003 /* We represent conversion within a hierarchy using RVALUE_CONV and
4004 BASE_CONV, as specified by [over.best.ics]; these become plain
4005 constructor calls, as specified in [dcl.init]. */
4006 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype
) || !MAYBE_CLASS_TYPE_P (totype
)
4007 || !DERIVED_FROM_P (totype
, fromtype
));
4009 if (CLASS_TYPE_P (totype
))
4010 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4011 creating a garbage BASELINK; constructors can't be inherited. */
4012 ctors
= get_class_binding (totype
, complete_ctor_identifier
);
4014 if (MAYBE_CLASS_TYPE_P (fromtype
))
4016 tree to_nonref
= non_reference (totype
);
4017 if (same_type_ignoring_top_level_qualifiers_p (to_nonref
, fromtype
) ||
4018 (CLASS_TYPE_P (to_nonref
) && CLASS_TYPE_P (fromtype
)
4019 && DERIVED_FROM_P (to_nonref
, fromtype
)))
4021 /* [class.conv.fct] A conversion function is never used to
4022 convert a (possibly cv-qualified) object to the (possibly
4023 cv-qualified) same object type (or a reference to it), to a
4024 (possibly cv-qualified) base class of that type (or a
4025 reference to it)... */
4028 conv_fns
= lookup_conversions (fromtype
);
4032 flags
|= LOOKUP_NO_CONVERSION
;
4033 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
4034 flags
|= LOOKUP_NO_NARROWING
;
4036 /* It's OK to bind a temporary for converting constructor arguments, but
4037 not in converting the return value of a conversion operator. */
4038 convflags
= ((flags
& LOOKUP_NO_TEMP_BIND
) | LOOKUP_NO_CONVERSION
4039 | (flags
& LOOKUP_NO_NARROWING
));
4040 flags
&= ~LOOKUP_NO_TEMP_BIND
;
4044 int ctorflags
= flags
;
4046 first_arg
= build_dummy_object (totype
);
4048 /* We should never try to call the abstract or base constructor
4050 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors
))
4051 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors
)));
4053 args
= make_tree_vector_single (expr
);
4054 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
4056 /* List-initialization. */
4057 add_list_candidates (ctors
, first_arg
, args
, totype
, NULL_TREE
,
4058 false, TYPE_BINFO (totype
), TYPE_BINFO (totype
),
4059 ctorflags
, &candidates
, complain
);
4063 add_candidates (ctors
, first_arg
, args
, NULL_TREE
, NULL_TREE
, false,
4064 TYPE_BINFO (totype
), TYPE_BINFO (totype
),
4065 ctorflags
, &candidates
, complain
);
4068 for (cand
= candidates
; cand
; cand
= cand
->next
)
4070 cand
->second_conv
= build_identity_conv (totype
, NULL_TREE
);
4072 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
4073 set, then this is copy-initialization. In that case, "The
4074 result of the call is then used to direct-initialize the
4075 object that is the destination of the copy-initialization."
4078 We represent this in the conversion sequence with an
4079 rvalue conversion, which means a constructor call. */
4080 if (!TYPE_REF_P (totype
)
4081 && !(convflags
& LOOKUP_NO_TEMP_BIND
))
4083 = build_conv (ck_rvalue
, totype
, cand
->second_conv
);
4089 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
4090 first_arg
= CONSTRUCTOR_ELT (expr
, 0)->value
;
4095 for (; conv_fns
; conv_fns
= TREE_CHAIN (conv_fns
))
4097 tree conversion_path
= TREE_PURPOSE (conv_fns
);
4098 struct z_candidate
*old_candidates
;
4100 /* If we are called to convert to a reference type, we are trying to
4101 find a direct binding, so don't even consider temporaries. If
4102 we don't find a direct binding, the caller will try again to
4103 look for a temporary binding. */
4104 if (TYPE_REF_P (totype
))
4105 convflags
|= LOOKUP_NO_TEMP_BIND
;
4107 old_candidates
= candidates
;
4108 add_candidates (TREE_VALUE (conv_fns
), first_arg
, NULL
, totype
,
4110 conversion_path
, TYPE_BINFO (fromtype
),
4111 flags
, &candidates
, complain
);
4113 for (cand
= candidates
; cand
!= old_candidates
; cand
= cand
->next
)
4115 if (cand
->viable
== 0)
4116 /* Already rejected, don't change to -1. */
4119 tree rettype
= TREE_TYPE (TREE_TYPE (cand
->fn
));
4121 = implicit_conversion (totype
,
4124 /*c_cast_p=*/false, convflags
,
4127 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4128 copy-initialization. In that case, "The result of the
4129 call is then used to direct-initialize the object that is
4130 the destination of the copy-initialization." [dcl.init]
4132 We represent this in the conversion sequence with an
4133 rvalue conversion, which means a constructor call. But
4134 don't add a second rvalue conversion if there's already
4135 one there. Which there really shouldn't be, but it's
4136 harmless since we'd add it here anyway. */
4137 if (ics
&& MAYBE_CLASS_TYPE_P (totype
) && ics
->kind
!= ck_rvalue
4138 && !(convflags
& LOOKUP_NO_TEMP_BIND
))
4139 ics
= build_conv (ck_rvalue
, totype
, ics
);
4141 cand
->second_conv
= ics
;
4146 cand
->reason
= arg_conversion_rejection (NULL_TREE
, -2,
4148 EXPR_LOCATION (expr
));
4150 else if (TYPE_REF_P (totype
) && !ics
->rvaluedness_matches_p
4151 /* Limit this to non-templates for now (PR90546). */
4152 && !cand
->template_decl
4153 && TREE_CODE (TREE_TYPE (totype
)) != FUNCTION_TYPE
)
4155 /* If we are called to convert to a reference type, we are trying
4156 to find a direct binding per [over.match.ref], so rvaluedness
4157 must match for non-functions. */
4160 else if (DECL_NONCONVERTING_P (cand
->fn
)
4161 && ics
->rank
> cr_exact
)
4163 /* 13.3.1.5: For direct-initialization, those explicit
4164 conversion functions that are not hidden within S and
4165 yield type T or a type that can be converted to type T
4166 with a qualification conversion (4.4) are also candidate
4168 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4169 I've raised this issue with the committee. --jason 9/2011 */
4171 cand
->reason
= explicit_conversion_rejection (rettype
, totype
);
4173 else if (cand
->viable
== 1 && ics
->bad_p
)
4177 = bad_arg_conversion_rejection (NULL_TREE
, -2,
4179 EXPR_LOCATION (expr
));
4181 else if (primary_template_specialization_p (cand
->fn
)
4182 && ics
->rank
> cr_exact
)
4184 /* 13.3.3.1.2: If the user-defined conversion is specified by
4185 a specialization of a conversion function template, the
4186 second standard conversion sequence shall have exact match
4189 cand
->reason
= template_conversion_rejection (rettype
, totype
);
4194 candidates
= splice_viable (candidates
, false, &any_viable_p
);
4198 release_tree_vector (args
);
4202 cand
= tourney (candidates
, complain
);
4205 if (complain
& tf_error
)
4207 auto_diagnostic_group d
;
4208 error_at (cp_expr_loc_or_input_loc (expr
),
4209 "conversion from %qH to %qI is ambiguous",
4211 print_z_candidates (location_of (expr
), candidates
);
4214 cand
= candidates
; /* any one will do */
4215 cand
->second_conv
= build_ambiguous_conv (totype
, expr
);
4216 cand
->second_conv
->user_conv_p
= true;
4217 if (!any_strictly_viable (candidates
))
4218 cand
->second_conv
->bad_p
= true;
4219 if (flags
& LOOKUP_ONLYCONVERTING
)
4220 cand
->second_conv
->need_temporary_p
= true;
4221 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4222 ambiguous conversion is no worse than another user-defined
4229 if (!DECL_CONSTRUCTOR_P (cand
->fn
))
4230 convtype
= non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
)));
4231 else if (cand
->second_conv
->kind
== ck_rvalue
)
4232 /* DR 5: [in the first step of copy-initialization]...if the function
4233 is a constructor, the call initializes a temporary of the
4234 cv-unqualified version of the destination type. */
4235 convtype
= cv_unqualified (totype
);
4238 /* Build the user conversion sequence. */
4242 build_identity_conv (TREE_TYPE (expr
), expr
));
4244 if (cand
->viable
== -1)
4247 /* We're performing the maybe-rvalue overload resolution and
4248 a conversion function is in play. Reject converting the return
4249 value of the conversion function to a base class. */
4250 if ((flags
& LOOKUP_PREFER_RVALUE
) && !DECL_CONSTRUCTOR_P (cand
->fn
))
4251 for (conversion
*t
= cand
->second_conv
; t
; t
= next_conversion (t
))
4252 if (t
->kind
== ck_base
)
4255 /* Remember that this was a list-initialization. */
4256 if (flags
& LOOKUP_NO_NARROWING
)
4257 conv
->check_narrowing
= true;
4259 /* Combine it with the second conversion sequence. */
4260 cand
->second_conv
= merge_conversion_sequences (conv
,
4266 /* Wrapper for above. */
4269 build_user_type_conversion (tree totype
, tree expr
, int flags
,
4270 tsubst_flags_t complain
)
4272 struct z_candidate
*cand
;
4275 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
4276 cand
= build_user_type_conversion_1 (totype
, expr
, flags
, complain
);
4280 if (cand
->second_conv
->kind
== ck_ambig
)
4281 ret
= error_mark_node
;
4284 expr
= convert_like (cand
->second_conv
, expr
, complain
);
4285 ret
= convert_from_reference (expr
);
4291 timevar_cond_stop (TV_OVERLOAD
, subtime
);
4295 /* Give a helpful diagnostic when implicit_conversion fails. */
4298 implicit_conversion_error (location_t loc
, tree type
, tree expr
)
4300 tsubst_flags_t complain
= tf_warning_or_error
;
4302 /* If expr has unknown type, then it is an overloaded function.
4303 Call instantiate_type to get good error messages. */
4304 if (TREE_TYPE (expr
) == unknown_type_node
)
4305 instantiate_type (type
, expr
, complain
);
4306 else if (invalid_nonstatic_memfn_p (loc
, expr
, complain
))
4307 /* We gave an error. */;
4308 else if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
4309 && CONSTRUCTOR_IS_DESIGNATED_INIT (expr
)
4310 && !CP_AGGREGATE_TYPE_P (type
))
4311 error_at (loc
, "designated initializers cannot be used with a "
4312 "non-aggregate type %qT", type
);
4315 range_label_for_type_mismatch
label (TREE_TYPE (expr
), type
);
4316 gcc_rich_location
rich_loc (loc
, &label
);
4317 error_at (&rich_loc
, "could not convert %qE from %qH to %qI",
4318 expr
, TREE_TYPE (expr
), type
);
4322 /* Worker for build_converted_constant_expr. */
4325 build_converted_constant_expr_internal (tree type
, tree expr
,
4326 int flags
, tsubst_flags_t complain
)
4331 location_t loc
= cp_expr_loc_or_input_loc (expr
);
4333 if (error_operand_p (expr
))
4334 return error_mark_node
;
4336 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4337 p
= conversion_obstack_alloc (0);
4339 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
4340 /*c_cast_p=*/false, flags
, complain
);
4342 /* A converted constant expression of type T is an expression, implicitly
4343 converted to type T, where the converted expression is a constant
4344 expression and the implicit conversion sequence contains only
4346 * user-defined conversions,
4347 * lvalue-to-rvalue conversions (7.1),
4348 * array-to-pointer conversions (7.2),
4349 * function-to-pointer conversions (7.3),
4350 * qualification conversions (7.5),
4351 * integral promotions (7.6),
4352 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4353 * null pointer conversions (7.11) from std::nullptr_t,
4354 * null member pointer conversions (7.12) from std::nullptr_t, and
4355 * function pointer conversions (7.13),
4357 and where the reference binding (if any) binds directly. */
4359 for (conversion
*c
= conv
;
4360 c
&& c
->kind
!= ck_identity
;
4361 c
= next_conversion (c
))
4365 /* A conversion function is OK. If it isn't constexpr, we'll
4366 complain later that the argument isn't constant. */
4368 /* List-initialization is OK. */
4370 /* The lvalue-to-rvalue conversion is OK. */
4372 /* Array-to-pointer and function-to-pointer. */
4374 /* Function pointer conversions. */
4376 /* Qualification conversions. */
4381 if (c
->need_temporary_p
)
4383 if (complain
& tf_error
)
4384 error_at (loc
, "initializing %qH with %qI in converted "
4385 "constant expression does not bind directly",
4386 type
, next_conversion (c
)->type
);
4395 t
= next_conversion (c
)->type
;
4396 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t
)
4397 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4398 /* Integral promotion or conversion. */
4400 if (NULLPTR_TYPE_P (t
))
4401 /* Conversion from nullptr to pointer or pointer-to-member. */
4404 if (complain
& tf_error
)
4405 error_at (loc
, "conversion from %qH to %qI in a "
4406 "converted constant expression", t
, type
);
4415 /* Avoid confusing convert_nontype_argument by introducing
4416 a redundant conversion to the same reference type. */
4417 if (conv
&& conv
->kind
== ck_ref_bind
4418 && REFERENCE_REF_P (expr
))
4420 tree ref
= TREE_OPERAND (expr
, 0);
4421 if (same_type_p (type
, TREE_TYPE (ref
)))
4427 /* Don't copy a class in a template. */
4428 if (CLASS_TYPE_P (type
) && conv
->kind
== ck_rvalue
4429 && processing_template_decl
)
4430 conv
= next_conversion (conv
);
4432 conv
->check_narrowing
= true;
4433 conv
->check_narrowing_const_only
= true;
4434 expr
= convert_like (conv
, expr
, complain
);
4438 if (complain
& tf_error
)
4439 implicit_conversion_error (loc
, type
, expr
);
4440 expr
= error_mark_node
;
4443 /* Free all the conversions we allocated. */
4444 obstack_free (&conversion_obstack
, p
);
4449 /* Subroutine of convert_nontype_argument.
4451 EXPR is an expression used in a context that requires a converted
4452 constant-expression, such as a template non-type parameter. Do any
4453 necessary conversions (that are permitted for converted
4454 constant-expressions) to convert it to the desired type.
4456 This function doesn't consider explicit conversion functions. If
4457 you mean to use "a contextually converted constant expression of type
4458 bool", use build_converted_constant_bool_expr.
4460 If conversion is successful, returns the converted expression;
4461 otherwise, returns error_mark_node. */
4464 build_converted_constant_expr (tree type
, tree expr
, tsubst_flags_t complain
)
4466 return build_converted_constant_expr_internal (type
, expr
, LOOKUP_IMPLICIT
,
4470 /* Used to create "a contextually converted constant expression of type
4471 bool". This differs from build_converted_constant_expr in that it
4472 also considers explicit conversion functions. */
4475 build_converted_constant_bool_expr (tree expr
, tsubst_flags_t complain
)
4477 return build_converted_constant_expr_internal (boolean_type_node
, expr
,
4478 LOOKUP_NORMAL
, complain
);
4481 /* Do any initial processing on the arguments to a function call. */
4484 resolve_args (vec
<tree
, va_gc
> *args
, tsubst_flags_t complain
)
4489 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
4491 if (error_operand_p (arg
))
4493 else if (VOID_TYPE_P (TREE_TYPE (arg
)))
4495 if (complain
& tf_error
)
4496 error_at (cp_expr_loc_or_input_loc (arg
),
4497 "invalid use of void expression");
4500 else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg
), arg
, complain
))
4506 /* Perform overload resolution on FN, which is called with the ARGS.
4508 Return the candidate function selected by overload resolution, or
4509 NULL if the event that overload resolution failed. In the case
4510 that overload resolution fails, *CANDIDATES will be the set of
4511 candidates considered, and ANY_VIABLE_P will be set to true or
4512 false to indicate whether or not any of the candidates were
4515 The ARGS should already have gone through RESOLVE_ARGS before this
4516 function is called. */
4518 static struct z_candidate
*
4519 perform_overload_resolution (tree fn
,
4520 const vec
<tree
, va_gc
> *args
,
4521 struct z_candidate
**candidates
,
4522 bool *any_viable_p
, tsubst_flags_t complain
)
4524 struct z_candidate
*cand
;
4525 tree explicit_targs
;
4528 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
4530 explicit_targs
= NULL_TREE
;
4534 *any_viable_p
= true;
4537 gcc_assert (OVL_P (fn
) || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
);
4539 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4541 explicit_targs
= TREE_OPERAND (fn
, 1);
4542 fn
= TREE_OPERAND (fn
, 0);
4546 /* Add the various candidate functions. */
4547 add_candidates (fn
, NULL_TREE
, args
, NULL_TREE
,
4548 explicit_targs
, template_only
,
4549 /*conversion_path=*/NULL_TREE
,
4550 /*access_path=*/NULL_TREE
,
4552 candidates
, complain
);
4554 *candidates
= splice_viable (*candidates
, false, any_viable_p
);
4556 cand
= tourney (*candidates
, complain
);
4560 timevar_cond_stop (TV_OVERLOAD
, subtime
);
4564 /* Print an error message about being unable to build a call to FN with
4565 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4566 be located; CANDIDATES is a possibly empty list of such
4570 print_error_for_call_failure (tree fn
, vec
<tree
, va_gc
> *args
,
4571 struct z_candidate
*candidates
)
4573 tree targs
= NULL_TREE
;
4574 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4576 targs
= TREE_OPERAND (fn
, 1);
4577 fn
= TREE_OPERAND (fn
, 0);
4579 tree name
= OVL_NAME (fn
);
4580 location_t loc
= location_of (name
);
4582 name
= lookup_template_function (name
, targs
);
4584 auto_diagnostic_group d
;
4585 if (!any_strictly_viable (candidates
))
4586 error_at (loc
, "no matching function for call to %<%D(%A)%>",
4587 name
, build_tree_list_vec (args
));
4589 error_at (loc
, "call of overloaded %<%D(%A)%> is ambiguous",
4590 name
, build_tree_list_vec (args
));
4592 print_z_candidates (loc
, candidates
);
4595 /* Return an expression for a call to FN (a namespace-scope function,
4596 or a static member function) with the ARGS. This may change
4600 build_new_function_call (tree fn
, vec
<tree
, va_gc
> **args
,
4601 tsubst_flags_t complain
)
4603 struct z_candidate
*candidates
, *cand
;
4608 if (args
!= NULL
&& *args
!= NULL
)
4610 *args
= resolve_args (*args
, complain
);
4612 return error_mark_node
;
4616 tm_malloc_replacement (fn
);
4618 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4619 p
= conversion_obstack_alloc (0);
4621 cand
= perform_overload_resolution (fn
, *args
, &candidates
, &any_viable_p
,
4626 if (complain
& tf_error
)
4628 // If there is a single (non-viable) function candidate,
4629 // let the error be diagnosed by cp_build_function_call_vec.
4630 if (!any_viable_p
&& candidates
&& ! candidates
->next
4631 && (TREE_CODE (candidates
->fn
) == FUNCTION_DECL
))
4632 return cp_build_function_call_vec (candidates
->fn
, args
, complain
);
4634 // Otherwise, emit notes for non-viable candidates.
4635 print_error_for_call_failure (fn
, *args
, candidates
);
4637 result
= error_mark_node
;
4641 result
= build_over_call (cand
, LOOKUP_NORMAL
, complain
);
4646 && TREE_CODE (result
) == CALL_EXPR
4647 && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result
), 0))
4649 result
= coro_validate_builtin_call (result
);
4651 /* Free all the conversions we allocated. */
4652 obstack_free (&conversion_obstack
, p
);
4657 /* Build a call to a global operator new. FNNAME is the name of the
4658 operator (either "operator new" or "operator new[]") and ARGS are
4659 the arguments provided. This may change ARGS. *SIZE points to the
4660 total number of bytes required by the allocation, and is updated if
4661 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4662 be used. If this function determines that no cookie should be
4663 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4664 is not NULL_TREE, it is evaluated before calculating the final
4665 array size, and if it fails, the array size is replaced with
4666 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4667 is non-NULL, it will be set, upon return, to the allocation
4671 build_operator_new_call (tree fnname
, vec
<tree
, va_gc
> **args
,
4672 tree
*size
, tree
*cookie_size
,
4673 tree align_arg
, tree size_check
,
4674 tree
*fn
, tsubst_flags_t complain
)
4676 tree original_size
= *size
;
4678 struct z_candidate
*candidates
;
4679 struct z_candidate
*cand
= NULL
;
4684 /* Set to (size_t)-1 if the size check fails. */
4685 if (size_check
!= NULL_TREE
)
4687 tree errval
= TYPE_MAX_VALUE (sizetype
);
4688 if (cxx_dialect
>= cxx11
&& flag_exceptions
)
4689 errval
= throw_bad_array_new_length ();
4690 *size
= fold_build3 (COND_EXPR
, sizetype
, size_check
,
4691 original_size
, errval
);
4693 vec_safe_insert (*args
, 0, *size
);
4694 *args
= resolve_args (*args
, complain
);
4696 return error_mark_node
;
4702 If this lookup fails to find the name, or if the allocated type
4703 is not a class type, the allocation function's name is looked
4704 up in the global scope.
4706 we disregard block-scope declarations of "operator new". */
4707 fns
= lookup_name_real (fnname
, LOOK_where::NAMESPACE
, 0, 0, 0);
4708 fns
= lookup_arg_dependent (fnname
, fns
, *args
);
4712 vec
<tree
, va_gc
>* align_args
4713 = vec_copy_and_insert (*args
, align_arg
, 1);
4714 cand
= perform_overload_resolution (fns
, align_args
, &candidates
,
4715 &any_viable_p
, tf_none
);
4718 /* If no aligned allocation function matches, try again without the
4722 /* Figure out what function is being called. */
4724 cand
= perform_overload_resolution (fns
, *args
, &candidates
, &any_viable_p
,
4727 /* If no suitable function could be found, issue an error message
4731 if (complain
& tf_error
)
4732 print_error_for_call_failure (fns
, *args
, candidates
);
4733 return error_mark_node
;
4736 /* If a cookie is required, add some extra space. Whether
4737 or not a cookie is required cannot be determined until
4738 after we know which function was called. */
4741 bool use_cookie
= true;
4744 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (cand
->fn
));
4745 /* Skip the size_t parameter. */
4746 arg_types
= TREE_CHAIN (arg_types
);
4747 /* Check the remaining parameters (if any). */
4749 && TREE_CHAIN (arg_types
) == void_list_node
4750 && same_type_p (TREE_VALUE (arg_types
),
4753 /* If we need a cookie, adjust the number of bytes allocated. */
4756 /* Update the total size. */
4757 *size
= size_binop (PLUS_EXPR
, original_size
, *cookie_size
);
4760 /* Set to (size_t)-1 if the size check fails. */
4761 gcc_assert (size_check
!= NULL_TREE
);
4762 *size
= fold_build3 (COND_EXPR
, sizetype
, size_check
,
4763 *size
, TYPE_MAX_VALUE (sizetype
));
4765 /* Update the argument list to reflect the adjusted size. */
4766 (**args
)[0] = *size
;
4769 *cookie_size
= NULL_TREE
;
4772 /* Tell our caller which function we decided to call. */
4776 /* Build the CALL_EXPR. */
4777 return build_over_call (cand
, LOOKUP_NORMAL
, complain
);
4780 /* Build a new call to operator(). This may change ARGS. */
4783 build_op_call_1 (tree obj
, vec
<tree
, va_gc
> **args
, tsubst_flags_t complain
)
4785 struct z_candidate
*candidates
= 0, *cand
;
4786 tree fns
, convs
, first_mem_arg
= NULL_TREE
;
4788 tree result
= NULL_TREE
;
4791 obj
= mark_lvalue_use (obj
);
4793 if (error_operand_p (obj
))
4794 return error_mark_node
;
4796 tree type
= TREE_TYPE (obj
);
4798 obj
= prep_operand (obj
);
4800 if (TYPE_PTRMEMFUNC_P (type
))
4802 if (complain
& tf_error
)
4803 /* It's no good looking for an overloaded operator() on a
4804 pointer-to-member-function. */
4805 error ("pointer-to-member function %qE cannot be called without "
4806 "an object; consider using %<.*%> or %<->*%>", obj
);
4807 return error_mark_node
;
4810 if (TYPE_BINFO (type
))
4812 fns
= lookup_fnfields (TYPE_BINFO (type
), call_op_identifier
, 1, complain
);
4813 if (fns
== error_mark_node
)
4814 return error_mark_node
;
4819 if (args
!= NULL
&& *args
!= NULL
)
4821 *args
= resolve_args (*args
, complain
);
4823 return error_mark_node
;
4826 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4827 p
= conversion_obstack_alloc (0);
4831 first_mem_arg
= obj
;
4833 add_candidates (BASELINK_FUNCTIONS (fns
),
4834 first_mem_arg
, *args
, NULL_TREE
,
4836 BASELINK_BINFO (fns
), BASELINK_ACCESS_BINFO (fns
),
4837 LOOKUP_NORMAL
, &candidates
, complain
);
4840 convs
= lookup_conversions (type
);
4842 for (; convs
; convs
= TREE_CHAIN (convs
))
4844 tree totype
= TREE_TYPE (convs
);
4846 if (TYPE_PTRFN_P (totype
)
4847 || TYPE_REFFN_P (totype
)
4848 || (TYPE_REF_P (totype
)
4849 && TYPE_PTRFN_P (TREE_TYPE (totype
))))
4850 for (ovl_iterator
iter (TREE_VALUE (convs
)); iter
; ++iter
)
4854 if (DECL_NONCONVERTING_P (fn
))
4857 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4858 add_template_conv_candidate
4859 (&candidates
, fn
, obj
, *args
, totype
,
4860 /*access_path=*/NULL_TREE
,
4861 /*conversion_path=*/NULL_TREE
, complain
);
4863 add_conv_candidate (&candidates
, fn
, obj
,
4864 *args
, /*conversion_path=*/NULL_TREE
,
4865 /*access_path=*/NULL_TREE
, complain
);
4869 /* Be strict here because if we choose a bad conversion candidate, the
4870 errors we get won't mention the call context. */
4871 candidates
= splice_viable (candidates
, true, &any_viable_p
);
4874 if (complain
& tf_error
)
4876 auto_diagnostic_group d
;
4877 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj
),
4878 build_tree_list_vec (*args
));
4879 print_z_candidates (location_of (TREE_TYPE (obj
)), candidates
);
4881 result
= error_mark_node
;
4885 cand
= tourney (candidates
, complain
);
4888 if (complain
& tf_error
)
4890 auto_diagnostic_group d
;
4891 error ("call of %<(%T) (%A)%> is ambiguous",
4892 TREE_TYPE (obj
), build_tree_list_vec (*args
));
4893 print_z_candidates (location_of (TREE_TYPE (obj
)), candidates
);
4895 result
= error_mark_node
;
4897 else if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
4898 && DECL_OVERLOADED_OPERATOR_P (cand
->fn
)
4899 && DECL_OVERLOADED_OPERATOR_IS (cand
->fn
, CALL_EXPR
))
4900 result
= build_over_call (cand
, LOOKUP_NORMAL
, complain
);
4903 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
4904 obj
= convert_like_with_context (cand
->convs
[0], obj
, cand
->fn
,
4908 gcc_checking_assert (TYPE_P (cand
->fn
));
4909 obj
= convert_like (cand
->convs
[0], obj
, complain
);
4911 obj
= convert_from_reference (obj
);
4912 result
= cp_build_function_call_vec (obj
, args
, complain
);
4916 /* Free all the conversions we allocated. */
4917 obstack_free (&conversion_obstack
, p
);
4922 /* Wrapper for above. */
4925 build_op_call (tree obj
, vec
<tree
, va_gc
> **args
, tsubst_flags_t complain
)
4928 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
4929 ret
= build_op_call_1 (obj
, args
, complain
);
4930 timevar_cond_stop (TV_OVERLOAD
, subtime
);
4934 /* Called by op_error to prepare format strings suitable for the error
4935 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4936 and a suffix (controlled by NTYPES). */
4939 op_error_string (const char *errmsg
, int ntypes
, bool match
)
4943 const char *msgp
= concat (match
? G_("ambiguous overload for ")
4944 : G_("no match for "), errmsg
, NULL
);
4947 msg
= concat (msgp
, G_(" (operand types are %qT, %qT, and %qT)"), NULL
);
4948 else if (ntypes
== 2)
4949 msg
= concat (msgp
, G_(" (operand types are %qT and %qT)"), NULL
);
4951 msg
= concat (msgp
, G_(" (operand type is %qT)"), NULL
);
4957 op_error (const op_location_t
&loc
,
4958 enum tree_code code
, enum tree_code code2
,
4959 tree arg1
, tree arg2
, tree arg3
, bool match
)
4961 bool assop
= code
== MODIFY_EXPR
;
4962 const char *opname
= OVL_OP_INFO (assop
, assop
? code2
: code
)->name
;
4967 if (flag_diagnostics_show_caret
)
4968 error_at (loc
, op_error_string (G_("ternary %<operator?:%>"),
4970 TREE_TYPE (arg1
), TREE_TYPE (arg2
), TREE_TYPE (arg3
));
4972 error_at (loc
, op_error_string (G_("ternary %<operator?:%> "
4973 "in %<%E ? %E : %E%>"), 3, match
),
4975 TREE_TYPE (arg1
), TREE_TYPE (arg2
), TREE_TYPE (arg3
));
4978 case POSTINCREMENT_EXPR
:
4979 case POSTDECREMENT_EXPR
:
4980 if (flag_diagnostics_show_caret
)
4981 error_at (loc
, op_error_string (G_("%<operator%s%>"), 1, match
),
4982 opname
, TREE_TYPE (arg1
));
4984 error_at (loc
, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4986 opname
, arg1
, opname
, TREE_TYPE (arg1
));
4990 if (flag_diagnostics_show_caret
)
4991 error_at (loc
, op_error_string (G_("%<operator[]%>"), 2, match
),
4992 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
4994 error_at (loc
, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
4996 arg1
, arg2
, TREE_TYPE (arg1
), TREE_TYPE (arg2
));
5001 if (flag_diagnostics_show_caret
)
5002 error_at (loc
, op_error_string (G_("%qs"), 1, match
),
5003 opname
, TREE_TYPE (arg1
));
5005 error_at (loc
, op_error_string (G_("%qs in %<%s %E%>"), 1, match
),
5006 opname
, opname
, arg1
, TREE_TYPE (arg1
));
5010 if (flag_diagnostics_show_caret
)
5011 error_at (loc
, op_error_string (G_("%<operator %s%>"), 1, match
),
5012 opname
, TREE_TYPE (arg1
));
5014 error_at (loc
, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5016 opname
, opname
, arg1
, TREE_TYPE (arg1
));
5021 if (flag_diagnostics_show_caret
)
5023 binary_op_rich_location
richloc (loc
, arg1
, arg2
, true);
5025 op_error_string (G_("%<operator%s%>"), 2, match
),
5026 opname
, TREE_TYPE (arg1
), TREE_TYPE (arg2
));
5029 error_at (loc
, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5031 opname
, arg1
, opname
, arg2
,
5032 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
5034 if (flag_diagnostics_show_caret
)
5035 error_at (loc
, op_error_string (G_("%<operator%s%>"), 1, match
),
5036 opname
, TREE_TYPE (arg1
));
5038 error_at (loc
, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5040 opname
, opname
, arg1
, TREE_TYPE (arg1
));
5045 /* Return the implicit conversion sequence that could be used to
5046 convert E1 to E2 in [expr.cond]. */
5049 conditional_conversion (tree e1
, tree e2
, tsubst_flags_t complain
)
5051 tree t1
= non_reference (TREE_TYPE (e1
));
5052 tree t2
= non_reference (TREE_TYPE (e2
));
5058 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5059 implicitly converted (clause _conv_) to the type "lvalue reference to
5060 T2", subject to the constraint that in the conversion the
5061 reference must bind directly (_dcl.init.ref_) to an lvalue.
5063 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5064 implicitly converted to the type "rvalue reference to T2", subject to
5065 the constraint that the reference must bind directly. */
5068 tree rtype
= cp_build_reference_type (t2
, !lvalue_p (e2
));
5069 conv
= implicit_conversion (rtype
,
5073 LOOKUP_NO_TEMP_BIND
|LOOKUP_NO_RVAL_BIND
5074 |LOOKUP_ONLYCONVERTING
,
5076 if (conv
&& !conv
->bad_p
)
5080 /* If E2 is a prvalue or if neither of the conversions above can be done
5081 and at least one of the operands has (possibly cv-qualified) class
5083 if (!CLASS_TYPE_P (t1
) && !CLASS_TYPE_P (t2
))
5088 If E1 and E2 have class type, and the underlying class types are
5089 the same or one is a base class of the other: E1 can be converted
5090 to match E2 if the class of T2 is the same type as, or a base
5091 class of, the class of T1, and the cv-qualification of T2 is the
5092 same cv-qualification as, or a greater cv-qualification than, the
5093 cv-qualification of T1. If the conversion is applied, E1 is
5094 changed to an rvalue of type T2 that still refers to the original
5095 source class object (or the appropriate subobject thereof). */
5096 if (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
5097 && ((good_base
= DERIVED_FROM_P (t2
, t1
)) || DERIVED_FROM_P (t1
, t2
)))
5099 if (good_base
&& at_least_as_qualified_p (t2
, t1
))
5101 conv
= build_identity_conv (t1
, e1
);
5102 if (!same_type_p (TYPE_MAIN_VARIANT (t1
),
5103 TYPE_MAIN_VARIANT (t2
)))
5104 conv
= build_conv (ck_base
, t2
, conv
);
5106 conv
= build_conv (ck_rvalue
, t2
, conv
);
5115 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5116 converted to the type that expression E2 would have if E2 were
5117 converted to an rvalue (or the type it has, if E2 is an rvalue). */
5118 return implicit_conversion (t2
, t1
, e1
, /*c_cast_p=*/false,
5119 LOOKUP_IMPLICIT
, complain
);
5122 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5123 arguments to the conditional expression. */
5126 build_conditional_expr_1 (const op_location_t
&loc
,
5127 tree arg1
, tree arg2
, tree arg3
,
5128 tsubst_flags_t complain
)
5132 tree result
= NULL_TREE
;
5133 tree result_type
= NULL_TREE
;
5134 bool is_glvalue
= true;
5135 struct z_candidate
*candidates
= 0;
5136 struct z_candidate
*cand
;
5138 tree orig_arg2
, orig_arg3
;
5140 /* As a G++ extension, the second argument to the conditional can be
5141 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5142 c'.) If the second operand is omitted, make sure it is
5143 calculated only once. */
5146 if (complain
& tf_error
)
5147 pedwarn (loc
, OPT_Wpedantic
,
5148 "ISO C++ forbids omitting the middle term of "
5149 "a %<?:%> expression");
5151 if ((complain
& tf_warning
) && !truth_value_p (TREE_CODE (arg1
)))
5152 warn_for_omitted_condop (loc
, arg1
);
5154 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5155 if (glvalue_p (arg1
))
5157 arg1
= cp_stabilize_reference (arg1
);
5158 arg2
= arg1
= prevent_lifetime_extension (arg1
);
5161 arg2
= arg1
= cp_save_expr (arg1
);
5164 /* If something has already gone wrong, just pass that fact up the
5166 if (error_operand_p (arg1
)
5167 || error_operand_p (arg2
)
5168 || error_operand_p (arg3
))
5169 return error_mark_node
;
5174 if (gnu_vector_type_p (TREE_TYPE (arg1
))
5175 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1
)))
5177 tree arg1_type
= TREE_TYPE (arg1
);
5179 /* If arg1 is another cond_expr choosing between -1 and 0,
5180 then we can use its comparison. It may help to avoid
5181 additional comparison, produce more accurate diagnostics
5182 and enables folding. */
5183 if (TREE_CODE (arg1
) == VEC_COND_EXPR
5184 && integer_minus_onep (TREE_OPERAND (arg1
, 1))
5185 && integer_zerop (TREE_OPERAND (arg1
, 2)))
5186 arg1
= TREE_OPERAND (arg1
, 0);
5188 arg1
= force_rvalue (arg1
, complain
);
5189 arg2
= force_rvalue (arg2
, complain
);
5190 arg3
= force_rvalue (arg3
, complain
);
5192 /* force_rvalue can return error_mark on valid arguments. */
5193 if (error_operand_p (arg1
)
5194 || error_operand_p (arg2
)
5195 || error_operand_p (arg3
))
5196 return error_mark_node
;
5198 arg2_type
= TREE_TYPE (arg2
);
5199 arg3_type
= TREE_TYPE (arg3
);
5201 if (!VECTOR_TYPE_P (arg2_type
)
5202 && !VECTOR_TYPE_P (arg3_type
))
5204 /* Rely on the error messages of the scalar version. */
5205 tree scal
= build_conditional_expr_1 (loc
, integer_one_node
,
5206 orig_arg2
, orig_arg3
, complain
);
5207 if (scal
== error_mark_node
)
5208 return error_mark_node
;
5209 tree stype
= TREE_TYPE (scal
);
5210 tree ctype
= TREE_TYPE (arg1_type
);
5211 if (TYPE_SIZE (stype
) != TYPE_SIZE (ctype
)
5212 || (!INTEGRAL_TYPE_P (stype
) && !SCALAR_FLOAT_TYPE_P (stype
)))
5214 if (complain
& tf_error
)
5215 error_at (loc
, "inferred scalar type %qT is not an integer or "
5216 "floating-point type of the same size as %qT", stype
,
5217 COMPARISON_CLASS_P (arg1
)
5218 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
5220 return error_mark_node
;
5223 tree vtype
= build_opaque_vector_type (stype
,
5224 TYPE_VECTOR_SUBPARTS (arg1_type
));
5225 /* We could pass complain & tf_warning to unsafe_conversion_p,
5226 but the warnings (like Wsign-conversion) have already been
5227 given by the scalar build_conditional_expr_1. We still check
5228 unsafe_conversion_p to forbid truncating long long -> float. */
5229 if (unsafe_conversion_p (stype
, arg2
, NULL_TREE
, false))
5231 if (complain
& tf_error
)
5232 error_at (loc
, "conversion of scalar %qH to vector %qI "
5233 "involves truncation", arg2_type
, vtype
);
5234 return error_mark_node
;
5236 if (unsafe_conversion_p (stype
, arg3
, NULL_TREE
, false))
5238 if (complain
& tf_error
)
5239 error_at (loc
, "conversion of scalar %qH to vector %qI "
5240 "involves truncation", arg3_type
, vtype
);
5241 return error_mark_node
;
5244 arg2
= cp_convert (stype
, arg2
, complain
);
5245 arg2
= save_expr (arg2
);
5246 arg2
= build_vector_from_val (vtype
, arg2
);
5248 arg3
= cp_convert (stype
, arg3
, complain
);
5249 arg3
= save_expr (arg3
);
5250 arg3
= build_vector_from_val (vtype
, arg3
);
5254 if ((gnu_vector_type_p (arg2_type
) && !VECTOR_TYPE_P (arg3_type
))
5255 || (gnu_vector_type_p (arg3_type
) && !VECTOR_TYPE_P (arg2_type
)))
5257 enum stv_conv convert_flag
=
5258 scalar_to_vector (loc
, VEC_COND_EXPR
, arg2
, arg3
,
5259 complain
& tf_error
);
5261 switch (convert_flag
)
5264 return error_mark_node
;
5267 arg2
= save_expr (arg2
);
5268 arg2
= convert (TREE_TYPE (arg3_type
), arg2
);
5269 arg2
= build_vector_from_val (arg3_type
, arg2
);
5270 arg2_type
= TREE_TYPE (arg2
);
5275 arg3
= save_expr (arg3
);
5276 arg3
= convert (TREE_TYPE (arg2_type
), arg3
);
5277 arg3
= build_vector_from_val (arg2_type
, arg3
);
5278 arg3_type
= TREE_TYPE (arg3
);
5286 if (!gnu_vector_type_p (arg2_type
)
5287 || !gnu_vector_type_p (arg3_type
)
5288 || !same_type_p (arg2_type
, arg3_type
)
5289 || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type
),
5290 TYPE_VECTOR_SUBPARTS (arg2_type
))
5291 || TYPE_SIZE (arg1_type
) != TYPE_SIZE (arg2_type
))
5293 if (complain
& tf_error
)
5295 "incompatible vector types in conditional expression: "
5296 "%qT, %qT and %qT", TREE_TYPE (arg1
),
5297 TREE_TYPE (orig_arg2
), TREE_TYPE (orig_arg3
));
5298 return error_mark_node
;
5301 if (!COMPARISON_CLASS_P (arg1
))
5303 tree cmp_type
= truth_type_for (arg1_type
);
5304 arg1
= build2 (NE_EXPR
, cmp_type
, arg1
, build_zero_cst (arg1_type
));
5306 return build3_loc (loc
, VEC_COND_EXPR
, arg2_type
, arg1
, arg2
, arg3
);
5311 The first expression is implicitly converted to bool (clause
5313 arg1
= perform_implicit_conversion_flags (boolean_type_node
, arg1
, complain
,
5315 if (error_operand_p (arg1
))
5316 return error_mark_node
;
5320 If either the second or the third operand has type (possibly
5321 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
5322 array-to-pointer (_conv.array_), and function-to-pointer
5323 (_conv.func_) standard conversions are performed on the second
5324 and third operands. */
5325 arg2_type
= unlowered_expr_type (arg2
);
5326 arg3_type
= unlowered_expr_type (arg3
);
5327 if (VOID_TYPE_P (arg2_type
) || VOID_TYPE_P (arg3_type
))
5329 /* 'void' won't help in resolving an overloaded expression on the
5330 other side, so require it to resolve by itself. */
5331 if (arg2_type
== unknown_type_node
)
5333 arg2
= resolve_nondeduced_context_or_error (arg2
, complain
);
5334 arg2_type
= TREE_TYPE (arg2
);
5336 if (arg3_type
== unknown_type_node
)
5338 arg3
= resolve_nondeduced_context_or_error (arg3
, complain
);
5339 arg3_type
= TREE_TYPE (arg3
);
5344 One of the following shall hold:
5346 --The second or the third operand (but not both) is a
5347 throw-expression (_except.throw_); the result is of the type
5348 and value category of the other.
5350 --Both the second and the third operands have type void; the
5351 result is of type void and is a prvalue. */
5352 if (TREE_CODE (arg2
) == THROW_EXPR
5353 && TREE_CODE (arg3
) != THROW_EXPR
)
5355 result_type
= arg3_type
;
5356 is_glvalue
= glvalue_p (arg3
);
5358 else if (TREE_CODE (arg2
) != THROW_EXPR
5359 && TREE_CODE (arg3
) == THROW_EXPR
)
5361 result_type
= arg2_type
;
5362 is_glvalue
= glvalue_p (arg2
);
5364 else if (VOID_TYPE_P (arg2_type
) && VOID_TYPE_P (arg3_type
))
5366 result_type
= void_type_node
;
5371 if (complain
& tf_error
)
5373 if (VOID_TYPE_P (arg2_type
))
5374 error_at (cp_expr_loc_or_loc (arg3
, loc
),
5375 "second operand to the conditional operator "
5376 "is of type %<void%>, but the third operand is "
5377 "neither a throw-expression nor of type %<void%>");
5379 error_at (cp_expr_loc_or_loc (arg2
, loc
),
5380 "third operand to the conditional operator "
5381 "is of type %<void%>, but the second operand is "
5382 "neither a throw-expression nor of type %<void%>");
5384 return error_mark_node
;
5387 goto valid_operands
;
5391 Otherwise, if the second and third operand have different types,
5392 and either has (possibly cv-qualified) class type, or if both are
5393 glvalues of the same value category and the same type except for
5394 cv-qualification, an attempt is made to convert each of those operands
5395 to the type of the other. */
5396 else if (!same_type_p (arg2_type
, arg3_type
)
5397 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)
5398 || (same_type_ignoring_top_level_qualifiers_p (arg2_type
,
5400 && glvalue_p (arg2
) && glvalue_p (arg3
)
5401 && lvalue_p (arg2
) == lvalue_p (arg3
))))
5405 bool converted
= false;
5407 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5408 p
= conversion_obstack_alloc (0);
5410 conv2
= conditional_conversion (arg2
, arg3
, complain
);
5411 conv3
= conditional_conversion (arg3
, arg2
, complain
);
5415 If both can be converted, or one can be converted but the
5416 conversion is ambiguous, the program is ill-formed. If
5417 neither can be converted, the operands are left unchanged and
5418 further checking is performed as described below. If exactly
5419 one conversion is possible, that conversion is applied to the
5420 chosen operand and the converted operand is used in place of
5421 the original operand for the remainder of this section. */
5422 if ((conv2
&& !conv2
->bad_p
5423 && conv3
&& !conv3
->bad_p
)
5424 || (conv2
&& conv2
->kind
== ck_ambig
)
5425 || (conv3
&& conv3
->kind
== ck_ambig
))
5427 if (complain
& tf_error
)
5429 error_at (loc
, "operands to %<?:%> have different types "
5431 arg2_type
, arg3_type
);
5432 if (conv2
&& !conv2
->bad_p
&& conv3
&& !conv3
->bad_p
)
5433 inform (loc
, " and each type can be converted to the other");
5434 else if (conv2
&& conv2
->kind
== ck_ambig
)
5435 convert_like (conv2
, arg2
, complain
);
5437 convert_like (conv3
, arg3
, complain
);
5439 result
= error_mark_node
;
5441 else if (conv2
&& !conv2
->bad_p
)
5443 arg2
= convert_like (conv2
, arg2
, complain
);
5444 arg2
= convert_from_reference (arg2
);
5445 arg2_type
= TREE_TYPE (arg2
);
5446 /* Even if CONV2 is a valid conversion, the result of the
5447 conversion may be invalid. For example, if ARG3 has type
5448 "volatile X", and X does not have a copy constructor
5449 accepting a "volatile X&", then even if ARG2 can be
5450 converted to X, the conversion will fail. */
5451 if (error_operand_p (arg2
))
5452 result
= error_mark_node
;
5455 else if (conv3
&& !conv3
->bad_p
)
5457 arg3
= convert_like (conv3
, arg3
, complain
);
5458 arg3
= convert_from_reference (arg3
);
5459 arg3_type
= TREE_TYPE (arg3
);
5460 if (error_operand_p (arg3
))
5461 result
= error_mark_node
;
5465 /* Free all the conversions we allocated. */
5466 obstack_free (&conversion_obstack
, p
);
5471 /* If, after the conversion, both operands have class type,
5472 treat the cv-qualification of both operands as if it were the
5473 union of the cv-qualification of the operands.
5475 The standard is not clear about what to do in this
5476 circumstance. For example, if the first operand has type
5477 "const X" and the second operand has a user-defined
5478 conversion to "volatile X", what is the type of the second
5479 operand after this step? Making it be "const X" (matching
5480 the first operand) seems wrong, as that discards the
5481 qualification without actually performing a copy. Leaving it
5482 as "volatile X" seems wrong as that will result in the
5483 conditional expression failing altogether, even though,
5484 according to this step, the one operand could be converted to
5485 the type of the other. */
5487 && CLASS_TYPE_P (arg2_type
)
5488 && cp_type_quals (arg2_type
) != cp_type_quals (arg3_type
))
5489 arg2_type
= arg3_type
=
5490 cp_build_qualified_type (arg2_type
,
5491 cp_type_quals (arg2_type
)
5492 | cp_type_quals (arg3_type
));
5497 If the second and third operands are glvalues of the same value
5498 category and have the same type, the result is of that type and
5500 if (((lvalue_p (arg2
) && lvalue_p (arg3
))
5501 || (xvalue_p (arg2
) && xvalue_p (arg3
)))
5502 && same_type_p (arg2_type
, arg3_type
))
5504 result_type
= arg2_type
;
5505 arg2
= mark_lvalue_use (arg2
);
5506 arg3
= mark_lvalue_use (arg3
);
5507 goto valid_operands
;
5512 Otherwise, the result is an rvalue. If the second and third
5513 operand do not have the same type, and either has (possibly
5514 cv-qualified) class type, overload resolution is used to
5515 determine the conversions (if any) to be applied to the operands
5516 (_over.match.oper_, _over.built_). */
5518 if (!same_type_p (arg2_type
, arg3_type
)
5519 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
5525 /* Rearrange the arguments so that add_builtin_candidate only has
5526 to know about two args. In build_builtin_candidate, the
5527 arguments are unscrambled. */
5528 args
->quick_push (arg2
);
5529 args
->quick_push (arg3
);
5530 args
->quick_push (arg1
);
5531 add_builtin_candidates (&candidates
,
5534 ovl_op_identifier (false, COND_EXPR
),
5536 LOOKUP_NORMAL
, complain
);
5540 If the overload resolution fails, the program is
5542 candidates
= splice_viable (candidates
, false, &any_viable_p
);
5545 if (complain
& tf_error
)
5546 error_at (loc
, "operands to %<?:%> have different types %qT and %qT",
5547 arg2_type
, arg3_type
);
5548 return error_mark_node
;
5550 cand
= tourney (candidates
, complain
);
5553 if (complain
& tf_error
)
5555 auto_diagnostic_group d
;
5556 op_error (loc
, COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, FALSE
);
5557 print_z_candidates (loc
, candidates
);
5559 return error_mark_node
;
5564 Otherwise, the conversions thus determined are applied, and
5565 the converted operands are used in place of the original
5566 operands for the remainder of this section. */
5567 conv
= cand
->convs
[0];
5568 arg1
= convert_like (conv
, arg1
, complain
);
5569 conv
= cand
->convs
[1];
5570 arg2
= convert_like (conv
, arg2
, complain
);
5571 arg2_type
= TREE_TYPE (arg2
);
5572 conv
= cand
->convs
[2];
5573 arg3
= convert_like (conv
, arg3
, complain
);
5574 arg3_type
= TREE_TYPE (arg3
);
5579 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5580 and function-to-pointer (_conv.func_) standard conversions are
5581 performed on the second and third operands.
5583 We need to force the lvalue-to-rvalue conversion here for class types,
5584 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5585 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5588 arg2
= force_rvalue (arg2
, complain
);
5589 if (!CLASS_TYPE_P (arg2_type
))
5590 arg2_type
= TREE_TYPE (arg2
);
5592 arg3
= force_rvalue (arg3
, complain
);
5593 if (!CLASS_TYPE_P (arg3_type
))
5594 arg3_type
= TREE_TYPE (arg3
);
5596 if (arg2
== error_mark_node
|| arg3
== error_mark_node
)
5597 return error_mark_node
;
5601 After those conversions, one of the following shall hold:
5603 --The second and third operands have the same type; the result is of
5605 if (same_type_p (arg2_type
, arg3_type
))
5606 result_type
= arg2_type
;
5609 --The second and third operands have arithmetic or enumeration
5610 type; the usual arithmetic conversions are performed to bring
5611 them to a common type, and the result is of that type. */
5612 else if ((ARITHMETIC_TYPE_P (arg2_type
)
5613 || UNSCOPED_ENUM_P (arg2_type
))
5614 && (ARITHMETIC_TYPE_P (arg3_type
)
5615 || UNSCOPED_ENUM_P (arg3_type
)))
5617 /* In this case, there is always a common type. */
5618 result_type
= type_after_usual_arithmetic_conversions (arg2_type
,
5620 if (complain
& tf_warning
)
5621 do_warn_double_promotion (result_type
, arg2_type
, arg3_type
,
5622 "implicit conversion from %qH to %qI to "
5623 "match other result of conditional",
5626 if (TREE_CODE (arg2_type
) == ENUMERAL_TYPE
5627 && TREE_CODE (arg3_type
) == ENUMERAL_TYPE
)
5629 tree stripped_orig_arg2
= tree_strip_any_location_wrapper (orig_arg2
);
5630 tree stripped_orig_arg3
= tree_strip_any_location_wrapper (orig_arg3
);
5631 if (TREE_CODE (stripped_orig_arg2
) == CONST_DECL
5632 && TREE_CODE (stripped_orig_arg3
) == CONST_DECL
5633 && (DECL_CONTEXT (stripped_orig_arg2
)
5634 == DECL_CONTEXT (stripped_orig_arg3
)))
5635 /* Two enumerators from the same enumeration can have different
5636 types when the enumeration is still being defined. */;
5637 else if (complain
& tf_warning
)
5638 warning_at (loc
, OPT_Wenum_compare
, "enumerated mismatch "
5639 "in conditional expression: %qT vs %qT",
5640 arg2_type
, arg3_type
);
5642 else if (extra_warnings
5643 && ((TREE_CODE (arg2_type
) == ENUMERAL_TYPE
5644 && !same_type_p (arg3_type
, type_promotes_to (arg2_type
)))
5645 || (TREE_CODE (arg3_type
) == ENUMERAL_TYPE
5646 && !same_type_p (arg2_type
,
5647 type_promotes_to (arg3_type
)))))
5649 if (complain
& tf_warning
)
5650 warning_at (loc
, OPT_Wextra
, "enumerated and non-enumerated "
5651 "type in conditional expression");
5654 arg2
= perform_implicit_conversion (result_type
, arg2
, complain
);
5655 arg3
= perform_implicit_conversion (result_type
, arg3
, complain
);
5659 --The second and third operands have pointer type, or one has
5660 pointer type and the other is a null pointer constant; pointer
5661 conversions (_conv.ptr_) and qualification conversions
5662 (_conv.qual_) are performed to bring them to their composite
5663 pointer type (_expr.rel_). The result is of the composite
5666 --The second and third operands have pointer to member type, or
5667 one has pointer to member type and the other is a null pointer
5668 constant; pointer to member conversions (_conv.mem_) and
5669 qualification conversions (_conv.qual_) are performed to bring
5670 them to a common type, whose cv-qualification shall match the
5671 cv-qualification of either the second or the third operand.
5672 The result is of the common type. */
5673 else if ((null_ptr_cst_p (arg2
)
5674 && TYPE_PTR_OR_PTRMEM_P (arg3_type
))
5675 || (null_ptr_cst_p (arg3
)
5676 && TYPE_PTR_OR_PTRMEM_P (arg2_type
))
5677 || (TYPE_PTR_P (arg2_type
) && TYPE_PTR_P (arg3_type
))
5678 || (TYPE_PTRDATAMEM_P (arg2_type
) && TYPE_PTRDATAMEM_P (arg3_type
))
5679 || (TYPE_PTRMEMFUNC_P (arg2_type
) && TYPE_PTRMEMFUNC_P (arg3_type
)))
5681 result_type
= composite_pointer_type (loc
,
5682 arg2_type
, arg3_type
, arg2
,
5683 arg3
, CPO_CONDITIONAL_EXPR
,
5685 if (result_type
== error_mark_node
)
5686 return error_mark_node
;
5687 arg2
= perform_implicit_conversion (result_type
, arg2
, complain
);
5688 arg3
= perform_implicit_conversion (result_type
, arg3
, complain
);
5693 if (complain
& tf_error
)
5694 error_at (loc
, "operands to %<?:%> have different types %qT and %qT",
5695 arg2_type
, arg3_type
);
5696 return error_mark_node
;
5699 if (arg2
== error_mark_node
|| arg3
== error_mark_node
)
5700 return error_mark_node
;
5703 if (processing_template_decl
&& is_glvalue
)
5705 /* Let lvalue_kind know this was a glvalue. */
5706 tree arg
= (result_type
== arg2_type
? arg2
: arg3
);
5707 result_type
= cp_build_reference_type (result_type
, xvalue_p (arg
));
5710 result
= build3_loc (loc
, COND_EXPR
, result_type
, arg1
, arg2
, arg3
);
5712 /* If the ARG2 and ARG3 are the same and don't have side-effects,
5713 warn here, because the COND_EXPR will be turned into ARG2. */
5714 if (warn_duplicated_branches
5715 && (complain
& tf_warning
)
5716 && (arg2
== arg3
|| operand_equal_p (arg2
, arg3
, 0)))
5717 warning_at (EXPR_LOCATION (result
), OPT_Wduplicated_branches
,
5718 "this condition has identical branches");
5720 /* We can't use result_type below, as fold might have returned a
5725 /* Expand both sides into the same slot, hopefully the target of
5726 the ?: expression. We used to check for TARGET_EXPRs here,
5727 but now we sometimes wrap them in NOP_EXPRs so the test would
5729 if (CLASS_TYPE_P (TREE_TYPE (result
)))
5730 result
= get_target_expr_sfinae (result
, complain
);
5731 /* If this expression is an rvalue, but might be mistaken for an
5732 lvalue, we must add a NON_LVALUE_EXPR. */
5733 result
= rvalue (result
);
5736 result
= force_paren_expr (result
);
5741 /* Wrapper for above. */
5744 build_conditional_expr (const op_location_t
&loc
,
5745 tree arg1
, tree arg2
, tree arg3
,
5746 tsubst_flags_t complain
)
5749 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
5750 ret
= build_conditional_expr_1 (loc
, arg1
, arg2
, arg3
, complain
);
5751 timevar_cond_stop (TV_OVERLOAD
, subtime
);
5755 /* OPERAND is an operand to an expression. Perform necessary steps
5756 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5760 prep_operand (tree operand
)
5764 if (CLASS_TYPE_P (TREE_TYPE (operand
))
5765 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand
)))
5766 /* Make sure the template type is instantiated now. */
5767 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand
)));
5773 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5774 OVERLOAD) to the CANDIDATES, returning an updated list of
5775 CANDIDATES. The ARGS are the arguments provided to the call;
5776 if FIRST_ARG is non-null it is the implicit object argument,
5777 otherwise the first element of ARGS is used if needed. The
5778 EXPLICIT_TARGS are explicit template arguments provided.
5779 TEMPLATE_ONLY is true if only template functions should be
5780 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5781 add_function_candidate. */
5784 add_candidates (tree fns
, tree first_arg
, const vec
<tree
, va_gc
> *args
,
5786 tree explicit_targs
, bool template_only
,
5787 tree conversion_path
, tree access_path
,
5789 struct z_candidate
**candidates
,
5790 tsubst_flags_t complain
)
5793 const vec
<tree
, va_gc
> *non_static_args
;
5794 bool check_list_ctor
= false;
5795 bool check_converting
= false;
5796 unification_kind_t strict
;
5801 /* Precalculate special handling of constructors and conversion ops. */
5802 tree fn
= OVL_FIRST (fns
);
5803 if (DECL_CONV_FN_P (fn
))
5805 check_list_ctor
= false;
5806 check_converting
= (flags
& LOOKUP_ONLYCONVERTING
) != 0;
5807 if (flags
& LOOKUP_NO_CONVERSION
)
5808 /* We're doing return_type(x). */
5809 strict
= DEDUCE_CONV
;
5811 /* We're doing x.operator return_type(). */
5812 strict
= DEDUCE_EXACT
;
5813 /* [over.match.funcs] For conversion functions, the function
5814 is considered to be a member of the class of the implicit
5815 object argument for the purpose of defining the type of
5816 the implicit object parameter. */
5817 ctype
= TYPE_MAIN_VARIANT (TREE_TYPE (first_arg
));
5821 if (DECL_CONSTRUCTOR_P (fn
))
5823 check_list_ctor
= (flags
& LOOKUP_LIST_ONLY
) != 0;
5824 /* For list-initialization we consider explicit constructors
5825 and complain if one is chosen. */
5827 = ((flags
& (LOOKUP_ONLYCONVERTING
|LOOKUP_LIST_INIT_CTOR
))
5828 == LOOKUP_ONLYCONVERTING
);
5830 strict
= DEDUCE_CALL
;
5831 ctype
= conversion_path
? BINFO_TYPE (conversion_path
) : NULL_TREE
;
5835 non_static_args
= args
;
5837 /* Delay creating the implicit this parameter until it is needed. */
5838 non_static_args
= NULL
;
5840 for (lkp_iterator
iter (fns
); iter
; ++iter
)
5844 if (check_converting
&& DECL_NONCONVERTING_P (fn
))
5846 if (check_list_ctor
&& !is_list_ctor (fn
))
5849 tree fn_first_arg
= NULL_TREE
;
5850 const vec
<tree
, va_gc
> *fn_args
= args
;
5852 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
5854 /* Figure out where the object arg comes from. If this
5855 function is a non-static member and we didn't get an
5856 implicit object argument, move it out of args. */
5857 if (first_arg
== NULL_TREE
)
5861 vec
<tree
, va_gc
> *tempvec
;
5862 vec_alloc (tempvec
, args
->length () - 1);
5863 for (ix
= 1; args
->iterate (ix
, &arg
); ++ix
)
5864 tempvec
->quick_push (arg
);
5865 non_static_args
= tempvec
;
5866 first_arg
= (*args
)[0];
5869 fn_first_arg
= first_arg
;
5870 fn_args
= non_static_args
;
5873 /* Don't bother reversing an operator with two identical parameters. */
5874 else if (vec_safe_length (args
) == 2 && (flags
& LOOKUP_REVERSED
))
5876 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5877 if (same_type_p (TREE_VALUE (parmlist
),
5878 TREE_VALUE (TREE_CHAIN (parmlist
))))
5882 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
5883 add_template_candidate (candidates
,
5895 else if (!template_only
)
5896 add_function_candidate (candidates
,
5909 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
5910 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
5913 op_is_ordered (tree_code code
)
5919 return (flag_strong_eval_order
> 1 ? -1 : 0);
5923 return (flag_strong_eval_order
> 1 ? 1 : 0);
5926 // Not overloadable (yet).
5928 // Only one argument.
5935 return (flag_strong_eval_order
? 1 : 0);
5942 /* Subroutine of build_new_op_1: Add to CANDIDATES all candidates for the
5943 operator indicated by CODE/CODE2. This function calls itself recursively to
5944 handle C++20 rewritten comparison operator candidates. */
5947 add_operator_candidates (z_candidate
**candidates
,
5948 tree_code code
, tree_code code2
,
5949 vec
<tree
, va_gc
> *arglist
,
5950 int flags
, tsubst_flags_t complain
)
5952 z_candidate
*start_candidates
= *candidates
;
5953 bool ismodop
= code2
!= ERROR_MARK
;
5954 tree fnname
= ovl_op_identifier (ismodop
, ismodop
? code2
: code
);
5956 /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
5957 rewrite from, and also when we're looking for the e.g. < operator to use
5958 on the result of <=>. In the latter case, we don't want the flag set in
5959 the candidate, we just want to suppress looking for rewrites. */
5960 bool rewritten
= (flags
& LOOKUP_REWRITTEN
);
5961 if (rewritten
&& code
!= EQ_EXPR
&& code
!= SPACESHIP_EXPR
)
5962 flags
&= ~LOOKUP_REWRITTEN
;
5964 bool memonly
= false;
5967 /* =, ->, [], () must be non-static member functions. */
5969 if (code2
!= NOP_EXPR
)
5981 /* Add namespace-scope operators to the list of functions to
5985 tree fns
= lookup_name_real (fnname
, LOOK_where::BLOCK_NAMESPACE
,
5987 fns
= lookup_arg_dependent (fnname
, fns
, arglist
);
5988 add_candidates (fns
, NULL_TREE
, arglist
, NULL_TREE
,
5989 NULL_TREE
, false, NULL_TREE
, NULL_TREE
,
5990 flags
, candidates
, complain
);
5993 /* Add class-member operators to the candidate set. */
5994 tree arg1_type
= TREE_TYPE ((*arglist
)[0]);
5995 unsigned nargs
= arglist
->length () > 1 ? 2 : 1;
5996 tree arg2_type
= nargs
> 1 ? TREE_TYPE ((*arglist
)[1]) : NULL_TREE
;
5997 if (CLASS_TYPE_P (arg1_type
))
5999 tree fns
= lookup_fnfields (arg1_type
, fnname
, 1, complain
);
6000 if (fns
== error_mark_node
)
6001 return error_mark_node
;
6003 add_candidates (BASELINK_FUNCTIONS (fns
),
6004 NULL_TREE
, arglist
, NULL_TREE
,
6006 BASELINK_BINFO (fns
),
6007 BASELINK_ACCESS_BINFO (fns
),
6008 flags
, candidates
, complain
);
6010 /* Per [over.match.oper]3.2, if no operand has a class type, then
6011 only non-member functions that have type T1 or reference to
6012 cv-qualified-opt T1 for the first argument, if the first argument
6013 has an enumeration type, or T2 or reference to cv-qualified-opt
6014 T2 for the second argument, if the second argument has an
6015 enumeration type. Filter out those that don't match. */
6016 else if (! arg2_type
|| ! CLASS_TYPE_P (arg2_type
))
6018 struct z_candidate
**candp
, **next
;
6020 for (candp
= candidates
; *candp
!= start_candidates
; candp
= next
)
6023 z_candidate
*cand
= *candp
;
6026 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (cand
->fn
));
6028 for (i
= 0; i
< nargs
; ++i
)
6030 tree parmtype
= TREE_VALUE (parmlist
);
6031 tree argtype
= unlowered_expr_type ((*arglist
)[i
]);
6033 if (TYPE_REF_P (parmtype
))
6034 parmtype
= TREE_TYPE (parmtype
);
6035 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
6036 && (same_type_ignoring_top_level_qualifiers_p
6037 (argtype
, parmtype
)))
6040 parmlist
= TREE_CHAIN (parmlist
);
6043 /* No argument has an appropriate type, so remove this
6044 candidate function from the list. */
6047 *candp
= cand
->next
;
6055 /* The standard says to rewrite built-in candidates, too,
6056 but there's no point. */
6057 add_builtin_candidates (candidates
, code
, code2
, fnname
, arglist
,
6060 /* Maybe add C++20 rewritten comparison candidates. */
6061 tree_code rewrite_code
= ERROR_MARK
;
6062 if (cxx_dialect
>= cxx20
6064 && (OVERLOAD_TYPE_P (arg1_type
) || OVERLOAD_TYPE_P (arg2_type
)))
6071 case SPACESHIP_EXPR
:
6072 rewrite_code
= SPACESHIP_EXPR
;
6077 rewrite_code
= EQ_EXPR
;
6085 flags
|= LOOKUP_REWRITTEN
;
6086 if (rewrite_code
!= code
)
6087 /* Add rewritten candidates in same order. */
6088 add_operator_candidates (candidates
, rewrite_code
, ERROR_MARK
,
6089 arglist
, flags
, complain
);
6091 z_candidate
*save_cand
= *candidates
;
6093 /* Add rewritten candidates in reverse order. */
6094 flags
|= LOOKUP_REVERSED
;
6095 vec
<tree
,va_gc
> *revlist
= make_tree_vector ();
6096 revlist
->quick_push ((*arglist
)[1]);
6097 revlist
->quick_push ((*arglist
)[0]);
6098 add_operator_candidates (candidates
, rewrite_code
, ERROR_MARK
,
6099 revlist
, flags
, complain
);
6101 /* Release the vec if we didn't add a candidate that uses it. */
6102 for (z_candidate
*c
= *candidates
; c
!= save_cand
; c
= c
->next
)
6103 if (c
->args
== revlist
)
6108 release_tree_vector (revlist
);
6116 build_new_op_1 (const op_location_t
&loc
, enum tree_code code
, int flags
,
6117 tree arg1
, tree arg2
, tree arg3
, tree
*overload
,
6118 tsubst_flags_t complain
)
6120 struct z_candidate
*candidates
= 0, *cand
;
6121 vec
<tree
, va_gc
> *arglist
;
6122 tree result
= NULL_TREE
;
6123 bool result_valid_p
= false;
6124 enum tree_code code2
= ERROR_MARK
;
6125 enum tree_code code_orig_arg1
= ERROR_MARK
;
6126 enum tree_code code_orig_arg2
= ERROR_MARK
;
6132 if (error_operand_p (arg1
)
6133 || error_operand_p (arg2
)
6134 || error_operand_p (arg3
))
6135 return error_mark_node
;
6137 bool ismodop
= code
== MODIFY_EXPR
;
6140 code2
= TREE_CODE (arg3
);
6144 tree arg1_type
= unlowered_expr_type (arg1
);
6145 tree arg2_type
= arg2
? unlowered_expr_type (arg2
) : NULL_TREE
;
6147 arg1
= prep_operand (arg1
);
6153 case VEC_DELETE_EXPR
:
6155 /* Use build_op_new_call and build_op_delete_call instead. */
6159 /* Use build_op_call instead. */
6162 case TRUTH_ORIF_EXPR
:
6163 case TRUTH_ANDIF_EXPR
:
6164 case TRUTH_AND_EXPR
:
6166 /* These are saved for the sake of warn_logical_operator. */
6167 code_orig_arg1
= TREE_CODE (arg1
);
6168 code_orig_arg2
= TREE_CODE (arg2
);
6176 /* These are saved for the sake of maybe_warn_bool_compare. */
6177 code_orig_arg1
= TREE_CODE (arg1_type
);
6178 code_orig_arg2
= TREE_CODE (arg2_type
);
6185 arg2
= prep_operand (arg2
);
6186 arg3
= prep_operand (arg3
);
6188 if (code
== COND_EXPR
)
6189 /* Use build_conditional_expr instead. */
6191 else if (! OVERLOAD_TYPE_P (arg1_type
)
6192 && (! arg2
|| ! OVERLOAD_TYPE_P (arg2_type
)))
6195 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6197 arg2
= integer_zero_node
;
6198 arg2_type
= integer_type_node
;
6201 vec_alloc (arglist
, 3);
6202 arglist
->quick_push (arg1
);
6203 if (arg2
!= NULL_TREE
)
6204 arglist
->quick_push (arg2
);
6205 if (arg3
!= NULL_TREE
)
6206 arglist
->quick_push (arg3
);
6208 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6209 p
= conversion_obstack_alloc (0);
6211 result
= add_operator_candidates (&candidates
, code
, code2
, arglist
,
6213 if (result
== error_mark_node
)
6214 goto user_defined_result_ready
;
6220 /* For these, the built-in candidates set is empty
6221 [over.match.oper]/3. We don't want non-strict matches
6222 because exact matches are always possible with built-in
6223 operators. The built-in candidate set for COMPONENT_REF
6224 would be empty too, but since there are no such built-in
6225 operators, we accept non-strict matches for them. */
6234 candidates
= splice_viable (candidates
, strict_p
, &any_viable_p
);
6239 case POSTINCREMENT_EXPR
:
6240 case POSTDECREMENT_EXPR
:
6241 /* Don't try anything fancy if we're not allowed to produce
6243 if (!(complain
& tf_error
))
6244 return error_mark_node
;
6246 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
6247 distinguish between prefix and postfix ++ and
6248 operator++() was used for both, so we allow this with
6252 tree fnname
= ovl_op_identifier (ismodop
, ismodop
? code2
: code
);
6253 const char *msg
= (flag_permissive
)
6254 ? G_("no %<%D(int)%> declared for postfix %qs,"
6255 " trying prefix operator instead")
6256 : G_("no %<%D(int)%> declared for postfix %qs");
6257 permerror (loc
, msg
, fnname
, OVL_OP_INFO (false, code
)->name
);
6260 if (!flag_permissive
)
6261 return error_mark_node
;
6263 if (code
== POSTINCREMENT_EXPR
)
6264 code
= PREINCREMENT_EXPR
;
6266 code
= PREDECREMENT_EXPR
;
6267 result
= build_new_op_1 (loc
, code
, flags
, arg1
, NULL_TREE
,
6268 NULL_TREE
, overload
, complain
);
6271 /* The caller will deal with these. */
6277 result_valid_p
= true;
6281 if (complain
& tf_error
)
6283 /* If one of the arguments of the operator represents
6284 an invalid use of member function pointer, try to report
6285 a meaningful error ... */
6286 if (invalid_nonstatic_memfn_p (loc
, arg1
, tf_error
)
6287 || invalid_nonstatic_memfn_p (loc
, arg2
, tf_error
)
6288 || invalid_nonstatic_memfn_p (loc
, arg3
, tf_error
))
6289 /* We displayed the error message. */;
6292 /* ... Otherwise, report the more generic
6293 "no matching operator found" error */
6294 auto_diagnostic_group d
;
6295 op_error (loc
, code
, code2
, arg1
, arg2
, arg3
, FALSE
);
6296 print_z_candidates (loc
, candidates
);
6299 result
= error_mark_node
;
6305 cand
= tourney (candidates
, complain
);
6308 if (complain
& tf_error
)
6310 auto_diagnostic_group d
;
6311 op_error (loc
, code
, code2
, arg1
, arg2
, arg3
, TRUE
);
6312 print_z_candidates (loc
, candidates
);
6314 result
= error_mark_node
;
6316 else if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
6319 *overload
= cand
->fn
;
6321 if (resolve_args (arglist
, complain
) == NULL
)
6322 result
= error_mark_node
;
6325 tsubst_flags_t ocomplain
= complain
;
6326 if (cand
->rewritten ())
6327 /* We'll wrap this call in another one. */
6328 ocomplain
&= ~tf_decltype
;
6329 if (cand
->reversed ())
6331 /* We swapped these in add_candidate, swap them back now. */
6332 std::swap (cand
->convs
[0], cand
->convs
[1]);
6333 if (cand
->fn
== current_function_decl
)
6334 warning_at (loc
, 0, "in C++20 this comparison calls the "
6335 "current function recursively with reversed "
6338 result
= build_over_call (cand
, LOOKUP_NORMAL
, ocomplain
);
6341 if (trivial_fn_p (cand
->fn
) || DECL_IMMEDIATE_FUNCTION_P (cand
->fn
))
6342 /* There won't be a CALL_EXPR. */;
6343 else if (result
&& result
!= error_mark_node
)
6345 tree call
= extract_call_expr (result
);
6346 CALL_EXPR_OPERATOR_SYNTAX (call
) = true;
6348 if (processing_template_decl
&& DECL_HIDDEN_FRIEND_P (cand
->fn
))
6349 /* This prevents build_new_function_call from discarding this
6350 function during instantiation of the enclosing template. */
6351 KOENIG_LOOKUP_P (call
) = 1;
6353 /* Specify evaluation order as per P0145R2. */
6354 CALL_EXPR_ORDERED_ARGS (call
) = false;
6355 switch (op_is_ordered (code
))
6358 CALL_EXPR_REVERSE_ARGS (call
) = true;
6362 CALL_EXPR_ORDERED_ARGS (call
) = true;
6370 /* If this was a C++20 rewritten comparison, adjust the result. */
6371 if (cand
->rewritten ())
6373 /* FIXME build_min_non_dep_op_overload can't handle rewrites. */
6375 *overload
= NULL_TREE
;
6379 gcc_checking_assert (cand
->reversed ());
6382 /* If a rewritten operator== candidate is selected by
6383 overload resolution for an operator @, its return type
6384 shall be cv bool.... */
6385 if (TREE_CODE (TREE_TYPE (result
)) != BOOLEAN_TYPE
)
6387 if (complain
& tf_error
)
6389 auto_diagnostic_group d
;
6390 error_at (loc
, "return type of %qD is not %qs",
6392 inform (loc
, "used as rewritten candidate for "
6393 "comparison of %qT and %qT",
6394 arg1_type
, arg2_type
);
6396 result
= error_mark_node
;
6398 else if (code
== NE_EXPR
)
6399 /* !(y == x) or !(x == y) */
6400 result
= build1_loc (loc
, TRUTH_NOT_EXPR
,
6401 boolean_type_node
, result
);
6404 /* If a rewritten operator<=> candidate is selected by
6405 overload resolution for an operator @, x @ y is
6406 interpreted as 0 @ (y <=> x) if the selected candidate is
6407 a synthesized candidate with reversed order of parameters,
6408 or (x <=> y) @ 0 otherwise, using the selected rewritten
6409 operator<=> candidate. */
6410 case SPACESHIP_EXPR
:
6411 if (!cand
->reversed ())
6412 /* We're in the build_new_op call below for an outer
6413 reversed call; we don't need to do anything more. */
6422 tree rhs
= integer_zero_node
;
6423 if (cand
->reversed ())
6424 std::swap (lhs
, rhs
);
6425 warning_sentinel
ws (warn_zero_as_null_pointer_constant
);
6426 result
= build_new_op (loc
, code
,
6427 LOOKUP_NORMAL
|LOOKUP_REWRITTEN
,
6428 lhs
, rhs
, NULL_TREE
,
6440 /* Give any warnings we noticed during overload resolution. */
6441 if (cand
->warnings
&& (complain
& tf_warning
))
6443 struct candidate_warning
*w
;
6444 for (w
= cand
->warnings
; w
; w
= w
->next
)
6445 joust (cand
, w
->loser
, 1, complain
);
6448 /* Check for comparison of different enum types. */
6457 if (TREE_CODE (arg1_type
) == ENUMERAL_TYPE
6458 && TREE_CODE (arg2_type
) == ENUMERAL_TYPE
6459 && (TYPE_MAIN_VARIANT (arg1_type
)
6460 != TYPE_MAIN_VARIANT (arg2_type
))
6461 && (complain
& tf_warning
))
6462 warning_at (loc
, OPT_Wenum_compare
,
6463 "comparison between %q#T and %q#T",
6464 arg1_type
, arg2_type
);
6470 /* "If a built-in candidate is selected by overload resolution, the
6471 operands of class type are converted to the types of the
6472 corresponding parameters of the selected operation function,
6473 except that the second standard conversion sequence of a
6474 user-defined conversion sequence (12.3.3.1.2) is not applied." */
6475 conv
= cand
->convs
[0];
6476 if (conv
->user_conv_p
)
6478 conv
= strip_standard_conversion (conv
);
6479 arg1
= convert_like (conv
, arg1
, complain
);
6484 conv
= cand
->convs
[1];
6485 if (conv
->user_conv_p
)
6487 conv
= strip_standard_conversion (conv
);
6488 arg2
= convert_like (conv
, arg2
, complain
);
6494 conv
= cand
->convs
[2];
6495 if (conv
->user_conv_p
)
6497 conv
= strip_standard_conversion (conv
);
6498 arg3
= convert_like (conv
, arg3
, complain
);
6504 user_defined_result_ready
:
6506 /* Free all the conversions we allocated. */
6507 obstack_free (&conversion_obstack
, p
);
6509 if (result
|| result_valid_p
)
6516 return cp_build_modify_expr (loc
, arg1
, code2
, arg2
, complain
);
6519 return cp_build_indirect_ref (loc
, arg1
, RO_UNARY_STAR
, complain
);
6521 case TRUTH_ANDIF_EXPR
:
6522 case TRUTH_ORIF_EXPR
:
6523 case TRUTH_AND_EXPR
:
6525 if (complain
& tf_warning
)
6526 warn_logical_operator (loc
, code
, boolean_type_node
,
6527 code_orig_arg1
, arg1
,
6528 code_orig_arg2
, arg2
);
6536 if ((complain
& tf_warning
)
6537 && ((code_orig_arg1
== BOOLEAN_TYPE
)
6538 ^ (code_orig_arg2
== BOOLEAN_TYPE
)))
6539 maybe_warn_bool_compare (loc
, code
, arg1
, arg2
);
6540 if (complain
& tf_warning
&& warn_tautological_compare
)
6541 warn_tautological_cmp (loc
, code
, arg1
, arg2
);
6543 case SPACESHIP_EXPR
:
6547 case TRUNC_DIV_EXPR
:
6552 case TRUNC_MOD_EXPR
:
6556 return cp_build_binary_op (loc
, code
, arg1
, arg2
, complain
);
6558 case UNARY_PLUS_EXPR
:
6561 case TRUTH_NOT_EXPR
:
6562 case PREINCREMENT_EXPR
:
6563 case POSTINCREMENT_EXPR
:
6564 case PREDECREMENT_EXPR
:
6565 case POSTDECREMENT_EXPR
:
6570 return cp_build_unary_op (code
, arg1
, false, complain
);
6573 return cp_build_array_ref (input_location
, arg1
, arg2
, complain
);
6576 return build_m_component_ref (cp_build_indirect_ref (loc
, arg1
,
6581 /* The caller will deal with these. */
6593 /* Wrapper for above. */
6596 build_new_op (const op_location_t
&loc
, enum tree_code code
, int flags
,
6597 tree arg1
, tree arg2
, tree arg3
,
6598 tree
*overload
, tsubst_flags_t complain
)
6601 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
6602 ret
= build_new_op_1 (loc
, code
, flags
, arg1
, arg2
, arg3
,
6603 overload
, complain
);
6604 timevar_cond_stop (TV_OVERLOAD
, subtime
);
6608 /* CALL was returned by some call-building function; extract the actual
6609 CALL_EXPR from any bits that have been tacked on, e.g. by
6610 convert_from_reference. */
6613 extract_call_expr (tree call
)
6615 while (TREE_CODE (call
) == COMPOUND_EXPR
)
6616 call
= TREE_OPERAND (call
, 1);
6617 if (REFERENCE_REF_P (call
))
6618 call
= TREE_OPERAND (call
, 0);
6619 if (TREE_CODE (call
) == TARGET_EXPR
)
6620 call
= TARGET_EXPR_INITIAL (call
);
6621 if (cxx_dialect
>= cxx20
)
6622 switch (TREE_CODE (call
))
6624 /* C++20 rewritten comparison operators. */
6625 case TRUTH_NOT_EXPR
:
6626 call
= TREE_OPERAND (call
, 0);
6632 case SPACESHIP_EXPR
:
6634 tree op0
= TREE_OPERAND (call
, 0);
6635 if (integer_zerop (op0
))
6636 call
= TREE_OPERAND (call
, 1);
6644 gcc_assert (TREE_CODE (call
) == CALL_EXPR
6645 || TREE_CODE (call
) == AGGR_INIT_EXPR
6646 || call
== error_mark_node
);
6650 /* Returns true if FN has two parameters, of which the second has type
6654 second_parm_is_size_t (tree fn
)
6656 tree t
= FUNCTION_ARG_CHAIN (fn
);
6657 if (!t
|| !same_type_p (TREE_VALUE (t
), size_type_node
))
6660 if (t
== void_list_node
)
6665 /* True if T, an allocation function, has std::align_val_t as its second
6669 aligned_allocation_fn_p (tree t
)
6671 if (!aligned_new_threshold
)
6674 tree a
= FUNCTION_ARG_CHAIN (t
);
6675 return (a
&& same_type_p (TREE_VALUE (a
), align_type_node
));
6678 /* True if T is std::destroying_delete_t. */
6681 std_destroying_delete_t_p (tree t
)
6683 return (TYPE_CONTEXT (t
) == std_node
6684 && id_equal (TYPE_IDENTIFIER (t
), "destroying_delete_t"));
6687 /* A deallocation function with at least two parameters whose second parameter
6688 type is of type std::destroying_delete_t is a destroying operator delete. A
6689 destroying operator delete shall be a class member function named operator
6690 delete. [ Note: Array deletion cannot use a destroying operator
6691 delete. --end note ] */
6694 destroying_delete_p (tree t
)
6696 tree a
= TYPE_ARG_TYPES (TREE_TYPE (t
));
6697 if (!a
|| !TREE_CHAIN (a
))
6699 tree type
= TREE_VALUE (TREE_CHAIN (a
));
6700 return std_destroying_delete_t_p (type
) ? type
: NULL_TREE
;
6710 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
6711 function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
6712 non-null, also set *DI. */
6715 usual_deallocation_fn_p (tree t
, dealloc_info
*di
)
6717 if (di
) *di
= dealloc_info();
6719 /* A template instance is never a usual deallocation function,
6720 regardless of its signature. */
6721 if (TREE_CODE (t
) == TEMPLATE_DECL
6722 || primary_template_specialization_p (t
))
6725 /* A usual deallocation function is a deallocation function whose parameters
6727 - optionally, a parameter of type std::destroying_delete_t, then
6728 - optionally, a parameter of type std::size_t, then
6729 - optionally, a parameter of type std::align_val_t. */
6730 bool global
= DECL_NAMESPACE_SCOPE_P (t
);
6731 tree chain
= FUNCTION_ARG_CHAIN (t
);
6732 if (chain
&& destroying_delete_p (t
))
6734 if (di
) di
->destroying
= TREE_VALUE (chain
);
6735 chain
= TREE_CHAIN (chain
);
6738 && (!global
|| flag_sized_deallocation
)
6739 && same_type_p (TREE_VALUE (chain
), size_type_node
))
6741 if (di
) di
->sized
= true;
6742 chain
= TREE_CHAIN (chain
);
6744 if (chain
&& aligned_new_threshold
6745 && same_type_p (TREE_VALUE (chain
), align_type_node
))
6747 if (di
) di
->aligned
= true;
6748 chain
= TREE_CHAIN (chain
);
6750 return (chain
== void_list_node
);
6753 /* Just return whether FN is a usual deallocation function. */
6756 usual_deallocation_fn_p (tree fn
)
6758 return usual_deallocation_fn_p (fn
, NULL
);
6761 /* Build a call to operator delete. This has to be handled very specially,
6762 because the restrictions on what signatures match are different from all
6763 other call instances. For a normal delete, only a delete taking (void *)
6764 or (void *, size_t) is accepted. For a placement delete, only an exact
6765 match with the placement new is accepted.
6767 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
6768 ADDR is the pointer to be deleted.
6769 SIZE is the size of the memory block to be deleted.
6770 GLOBAL_P is true if the delete-expression should not consider
6771 class-specific delete operators.
6772 PLACEMENT is the corresponding placement new call, or NULL_TREE.
6774 If this call to "operator delete" is being generated as part to
6775 deallocate memory allocated via a new-expression (as per [expr.new]
6776 which requires that if the initialization throws an exception then
6777 we call a deallocation function), then ALLOC_FN is the allocation
6781 build_op_delete_call (enum tree_code code
, tree addr
, tree size
,
6782 bool global_p
, tree placement
,
6783 tree alloc_fn
, tsubst_flags_t complain
)
6785 tree fn
= NULL_TREE
;
6786 tree fns
, fnname
, type
, t
;
6787 dealloc_info di_fn
= { };
6789 if (addr
== error_mark_node
)
6790 return error_mark_node
;
6792 type
= strip_array_types (TREE_TYPE (TREE_TYPE (addr
)));
6794 fnname
= ovl_op_identifier (false, code
);
6796 if (CLASS_TYPE_P (type
)
6797 && COMPLETE_TYPE_P (complete_type (type
))
6801 If the result of the lookup is ambiguous or inaccessible, or if
6802 the lookup selects a placement deallocation function, the
6803 program is ill-formed.
6805 Therefore, we ask lookup_fnfields to complain about ambiguity. */
6807 fns
= lookup_fnfields (TYPE_BINFO (type
), fnname
, 1, complain
);
6808 if (fns
== error_mark_node
)
6809 return error_mark_node
;
6814 if (fns
== NULL_TREE
)
6815 fns
= lookup_name_nonclass (fnname
);
6817 /* Strip const and volatile from addr. */
6819 addr
= cp_convert (ptr_type_node
, addr
, complain
);
6823 /* "A declaration of a placement deallocation function matches the
6824 declaration of a placement allocation function if it has the same
6825 number of parameters and, after parameter transformations (8.3.5),
6826 all parameter types except the first are identical."
6828 So we build up the function type we want and ask instantiate_type
6829 to get it for us. */
6830 t
= FUNCTION_ARG_CHAIN (alloc_fn
);
6831 t
= tree_cons (NULL_TREE
, ptr_type_node
, t
);
6832 t
= build_function_type (void_type_node
, t
);
6834 fn
= instantiate_type (t
, fns
, tf_none
);
6835 if (fn
== error_mark_node
)
6838 fn
= MAYBE_BASELINK_FUNCTIONS (fn
);
6840 /* "If the lookup finds the two-parameter form of a usual deallocation
6841 function (3.7.4.2) and that function, considered as a placement
6842 deallocation function, would have been selected as a match for the
6843 allocation function, the program is ill-formed." */
6844 if (second_parm_is_size_t (fn
))
6846 const char *const msg1
6847 = G_("exception cleanup for this placement new selects "
6848 "non-placement %<operator delete%>");
6849 const char *const msg2
6850 = G_("%qD is a usual (non-placement) deallocation "
6851 "function in C++14 (or with %<-fsized-deallocation%>)");
6853 /* But if the class has an operator delete (void *), then that is
6854 the usual deallocation function, so we shouldn't complain
6855 about using the operator delete (void *, size_t). */
6856 if (DECL_CLASS_SCOPE_P (fn
))
6857 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (fns
));
6861 if (usual_deallocation_fn_p (elt
)
6862 && FUNCTION_ARG_CHAIN (elt
) == void_list_node
)
6865 /* Before C++14 a two-parameter global deallocation function is
6866 always a placement deallocation function, but warn if
6868 else if (!flag_sized_deallocation
)
6870 if (complain
& tf_warning
)
6872 auto_diagnostic_group d
;
6873 if (warning (OPT_Wc__14_compat
, msg1
))
6874 inform (DECL_SOURCE_LOCATION (fn
), msg2
, fn
);
6879 if (complain
& tf_warning_or_error
)
6881 auto_diagnostic_group d
;
6882 if (permerror (input_location
, msg1
))
6884 /* Only mention C++14 for namespace-scope delete. */
6885 if (DECL_NAMESPACE_SCOPE_P (fn
))
6886 inform (DECL_SOURCE_LOCATION (fn
), msg2
, fn
);
6888 inform (DECL_SOURCE_LOCATION (fn
),
6889 "%qD is a usual (non-placement) deallocation "
6894 return error_mark_node
;
6899 /* "Any non-placement deallocation function matches a non-placement
6900 allocation function. If the lookup finds a single matching
6901 deallocation function, that function will be called; otherwise, no
6902 deallocation function will be called." */
6903 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
6906 dealloc_info di_elt
;
6907 if (usual_deallocation_fn_p (elt
, &di_elt
))
6916 /* -- If any of the deallocation functions is a destroying
6917 operator delete, all deallocation functions that are not
6918 destroying operator deletes are eliminated from further
6920 if (di_elt
.destroying
!= di_fn
.destroying
)
6922 if (di_elt
.destroying
)
6930 /* -- If the type has new-extended alignment, a function with a
6931 parameter of type std::align_val_t is preferred; otherwise a
6932 function without such a parameter is preferred. If exactly one
6933 preferred function is found, that function is selected and the
6934 selection process terminates. If more than one preferred
6935 function is found, all non-preferred functions are eliminated
6936 from further consideration. */
6937 if (aligned_new_threshold
)
6939 bool want_align
= type_has_new_extended_alignment (type
);
6940 if (di_elt
.aligned
!= di_fn
.aligned
)
6942 if (want_align
== di_elt
.aligned
)
6951 /* -- If the deallocation functions have class scope, the one
6952 without a parameter of type std::size_t is selected. */
6954 if (DECL_CLASS_SCOPE_P (fn
))
6957 /* -- If the type is complete and if, for the second alternative
6958 (delete array) only, the operand is a pointer to a class type
6959 with a non-trivial destructor or a (possibly multi-dimensional)
6960 array thereof, the function with a parameter of type std::size_t
6963 -- Otherwise, it is unspecified whether a deallocation function
6964 with a parameter of type std::size_t is selected. */
6967 want_size
= COMPLETE_TYPE_P (type
);
6968 if (code
== VEC_DELETE_EXPR
6969 && !TYPE_VEC_NEW_USES_COOKIE (type
))
6970 /* We need a cookie to determine the array size. */
6973 gcc_assert (di_fn
.sized
!= di_elt
.sized
);
6974 if (want_size
== di_elt
.sized
)
6982 /* If we have a matching function, call it. */
6985 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
6987 /* If the FN is a member function, make sure that it is
6989 if (BASELINK_P (fns
))
6990 perform_or_defer_access_check (BASELINK_BINFO (fns
), fn
, fn
,
6993 /* Core issue 901: It's ok to new a type with deleted delete. */
6994 if (DECL_DELETED_FN (fn
) && alloc_fn
)
6999 /* The placement args might not be suitable for overload
7000 resolution at this point, so build the call directly. */
7001 int nargs
= call_expr_nargs (placement
);
7002 tree
*argarray
= XALLOCAVEC (tree
, nargs
);
7005 for (i
= 1; i
< nargs
; i
++)
7006 argarray
[i
] = CALL_EXPR_ARG (placement
, i
);
7007 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
7008 return error_mark_node
;
7009 return build_cxx_call (fn
, nargs
, argarray
, complain
);
7013 tree destroying
= di_fn
.destroying
;
7016 /* Strip const and volatile from addr but retain the type of the
7018 tree rtype
= TREE_TYPE (TREE_TYPE (oaddr
));
7019 rtype
= cv_unqualified (rtype
);
7020 rtype
= TYPE_POINTER_TO (rtype
);
7021 addr
= cp_convert (rtype
, oaddr
, complain
);
7022 destroying
= build_functional_cast (input_location
,
7023 destroying
, NULL_TREE
,
7029 args
->quick_push (addr
);
7031 args
->quick_push (destroying
);
7033 args
->quick_push (size
);
7036 tree al
= build_int_cst (align_type_node
, TYPE_ALIGN_UNIT (type
));
7037 args
->quick_push (al
);
7039 ret
= cp_build_function_call_vec (fn
, &args
, complain
);
7046 If no unambiguous matching deallocation function can be found,
7047 propagating the exception does not cause the object's memory to
7051 if ((complain
& tf_warning
)
7053 warning (0, "no corresponding deallocation function for %qD",
7058 if (complain
& tf_error
)
7059 error ("no suitable %<operator %s%> for %qT",
7060 OVL_OP_INFO (false, code
)->name
, type
);
7061 return error_mark_node
;
7064 /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
7067 If ISSUE_ERROR is true, then issue an error about the
7068 access, followed by a note showing the declaration.
7069 Otherwise, just show the note. */
7072 complain_about_access (tree decl
, tree diag_decl
, bool issue_error
)
7074 if (TREE_PRIVATE (decl
))
7077 error ("%q#D is private within this context", diag_decl
);
7078 inform (DECL_SOURCE_LOCATION (diag_decl
),
7079 "declared private here");
7081 else if (TREE_PROTECTED (decl
))
7084 error ("%q#D is protected within this context", diag_decl
);
7085 inform (DECL_SOURCE_LOCATION (diag_decl
),
7086 "declared protected here");
7091 error ("%q#D is inaccessible within this context", diag_decl
);
7092 inform (DECL_SOURCE_LOCATION (diag_decl
), "declared here");
7096 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
7097 bitwise or of LOOKUP_* values. If any errors are warnings are
7098 generated, set *DIAGNOSTIC_FN to "error" or "warning",
7099 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
7103 build_temp (tree expr
, tree type
, int flags
,
7104 diagnostic_t
*diagnostic_kind
, tsubst_flags_t complain
)
7108 *diagnostic_kind
= DK_UNSPECIFIED
;
7110 /* If the source is a packed field, calling the copy constructor will require
7111 binding the field to the reference parameter to the copy constructor, and
7112 we'll end up with an infinite loop. If we can use a bitwise copy, then
7114 if ((lvalue_kind (expr
) & clk_packed
)
7115 && CLASS_TYPE_P (TREE_TYPE (expr
))
7116 && !type_has_nontrivial_copy_init (TREE_TYPE (expr
)))
7117 return get_target_expr_sfinae (expr
, complain
);
7119 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
7120 releasing_vec
args (make_tree_vector_single (expr
));
7121 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
7122 &args
, type
, flags
, complain
);
7123 if (warningcount
+ werrorcount
> savew
)
7124 *diagnostic_kind
= DK_WARNING
;
7125 else if (errorcount
> savee
)
7126 *diagnostic_kind
= DK_ERROR
;
7130 /* Get any location for EXPR, falling back to input_location.
7132 If the result is in a system header and is the virtual location for
7133 a token coming from the expansion of a macro, unwind it to the
7134 location of the expansion point of the macro (e.g. to avoid the
7135 diagnostic being suppressed for expansions of NULL where "NULL" is
7136 in a system header). */
7139 get_location_for_expr_unwinding_for_system_header (tree expr
)
7141 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
7142 loc
= expansion_point_location_if_in_system_header (loc
);
7146 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
7147 Also handle a subset of zero as null warnings.
7148 EXPR is implicitly converted to type TOTYPE.
7149 FN and ARGNUM are used for diagnostics. */
7152 conversion_null_warnings (tree totype
, tree expr
, tree fn
, int argnum
)
7154 /* Issue warnings about peculiar, but valid, uses of NULL. */
7155 if (TREE_CODE (totype
) != BOOLEAN_TYPE
7156 && ARITHMETIC_TYPE_P (totype
)
7157 && null_node_p (expr
))
7159 location_t loc
= get_location_for_expr_unwinding_for_system_header (expr
);
7162 auto_diagnostic_group d
;
7163 if (warning_at (loc
, OPT_Wconversion_null
,
7164 "passing NULL to non-pointer argument %P of %qD",
7166 inform (get_fndecl_argument_location (fn
, argnum
),
7170 warning_at (loc
, OPT_Wconversion_null
,
7171 "converting to non-pointer type %qT from NULL", totype
);
7174 /* Issue warnings if "false" is converted to a NULL pointer */
7175 else if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
7176 && TYPE_PTR_P (totype
))
7178 location_t loc
= get_location_for_expr_unwinding_for_system_header (expr
);
7181 auto_diagnostic_group d
;
7182 if (warning_at (loc
, OPT_Wconversion_null
,
7183 "converting %<false%> to pointer type for argument "
7184 "%P of %qD", argnum
, fn
))
7185 inform (get_fndecl_argument_location (fn
, argnum
),
7189 warning_at (loc
, OPT_Wconversion_null
,
7190 "converting %<false%> to pointer type %qT", totype
);
7192 /* Handle zero as null pointer warnings for cases other
7193 than EQ_EXPR and NE_EXPR */
7194 else if ((TYPE_PTR_OR_PTRMEM_P (totype
) || NULLPTR_TYPE_P (totype
))
7195 && null_ptr_cst_p (expr
))
7197 location_t loc
= get_location_for_expr_unwinding_for_system_header (expr
);
7198 maybe_warn_zero_as_null_pointer_constant (expr
, loc
);
7202 /* We gave a diagnostic during a conversion. If this was in the second
7203 standard conversion sequence of a user-defined conversion sequence, say
7204 which user-defined conversion. */
7207 maybe_print_user_conv_context (conversion
*convs
)
7209 if (convs
->user_conv_p
)
7210 for (conversion
*t
= convs
; t
; t
= next_conversion (t
))
7211 if (t
->kind
== ck_user
)
7213 print_z_candidate (0, N_(" after user-defined conversion:"),
7219 /* Locate the parameter with the given index within FNDECL.
7220 ARGNUM is zero based, -1 indicates the `this' argument of a method.
7221 Return the location of the FNDECL itself if there are problems. */
7224 get_fndecl_argument_location (tree fndecl
, int argnum
)
7226 /* The locations of implicitly-declared functions are likely to be
7227 more meaningful than those of their parameters. */
7228 if (DECL_ARTIFICIAL (fndecl
))
7229 return DECL_SOURCE_LOCATION (fndecl
);
7234 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
7235 for (i
= 0, param
= FUNCTION_FIRST_USER_PARM (fndecl
);
7236 i
< argnum
&& param
;
7237 i
++, param
= TREE_CHAIN (param
))
7240 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
7241 return the location of FNDECL. */
7243 return DECL_SOURCE_LOCATION (fndecl
);
7245 return DECL_SOURCE_LOCATION (param
);
7248 /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
7249 within its declaration (or the fndecl itself if something went
7253 maybe_inform_about_fndecl_for_bogus_argument_init (tree fn
, int argnum
)
7256 inform (get_fndecl_argument_location (fn
, argnum
),
7257 " initializing argument %P of %qD", argnum
, fn
);
7260 /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
7261 the conversion, EXPR is the expression we're converting. */
7264 maybe_warn_array_conv (location_t loc
, conversion
*c
, tree expr
)
7266 if (cxx_dialect
>= cxx20
)
7269 tree type
= TREE_TYPE (expr
);
7270 type
= strip_pointer_operator (type
);
7272 if (TREE_CODE (type
) != ARRAY_TYPE
7273 || TYPE_DOMAIN (type
) == NULL_TREE
)
7276 if (conv_binds_to_array_of_unknown_bound (c
))
7277 pedwarn (loc
, OPT_Wpedantic
, "conversions to arrays of unknown bound "
7278 "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
7281 /* We call this recursively in convert_like_internal. */
7282 static tree
convert_like (conversion
*, tree
, tree
, int, bool, bool,
7285 /* Perform the conversions in CONVS on the expression EXPR. FN and
7286 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
7287 indicates the `this' argument of a method. INNER is nonzero when
7288 being called to continue a conversion chain. It is negative when a
7289 reference binding will be applied, positive otherwise. If
7290 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
7291 conversions will be emitted if appropriate. If C_CAST_P is true,
7292 this conversion is coming from a C-style cast; in that case,
7293 conversions to inaccessible bases are permitted. */
7296 convert_like_internal (conversion
*convs
, tree expr
, tree fn
, int argnum
,
7297 bool issue_conversion_warnings
, bool c_cast_p
,
7298 tsubst_flags_t complain
)
7300 tree totype
= convs
->type
;
7301 diagnostic_t diag_kind
;
7303 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7305 if (convs
->bad_p
&& !(complain
& tf_error
))
7306 return error_mark_node
;
7309 && convs
->kind
!= ck_user
7310 && convs
->kind
!= ck_list
7311 && convs
->kind
!= ck_ambig
7312 && (convs
->kind
!= ck_ref_bind
7313 || (convs
->user_conv_p
&& next_conversion (convs
)->bad_p
))
7314 && (convs
->kind
!= ck_rvalue
7315 || SCALAR_TYPE_P (totype
))
7316 && convs
->kind
!= ck_base
)
7318 bool complained
= false;
7319 conversion
*t
= convs
;
7321 /* Give a helpful error if this is bad because of excess braces. */
7322 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
7323 && SCALAR_TYPE_P (totype
)
7324 && CONSTRUCTOR_NELTS (expr
) > 0
7325 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr
, 0)->value
))
7327 complained
= permerror (loc
, "too many braces around initializer "
7329 while (BRACE_ENCLOSED_INITIALIZER_P (expr
)
7330 && CONSTRUCTOR_NELTS (expr
) == 1)
7331 expr
= CONSTRUCTOR_ELT (expr
, 0)->value
;
7334 /* Give a helpful error if this is bad because a conversion to bool
7335 from std::nullptr_t requires direct-initialization. */
7336 if (NULLPTR_TYPE_P (TREE_TYPE (expr
))
7337 && TREE_CODE (totype
) == BOOLEAN_TYPE
)
7338 complained
= permerror (loc
, "converting to %qH from %qI requires "
7339 "direct-initialization",
7340 totype
, TREE_TYPE (expr
));
7342 for (; t
; t
= next_conversion (t
))
7344 if (t
->kind
== ck_user
&& t
->cand
->reason
)
7346 auto_diagnostic_group d
;
7347 complained
= permerror (loc
, "invalid user-defined conversion "
7348 "from %qH to %qI", TREE_TYPE (expr
),
7351 print_z_candidate (loc
, N_("candidate is:"), t
->cand
);
7352 expr
= convert_like (t
, expr
, fn
, argnum
,
7353 /*issue_conversion_warnings=*/false,
7354 /*c_cast_p=*/false, complain
);
7355 if (convs
->kind
== ck_ref_bind
)
7356 expr
= convert_to_reference (totype
, expr
, CONV_IMPLICIT
,
7357 LOOKUP_NORMAL
, NULL_TREE
,
7360 expr
= cp_convert (totype
, expr
, complain
);
7362 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7365 else if (t
->kind
== ck_user
|| !t
->bad_p
)
7367 expr
= convert_like (t
, expr
, fn
, argnum
,
7368 /*issue_conversion_warnings=*/false,
7369 /*c_cast_p=*/false, complain
);
7372 else if (t
->kind
== ck_ambig
)
7373 return convert_like (t
, expr
, fn
, argnum
,
7374 /*issue_conversion_warnings=*/false,
7375 /*c_cast_p=*/false, complain
);
7376 else if (t
->kind
== ck_identity
)
7381 range_label_for_type_mismatch
label (TREE_TYPE (expr
), totype
);
7382 gcc_rich_location
richloc (loc
, &label
);
7383 complained
= permerror (&richloc
,
7384 "invalid conversion from %qH to %qI",
7385 TREE_TYPE (expr
), totype
);
7388 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7390 return cp_convert (totype
, expr
, complain
);
7393 if (issue_conversion_warnings
&& (complain
& tf_warning
))
7394 conversion_null_warnings (totype
, expr
, fn
, argnum
);
7396 switch (convs
->kind
)
7400 struct z_candidate
*cand
= convs
->cand
;
7403 /* We chose the surrogate function from add_conv_candidate, now we
7404 actually need to build the conversion. */
7405 cand
= build_user_type_conversion_1 (totype
, expr
,
7406 LOOKUP_NO_CONVERSION
, complain
);
7408 tree convfn
= cand
->fn
;
7410 /* When converting from an init list we consider explicit
7411 constructors, but actually trying to call one is an error. */
7412 if (DECL_NONCONVERTING_P (convfn
) && DECL_CONSTRUCTOR_P (convfn
)
7413 && BRACE_ENCLOSED_INITIALIZER_P (expr
)
7414 /* Unless this is for direct-list-initialization. */
7415 && (!CONSTRUCTOR_IS_DIRECT_INIT (expr
) || convs
->need_temporary_p
)
7416 /* And in C++98 a default constructor can't be explicit. */
7417 && cxx_dialect
>= cxx11
)
7419 if (!(complain
& tf_error
))
7420 return error_mark_node
;
7421 location_t loc
= location_of (expr
);
7422 if (CONSTRUCTOR_NELTS (expr
) == 0
7423 && FUNCTION_FIRST_USER_PARMTYPE (convfn
) != void_list_node
)
7425 auto_diagnostic_group d
;
7426 if (pedwarn (loc
, 0, "converting to %qT from initializer list "
7427 "would use explicit constructor %qD",
7429 inform (loc
, "in C++11 and above a default constructor "
7433 error ("converting to %qT from initializer list would use "
7434 "explicit constructor %qD", totype
, convfn
);
7437 /* If we're initializing from {}, it's value-initialization. */
7438 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
7439 && CONSTRUCTOR_NELTS (expr
) == 0
7440 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype
)
7441 && !processing_template_decl
)
7443 bool direct
= CONSTRUCTOR_IS_DIRECT_INIT (expr
);
7444 if (abstract_virtuals_error_sfinae (NULL_TREE
, totype
, complain
))
7445 return error_mark_node
;
7446 expr
= build_value_init (totype
, complain
);
7447 expr
= get_target_expr_sfinae (expr
, complain
);
7448 if (expr
!= error_mark_node
)
7450 TARGET_EXPR_LIST_INIT_P (expr
) = true;
7451 TARGET_EXPR_DIRECT_INIT_P (expr
) = direct
;
7456 /* We don't know here whether EXPR is being used as an lvalue or
7457 rvalue, but we know it's read. */
7458 mark_exp_read (expr
);
7460 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
7462 expr
= build_over_call (cand
, LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
,
7465 /* If this is a constructor or a function returning an aggr type,
7466 we need to build up a TARGET_EXPR. */
7467 if (DECL_CONSTRUCTOR_P (convfn
))
7469 expr
= build_cplus_new (totype
, expr
, complain
);
7471 /* Remember that this was list-initialization. */
7472 if (convs
->check_narrowing
&& expr
!= error_mark_node
)
7473 TARGET_EXPR_LIST_INIT_P (expr
) = true;
7479 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
7481 int nelts
= CONSTRUCTOR_NELTS (expr
);
7483 expr
= build_value_init (totype
, complain
);
7484 else if (nelts
== 1)
7485 expr
= CONSTRUCTOR_ELT (expr
, 0)->value
;
7489 expr
= mark_use (expr
, /*rvalue_p=*/!convs
->rvaluedness_matches_p
,
7490 /*read_p=*/true, UNKNOWN_LOCATION
,
7491 /*reject_builtin=*/true);
7493 if (type_unknown_p (expr
))
7494 expr
= instantiate_type (totype
, expr
, complain
);
7495 if (expr
== null_node
7496 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype
))
7497 /* If __null has been converted to an integer type, we do not want to
7498 continue to warn about uses of EXPR as an integer, rather than as a
7500 expr
= build_int_cst (totype
, 0);
7503 /* We leave bad_p off ck_ambig because overload resolution considers
7504 it valid, it just fails when we try to perform it. So we need to
7505 check complain here, too. */
7506 if (complain
& tf_error
)
7508 /* Call build_user_type_conversion again for the error. */
7509 int flags
= (convs
->need_temporary_p
7510 ? LOOKUP_IMPLICIT
: LOOKUP_NORMAL
);
7511 build_user_type_conversion (totype
, convs
->u
.expr
, flags
, complain
);
7512 gcc_assert (seen_error ());
7513 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7515 return error_mark_node
;
7519 /* Conversion to std::initializer_list<T>. */
7520 tree elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype
), 0);
7521 unsigned len
= CONSTRUCTOR_NELTS (expr
);
7526 tree val
; unsigned ix
;
7528 tree new_ctor
= build_constructor (init_list_type_node
, NULL
);
7530 /* Convert all the elements. */
7531 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), ix
, val
)
7533 tree sub
= convert_like (convs
->u
.list
[ix
], val
, fn
,
7534 argnum
, false, false, complain
);
7535 if (sub
== error_mark_node
)
7537 if (!BRACE_ENCLOSED_INITIALIZER_P (val
)
7538 && !check_narrowing (TREE_TYPE (sub
), val
, complain
))
7539 return error_mark_node
;
7540 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor
),
7542 if (!TREE_CONSTANT (sub
))
7543 TREE_CONSTANT (new_ctor
) = false;
7545 /* Build up the array. */
7546 elttype
= cp_build_qualified_type
7547 (elttype
, cp_type_quals (elttype
) | TYPE_QUAL_CONST
);
7548 array
= build_array_of_n_type (elttype
, len
);
7549 array
= finish_compound_literal (array
, new_ctor
, complain
);
7550 /* Take the address explicitly rather than via decay_conversion
7551 to avoid the error about taking the address of a temporary. */
7552 array
= cp_build_addr_expr (array
, complain
);
7555 array
= nullptr_node
;
7557 array
= cp_convert (build_pointer_type (elttype
), array
, complain
);
7558 if (array
== error_mark_node
)
7559 return error_mark_node
;
7561 /* Build up the initializer_list object. Note: fail gracefully
7562 if the object cannot be completed because, for example, no
7563 definition is provided (c++/80956). */
7564 totype
= complete_type_or_maybe_complain (totype
, NULL_TREE
, complain
);
7566 return error_mark_node
;
7567 tree field
= next_initializable_field (TYPE_FIELDS (totype
));
7568 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
7569 CONSTRUCTOR_APPEND_ELT (vec
, field
, array
);
7570 field
= next_initializable_field (DECL_CHAIN (field
));
7571 CONSTRUCTOR_APPEND_ELT (vec
, field
, size_int (len
));
7572 tree new_ctor
= build_constructor (totype
, vec
);
7573 return get_target_expr_sfinae (new_ctor
, complain
);
7577 if (TREE_CODE (totype
) == COMPLEX_TYPE
)
7579 tree real
= CONSTRUCTOR_ELT (expr
, 0)->value
;
7580 tree imag
= CONSTRUCTOR_ELT (expr
, 1)->value
;
7581 real
= perform_implicit_conversion (TREE_TYPE (totype
),
7583 imag
= perform_implicit_conversion (TREE_TYPE (totype
),
7585 expr
= build2 (COMPLEX_EXPR
, totype
, real
, imag
);
7588 expr
= reshape_init (totype
, expr
, complain
);
7589 expr
= get_target_expr_sfinae (digest_init (totype
, expr
, complain
),
7591 if (expr
!= error_mark_node
)
7592 TARGET_EXPR_LIST_INIT_P (expr
) = true;
7599 expr
= convert_like (next_conversion (convs
), expr
, fn
, argnum
,
7600 convs
->kind
== ck_ref_bind
7601 ? issue_conversion_warnings
: false,
7602 c_cast_p
, complain
);
7603 if (expr
== error_mark_node
)
7604 return error_mark_node
;
7606 switch (convs
->kind
)
7609 expr
= decay_conversion (expr
, complain
);
7610 if (expr
== error_mark_node
)
7612 if (complain
& tf_error
)
7614 auto_diagnostic_group d
;
7615 maybe_print_user_conv_context (convs
);
7616 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7618 return error_mark_node
;
7621 if (! MAYBE_CLASS_TYPE_P (totype
))
7624 /* Don't introduce copies when passing arguments along to the inherited
7626 if (current_function_decl
7627 && flag_new_inheriting_ctors
7628 && DECL_INHERITED_CTOR (current_function_decl
))
7631 if (TREE_CODE (expr
) == TARGET_EXPR
7632 && TARGET_EXPR_LIST_INIT_P (expr
))
7633 /* Copy-list-initialization doesn't actually involve a copy. */
7638 if (convs
->kind
== ck_base
&& !convs
->need_temporary_p
)
7640 /* We are going to bind a reference directly to a base-class
7641 subobject of EXPR. */
7642 /* Build an expression for `*((base*) &expr)'. */
7643 expr
= convert_to_base (expr
, totype
,
7644 !c_cast_p
, /*nonnull=*/true, complain
);
7648 /* Copy-initialization where the cv-unqualified version of the source
7649 type is the same class as, or a derived class of, the class of the
7650 destination [is treated as direct-initialization]. [dcl.init] */
7651 flags
= LOOKUP_NORMAL
;
7652 /* This conversion is being done in the context of a user-defined
7653 conversion (i.e. the second step of copy-initialization), so
7654 don't allow any more. */
7655 if (convs
->user_conv_p
)
7656 flags
|= LOOKUP_NO_CONVERSION
;
7657 /* We might be performing a conversion of the argument
7658 to the user-defined conversion, i.e., not a conversion of the
7659 result of the user-defined conversion. In which case we skip
7660 explicit constructors. */
7661 if (convs
->copy_init_p
)
7662 flags
|= LOOKUP_ONLYCONVERTING
;
7663 if (convs
->rvaluedness_matches_p
)
7664 /* standard_conversion got LOOKUP_PREFER_RVALUE. */
7665 flags
|= LOOKUP_PREFER_RVALUE
;
7666 expr
= build_temp (expr
, totype
, flags
, &diag_kind
, complain
);
7667 if (diag_kind
&& complain
)
7669 auto_diagnostic_group d
;
7670 maybe_print_user_conv_context (convs
);
7671 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7674 return build_cplus_new (totype
, expr
, complain
);
7678 tree ref_type
= totype
;
7680 /* direct_reference_binding might have inserted a ck_qual under
7681 this ck_ref_bind for the benefit of conversion sequence ranking.
7682 Ignore the conversion; we'll create our own below. */
7683 if (next_conversion (convs
)->kind
== ck_qual
7684 && !convs
->need_temporary_p
)
7686 gcc_assert (same_type_p (TREE_TYPE (expr
),
7687 next_conversion (convs
)->type
));
7688 /* Strip the cast created by the ck_qual; cp_build_addr_expr
7689 below expects an lvalue. */
7693 if (convs
->bad_p
&& !next_conversion (convs
)->bad_p
)
7695 tree extype
= TREE_TYPE (expr
);
7696 auto_diagnostic_group d
;
7697 if (TYPE_REF_IS_RVALUE (ref_type
)
7699 error_at (loc
, "cannot bind rvalue reference of type %qH to "
7700 "lvalue of type %qI", totype
, extype
);
7701 else if (!TYPE_REF_IS_RVALUE (ref_type
) && !lvalue_p (expr
)
7702 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type
)))
7703 error_at (loc
, "cannot bind non-const lvalue reference of "
7704 "type %qH to an rvalue of type %qI", totype
, extype
);
7705 else if (!reference_compatible_p (TREE_TYPE (totype
), extype
))
7707 /* If we're converting from T[] to T[N], don't talk
7708 about discarding qualifiers. (Converting from T[N] to
7709 T[] is allowed by P0388R4.) */
7710 if (TREE_CODE (extype
) == ARRAY_TYPE
7711 && TYPE_DOMAIN (extype
) == NULL_TREE
7712 && TREE_CODE (TREE_TYPE (totype
)) == ARRAY_TYPE
7713 && TYPE_DOMAIN (TREE_TYPE (totype
)) != NULL_TREE
)
7714 error_at (loc
, "cannot bind reference of type %qH to %qI "
7715 "due to different array bounds", totype
, extype
);
7717 error_at (loc
, "binding reference of type %qH to %qI "
7718 "discards qualifiers", totype
, extype
);
7722 maybe_print_user_conv_context (convs
);
7723 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7725 return error_mark_node
;
7727 else if (complain
& tf_warning
)
7728 maybe_warn_array_conv (loc
, convs
, expr
);
7730 /* If necessary, create a temporary.
7732 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
7733 that need temporaries, even when their types are reference
7734 compatible with the type of reference being bound, so the
7735 upcoming call to cp_build_addr_expr doesn't fail. */
7736 if (convs
->need_temporary_p
7737 || TREE_CODE (expr
) == CONSTRUCTOR
7738 || TREE_CODE (expr
) == VA_ARG_EXPR
)
7740 /* Otherwise, a temporary of type "cv1 T1" is created and
7741 initialized from the initializer expression using the rules
7742 for a non-reference copy-initialization (8.5). */
7744 tree type
= TREE_TYPE (ref_type
);
7745 cp_lvalue_kind lvalue
= lvalue_kind (expr
);
7747 gcc_assert (similar_type_p (type
, next_conversion (convs
)->type
));
7748 if (!CP_TYPE_CONST_NON_VOLATILE_P (type
)
7749 && !TYPE_REF_IS_RVALUE (ref_type
))
7751 /* If the reference is volatile or non-const, we
7752 cannot create a temporary. */
7753 if (complain
& tf_error
)
7755 if (lvalue
& clk_bitfield
)
7756 error_at (loc
, "cannot bind bit-field %qE to %qT",
7758 else if (lvalue
& clk_packed
)
7759 error_at (loc
, "cannot bind packed field %qE to %qT",
7762 error_at (loc
, "cannot bind rvalue %qE to %qT",
7765 return error_mark_node
;
7767 /* If the source is a packed field, and we must use a copy
7768 constructor, then building the target expr will require
7769 binding the field to the reference parameter to the
7770 copy constructor, and we'll end up with an infinite
7771 loop. If we can use a bitwise copy, then we'll be
7773 if ((lvalue
& clk_packed
)
7774 && CLASS_TYPE_P (type
)
7775 && type_has_nontrivial_copy_init (type
))
7777 error_at (loc
, "cannot bind packed field %qE to %qT",
7779 return error_mark_node
;
7781 if (lvalue
& clk_bitfield
)
7783 expr
= convert_bitfield_to_declared_type (expr
);
7784 expr
= fold_convert (type
, expr
);
7787 /* Creating &TARGET_EXPR<> in a template would break when
7788 tsubsting the expression, so use an IMPLICIT_CONV_EXPR
7789 instead. This can happen even when there's no class
7790 involved, e.g., when converting an integer to a reference
7792 if (processing_template_decl
)
7793 return build1 (IMPLICIT_CONV_EXPR
, totype
, expr
);
7794 expr
= build_target_expr_with_type (expr
, type
, complain
);
7797 /* Take the address of the thing to which we will bind the
7799 expr
= cp_build_addr_expr (expr
, complain
);
7800 if (expr
== error_mark_node
)
7801 return error_mark_node
;
7803 /* Convert it to a pointer to the type referred to by the
7804 reference. This will adjust the pointer if a derived to
7805 base conversion is being performed. */
7806 expr
= cp_convert (build_pointer_type (TREE_TYPE (ref_type
)),
7808 /* Convert the pointer to the desired reference type. */
7809 return build_nop (ref_type
, expr
);
7813 return decay_conversion (expr
, complain
);
7816 /* ??? Should the address of a transaction-safe pointer point to the TM
7817 clone, and this conversion look up the primary function? */
7818 return build_nop (totype
, expr
);
7821 /* Warn about deprecated conversion if appropriate. */
7822 if (complain
& tf_warning
)
7824 string_conv_p (totype
, expr
, 1);
7825 maybe_warn_array_conv (loc
, convs
, expr
);
7831 expr
= convert_to_base (expr
, totype
, !c_cast_p
,
7832 /*nonnull=*/false, complain
);
7833 return build_nop (totype
, expr
);
7836 return convert_ptrmem (totype
, expr
, /*allow_inverse_p=*/false,
7837 c_cast_p
, complain
);
7843 if (convs
->check_narrowing
7844 && !check_narrowing (totype
, expr
, complain
,
7845 convs
->check_narrowing_const_only
))
7846 return error_mark_node
;
7848 warning_sentinel
w (warn_zero_as_null_pointer_constant
);
7849 if (issue_conversion_warnings
)
7850 expr
= cp_convert_and_check (totype
, expr
, complain
);
7852 expr
= cp_convert (totype
, expr
, complain
);
7857 /* Wrapper for convert_like_internal that handles creating
7858 IMPLICIT_CONV_EXPR. */
7861 convert_like (conversion
*convs
, tree expr
, tree fn
, int argnum
,
7862 bool issue_conversion_warnings
, bool c_cast_p
,
7863 tsubst_flags_t complain
)
7865 /* Creating &TARGET_EXPR<> in a template breaks when substituting,
7866 and creating a CALL_EXPR in a template breaks in finish_call_expr
7867 so use an IMPLICIT_CONV_EXPR for this conversion. We would have
7868 created such codes e.g. when calling a user-defined conversion
7870 tree conv_expr
= NULL_TREE
;
7871 if (processing_template_decl
7872 && convs
->kind
!= ck_identity
7873 && (CLASS_TYPE_P (convs
->type
) || CLASS_TYPE_P (TREE_TYPE (expr
))))
7875 conv_expr
= build1 (IMPLICIT_CONV_EXPR
, convs
->type
, expr
);
7876 if (convs
->kind
!= ck_ref_bind
)
7877 conv_expr
= convert_from_reference (conv_expr
);
7880 /* Do the normal processing to give the bad_p errors. But we still
7881 need to return the IMPLICIT_CONV_EXPR, unless we're returning
7884 expr
= convert_like_internal (convs
, expr
, fn
, argnum
,
7885 issue_conversion_warnings
, c_cast_p
, complain
);
7886 if (expr
== error_mark_node
)
7887 return error_mark_node
;
7888 return conv_expr
? conv_expr
: expr
;
7891 /* Convenience wrapper for convert_like. */
7894 convert_like (conversion
*convs
, tree expr
, tsubst_flags_t complain
)
7896 return convert_like (convs
, expr
, NULL_TREE
, 0,
7897 /*issue_conversion_warnings=*/true,
7898 /*c_cast_p=*/false, complain
);
7901 /* Convenience wrapper for convert_like. */
7904 convert_like_with_context (conversion
*convs
, tree expr
, tree fn
, int argnum
,
7905 tsubst_flags_t complain
)
7907 return convert_like (convs
, expr
, fn
, argnum
,
7908 /*issue_conversion_warnings=*/true,
7909 /*c_cast_p=*/false, complain
);
7912 /* ARG is being passed to a varargs function. Perform any conversions
7913 required. Return the converted value. */
7916 convert_arg_to_ellipsis (tree arg
, tsubst_flags_t complain
)
7918 tree arg_type
= TREE_TYPE (arg
);
7919 location_t loc
= cp_expr_loc_or_input_loc (arg
);
7923 If the argument has integral or enumeration type that is subject
7924 to the integral promotions (_conv.prom_), or a floating-point
7925 type that is subject to the floating-point promotion
7926 (_conv.fpprom_), the value of the argument is converted to the
7927 promoted type before the call. */
7928 if (TREE_CODE (arg_type
) == REAL_TYPE
7929 && (TYPE_PRECISION (arg_type
)
7930 < TYPE_PRECISION (double_type_node
))
7931 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type
)))
7933 if ((complain
& tf_warning
)
7934 && warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
7935 warning_at (loc
, OPT_Wdouble_promotion
,
7936 "implicit conversion from %qH to %qI when passing "
7937 "argument to function",
7938 arg_type
, double_type_node
);
7939 arg
= mark_rvalue_use (arg
);
7940 arg
= convert_to_real_nofold (double_type_node
, arg
);
7942 else if (NULLPTR_TYPE_P (arg_type
))
7944 arg
= mark_rvalue_use (arg
);
7945 if (TREE_SIDE_EFFECTS (arg
))
7946 arg
= cp_build_compound_expr (arg
, null_pointer_node
, complain
);
7948 arg
= null_pointer_node
;
7950 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type
))
7952 if (SCOPED_ENUM_P (arg_type
))
7954 tree prom
= cp_convert (ENUM_UNDERLYING_TYPE (arg_type
), arg
,
7956 prom
= cp_perform_integral_promotions (prom
, complain
);
7957 if (abi_version_crosses (6)
7958 && TYPE_MODE (TREE_TYPE (prom
)) != TYPE_MODE (arg_type
)
7959 && (complain
& tf_warning
))
7960 warning_at (loc
, OPT_Wabi
, "scoped enum %qT passed through %<...%>"
7961 " as %qT before %<-fabi-version=6%>, %qT after",
7963 TREE_TYPE (prom
), ENUM_UNDERLYING_TYPE (arg_type
));
7964 if (!abi_version_at_least (6))
7968 arg
= cp_perform_integral_promotions (arg
, complain
);
7973 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7974 standard conversions are performed. */
7975 arg
= decay_conversion (arg
, complain
);
7977 arg
= require_complete_type_sfinae (arg
, complain
);
7978 arg_type
= TREE_TYPE (arg
);
7980 if (arg
!= error_mark_node
7981 /* In a template (or ill-formed code), we can have an incomplete type
7982 even after require_complete_type_sfinae, in which case we don't know
7983 whether it has trivial copy or not. */
7984 && COMPLETE_TYPE_P (arg_type
)
7985 && !cp_unevaluated_operand
)
7987 /* [expr.call] 5.2.2/7:
7988 Passing a potentially-evaluated argument of class type (Clause 9)
7989 with a non-trivial copy constructor or a non-trivial destructor
7990 with no corresponding parameter is conditionally-supported, with
7991 implementation-defined semantics.
7993 We support it as pass-by-invisible-reference, just like a normal
7996 If the call appears in the context of a sizeof expression,
7997 it is not potentially-evaluated. */
7998 if (type_has_nontrivial_copy_init (arg_type
)
7999 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type
))
8001 arg
= force_rvalue (arg
, complain
);
8002 if (complain
& tf_warning
)
8003 warning (OPT_Wconditionally_supported
,
8004 "passing objects of non-trivially-copyable "
8005 "type %q#T through %<...%> is conditionally supported",
8007 return build1 (ADDR_EXPR
, build_reference_type (arg_type
), arg
);
8009 /* Build up a real lvalue-to-rvalue conversion in case the
8010 copy constructor is trivial but not callable. */
8011 else if (CLASS_TYPE_P (arg_type
))
8012 force_rvalue (arg
, complain
);
8019 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
8022 build_x_va_arg (location_t loc
, tree expr
, tree type
)
8024 if (processing_template_decl
)
8026 tree r
= build_min (VA_ARG_EXPR
, type
, expr
);
8027 SET_EXPR_LOCATION (r
, loc
);
8031 type
= complete_type_or_else (type
, NULL_TREE
);
8033 if (expr
== error_mark_node
|| !type
)
8034 return error_mark_node
;
8036 expr
= mark_lvalue_use (expr
);
8038 if (TYPE_REF_P (type
))
8040 error ("cannot receive reference type %qT through %<...%>", type
);
8041 return error_mark_node
;
8044 if (type_has_nontrivial_copy_init (type
)
8045 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
8047 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
8048 it as pass by invisible reference. */
8049 warning_at (loc
, OPT_Wconditionally_supported
,
8050 "receiving objects of non-trivially-copyable type %q#T "
8051 "through %<...%> is conditionally-supported", type
);
8053 tree ref
= cp_build_reference_type (type
, false);
8054 expr
= build_va_arg (loc
, expr
, ref
);
8055 return convert_from_reference (expr
);
8058 tree ret
= build_va_arg (loc
, expr
, type
);
8059 if (CLASS_TYPE_P (type
))
8060 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
8061 know how to handle it. */
8062 ret
= get_target_expr (ret
);
8066 /* TYPE has been given to va_arg. Apply the default conversions which
8067 would have happened when passed via ellipsis. Return the promoted
8068 type, or the passed type if there is no change. */
8071 cxx_type_promotes_to (tree type
)
8075 /* Perform the array-to-pointer and function-to-pointer
8077 type
= type_decays_to (type
);
8079 promote
= type_promotes_to (type
);
8080 if (same_type_p (type
, promote
))
8086 /* ARG is a default argument expression being passed to a parameter of
8087 the indicated TYPE, which is a parameter to FN. PARMNUM is the
8088 zero-based argument number. Do any required conversions. Return
8089 the converted value. */
8091 static GTY(()) vec
<tree
, va_gc
> *default_arg_context
;
8093 push_defarg_context (tree fn
)
8094 { vec_safe_push (default_arg_context
, fn
); }
8097 pop_defarg_context (void)
8098 { default_arg_context
->pop (); }
8101 convert_default_arg (tree type
, tree arg
, tree fn
, int parmnum
,
8102 tsubst_flags_t complain
)
8107 /* See through clones. */
8108 fn
= DECL_ORIGIN (fn
);
8109 /* And inheriting ctors. */
8110 if (flag_new_inheriting_ctors
)
8111 fn
= strip_inheriting_ctors (fn
);
8113 /* Detect recursion. */
8114 FOR_EACH_VEC_SAFE_ELT (default_arg_context
, i
, t
)
8117 if (complain
& tf_error
)
8118 error ("recursive evaluation of default argument for %q#D", fn
);
8119 return error_mark_node
;
8122 /* If the ARG is an unparsed default argument expression, the
8123 conversion cannot be performed. */
8124 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
8126 if (complain
& tf_error
)
8127 error ("call to %qD uses the default argument for parameter %P, which "
8128 "is not yet defined", fn
, parmnum
);
8129 return error_mark_node
;
8132 push_defarg_context (fn
);
8134 if (fn
&& DECL_TEMPLATE_INFO (fn
))
8135 arg
= tsubst_default_argument (fn
, parmnum
, type
, arg
, complain
);
8141 The names in the expression are bound, and the semantic
8142 constraints are checked, at the point where the default
8143 expressions appears.
8145 we must not perform access checks here. */
8146 push_deferring_access_checks (dk_no_check
);
8147 /* We must make a copy of ARG, in case subsequent processing
8148 alters any part of it. */
8149 arg
= break_out_target_exprs (arg
, /*clear location*/true);
8151 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_IMPLICIT
,
8152 ICR_DEFAULT_ARGUMENT
, fn
, parmnum
,
8154 arg
= convert_for_arg_passing (type
, arg
, complain
);
8155 pop_deferring_access_checks();
8157 pop_defarg_context ();
8162 /* Returns the type which will really be used for passing an argument of
8166 type_passed_as (tree type
)
8168 /* Pass classes with copy ctors by invisible reference. */
8169 if (TREE_ADDRESSABLE (type
))
8171 type
= build_reference_type (type
);
8172 /* There are no other pointers to this temporary. */
8173 type
= cp_build_qualified_type (type
, TYPE_QUAL_RESTRICT
);
8175 else if (targetm
.calls
.promote_prototypes (NULL_TREE
)
8176 && INTEGRAL_TYPE_P (type
)
8177 && COMPLETE_TYPE_P (type
)
8178 && tree_int_cst_lt (TYPE_SIZE (type
), TYPE_SIZE (integer_type_node
)))
8179 type
= integer_type_node
;
8184 /* Actually perform the appropriate conversion. */
8187 convert_for_arg_passing (tree type
, tree val
, tsubst_flags_t complain
)
8191 /* If VAL is a bitfield, then -- since it has already been converted
8192 to TYPE -- it cannot have a precision greater than TYPE.
8194 If it has a smaller precision, we must widen it here. For
8195 example, passing "int f:3;" to a function expecting an "int" will
8196 not result in any conversion before this point.
8198 If the precision is the same we must not risk widening. For
8199 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
8200 often have type "int", even though the C++ type for the field is
8201 "long long". If the value is being passed to a function
8202 expecting an "int", then no conversions will be required. But,
8203 if we call convert_bitfield_to_declared_type, the bitfield will
8204 be converted to "long long". */
8205 bitfield_type
= is_bitfield_expr_with_lowered_type (val
);
8207 && TYPE_PRECISION (TREE_TYPE (val
)) < TYPE_PRECISION (type
))
8208 val
= convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type
), val
);
8210 if (val
== error_mark_node
)
8212 /* Pass classes with copy ctors by invisible reference. */
8213 else if (TREE_ADDRESSABLE (type
))
8214 val
= build1 (ADDR_EXPR
, build_reference_type (type
), val
);
8215 else if (targetm
.calls
.promote_prototypes (NULL_TREE
)
8216 && INTEGRAL_TYPE_P (type
)
8217 && COMPLETE_TYPE_P (type
)
8218 && tree_int_cst_lt (TYPE_SIZE (type
), TYPE_SIZE (integer_type_node
)))
8219 val
= cp_perform_integral_promotions (val
, complain
);
8220 if (complain
& tf_warning
)
8222 if (warn_suggest_attribute_format
)
8224 tree rhstype
= TREE_TYPE (val
);
8225 const enum tree_code coder
= TREE_CODE (rhstype
);
8226 const enum tree_code codel
= TREE_CODE (type
);
8227 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
8229 && check_missing_format_attribute (type
, rhstype
))
8230 warning (OPT_Wsuggest_attribute_format
,
8231 "argument of function call might be a candidate "
8232 "for a format attribute");
8234 maybe_warn_parm_abi (type
, cp_expr_loc_or_input_loc (val
));
8237 if (complain
& tf_warning
)
8238 warn_for_address_or_pointer_of_packed_member (type
, val
);
8243 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
8244 which just decay_conversion or no conversions at all should be done.
8245 This is true for some builtins which don't act like normal functions.
8246 Return 2 if no conversions at all should be done, 1 if just
8247 decay_conversion. Return 3 for special treatment of the 3rd argument
8248 for __builtin_*_overflow_p. */
8251 magic_varargs_p (tree fn
)
8253 if (DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
8254 switch (DECL_FUNCTION_CODE (fn
))
8256 case BUILT_IN_CLASSIFY_TYPE
:
8257 case BUILT_IN_CONSTANT_P
:
8258 case BUILT_IN_NEXT_ARG
:
8259 case BUILT_IN_VA_START
:
8262 case BUILT_IN_ADD_OVERFLOW_P
:
8263 case BUILT_IN_SUB_OVERFLOW_P
:
8264 case BUILT_IN_MUL_OVERFLOW_P
:
8268 return lookup_attribute ("type generic",
8269 TYPE_ATTRIBUTES (TREE_TYPE (fn
))) != 0;
8275 /* Returns the decl of the dispatcher function if FN is a function version. */
8278 get_function_version_dispatcher (tree fn
)
8280 tree dispatcher_decl
= NULL
;
8282 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
8283 && DECL_FUNCTION_VERSIONED (fn
));
8285 gcc_assert (targetm
.get_function_versions_dispatcher
);
8286 dispatcher_decl
= targetm
.get_function_versions_dispatcher (fn
);
8288 if (dispatcher_decl
== NULL
)
8290 error_at (input_location
, "use of multiversioned function "
8291 "without a default");
8295 retrofit_lang_decl (dispatcher_decl
);
8296 gcc_assert (dispatcher_decl
!= NULL
);
8297 return dispatcher_decl
;
8300 /* fn is a function version dispatcher that is marked used. Mark all the
8301 semantically identical function versions it will dispatch as used. */
8304 mark_versions_used (tree fn
)
8306 struct cgraph_node
*node
;
8307 struct cgraph_function_version_info
*node_v
;
8308 struct cgraph_function_version_info
*it_v
;
8310 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8312 node
= cgraph_node::get (fn
);
8316 gcc_assert (node
->dispatcher_function
);
8318 node_v
= node
->function_version ();
8322 /* All semantically identical versions are chained. Traverse and mark each
8323 one of them as used. */
8324 it_v
= node_v
->next
;
8325 while (it_v
!= NULL
)
8327 mark_used (it_v
->this_node
->decl
);
8332 /* Build a call to "the copy constructor" for the type of A, even if it
8333 wouldn't be selected by normal overload resolution. Used for
8337 call_copy_ctor (tree a
, tsubst_flags_t complain
)
8339 tree ctype
= TYPE_MAIN_VARIANT (TREE_TYPE (a
));
8340 tree binfo
= TYPE_BINFO (ctype
);
8341 tree copy
= get_copy_ctor (ctype
, complain
);
8342 copy
= build_baselink (binfo
, binfo
, copy
, NULL_TREE
);
8343 tree ob
= build_dummy_object (ctype
);
8344 releasing_vec
args (make_tree_vector_single (a
));
8345 tree r
= build_new_method_call (ob
, copy
, &args
, NULL_TREE
,
8346 LOOKUP_NORMAL
, NULL
, complain
);
8350 /* Return true iff T refers to a base field. */
8353 is_base_field_ref (tree t
)
8356 if (TREE_CODE (t
) == ADDR_EXPR
)
8357 t
= TREE_OPERAND (t
, 0);
8358 if (TREE_CODE (t
) == COMPONENT_REF
)
8359 t
= TREE_OPERAND (t
, 1);
8360 if (TREE_CODE (t
) == FIELD_DECL
)
8361 return DECL_FIELD_IS_BASE (t
);
8365 /* We can't elide a copy from a function returning by value to a base
8366 subobject, as the callee might clobber tail padding. Return true iff this
8367 could be that case. */
8370 unsafe_copy_elision_p (tree target
, tree exp
)
8372 /* Copy elision only happens with a TARGET_EXPR. */
8373 if (TREE_CODE (exp
) != TARGET_EXPR
)
8375 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
8376 /* It's safe to elide the copy for a class with no tail padding. */
8377 if (tree_int_cst_equal (TYPE_SIZE (type
), CLASSTYPE_SIZE (type
)))
8379 /* It's safe to elide the copy if we aren't initializing a base object. */
8380 if (!is_base_field_ref (target
))
8382 tree init
= TARGET_EXPR_INITIAL (exp
);
8383 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
8384 while (TREE_CODE (init
) == COMPOUND_EXPR
)
8385 init
= TREE_OPERAND (init
, 1);
8386 if (TREE_CODE (init
) == COND_EXPR
)
8388 /* We'll end up copying from each of the arms of the COND_EXPR directly
8389 into the target, so look at them. */
8390 if (tree op
= TREE_OPERAND (init
, 1))
8391 if (unsafe_copy_elision_p (target
, op
))
8393 return unsafe_copy_elision_p (target
, TREE_OPERAND (init
, 2));
8395 return (TREE_CODE (init
) == AGGR_INIT_EXPR
8396 && !AGGR_INIT_VIA_CTOR_P (init
));
8399 /* True iff C is a conversion that binds a reference to a prvalue. */
8402 conv_binds_ref_to_prvalue (conversion
*c
)
8404 if (c
->kind
!= ck_ref_bind
)
8406 if (c
->need_temporary_p
)
8409 c
= next_conversion (c
);
8411 if (c
->kind
== ck_rvalue
)
8413 if (c
->kind
== ck_user
&& !TYPE_REF_P (c
->type
))
8415 if (c
->kind
== ck_identity
&& c
->u
.expr
8416 && TREE_CODE (c
->u
.expr
) == TARGET_EXPR
)
8422 /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
8423 class type or a pointer to class type. */
8426 build_trivial_dtor_call (tree instance
)
8428 gcc_assert (!is_dummy_object (instance
));
8430 if (!flag_lifetime_dse
)
8433 return fold_convert (void_type_node
, instance
);
8436 if (INDIRECT_TYPE_P (TREE_TYPE (instance
)))
8438 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
8440 instance
= cp_build_fold_indirect_ref (instance
);
8443 /* A trivial destructor should still clobber the object. */
8444 tree clobber
= build_clobber (TREE_TYPE (instance
));
8445 return build2 (MODIFY_EXPR
, void_type_node
,
8449 /* Subroutine of the various build_*_call functions. Overload resolution
8450 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
8451 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
8452 bitmask of various LOOKUP_* flags which apply to the call itself. */
8455 build_over_call (struct z_candidate
*cand
, int flags
, tsubst_flags_t complain
)
8458 const vec
<tree
, va_gc
> *args
= cand
->args
;
8459 tree first_arg
= cand
->first_arg
;
8460 conversion
**convs
= cand
->convs
;
8462 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
8467 unsigned int arg_index
= 0;
8471 bool already_used
= false;
8473 /* In a template, there is no need to perform all of the work that
8474 is normally done. We are only interested in the type of the call
8475 expression, i.e., the return type of the function. Any semantic
8476 errors will be deferred until the template is instantiated. */
8477 if (processing_template_decl
)
8481 const tree
*argarray
;
8484 if (undeduced_auto_decl (fn
))
8485 mark_used (fn
, complain
);
8487 /* Otherwise set TREE_USED for the benefit of -Wunused-function.
8491 return_type
= TREE_TYPE (TREE_TYPE (fn
));
8492 nargs
= vec_safe_length (args
);
8493 if (first_arg
== NULL_TREE
)
8494 argarray
= args
->address ();
8502 alcarray
= XALLOCAVEC (tree
, nargs
);
8503 alcarray
[0] = build_this (first_arg
);
8504 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
8505 alcarray
[ix
+ 1] = arg
;
8506 argarray
= alcarray
;
8509 addr
= build_addr_func (fn
, complain
);
8510 if (addr
== error_mark_node
)
8511 return error_mark_node
;
8512 expr
= build_call_array_loc (input_location
, return_type
,
8513 addr
, nargs
, argarray
);
8514 if (TREE_THIS_VOLATILE (fn
) && cfun
)
8515 current_function_returns_abnormally
= 1;
8516 if (TREE_CODE (fn
) == FUNCTION_DECL
8517 && DECL_IMMEDIATE_FUNCTION_P (fn
)
8518 && cp_unevaluated_operand
== 0
8519 && (current_function_decl
== NULL_TREE
8520 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
))
8521 && (current_binding_level
->kind
!= sk_function_parms
8522 || !current_binding_level
->immediate_fn_ctx_p
))
8524 tree obj_arg
= NULL_TREE
, exprimm
= expr
;
8525 if (DECL_CONSTRUCTOR_P (fn
))
8526 obj_arg
= first_arg
;
8528 && is_dummy_object (obj_arg
)
8529 && !type_dependent_expression_p (obj_arg
))
8531 exprimm
= build_cplus_new (DECL_CONTEXT (fn
), expr
, complain
);
8532 obj_arg
= NULL_TREE
;
8534 /* Look through *(const T *)&obj. */
8535 else if (obj_arg
&& TREE_CODE (obj_arg
) == INDIRECT_REF
)
8537 tree addr
= TREE_OPERAND (obj_arg
, 0);
8539 if (TREE_CODE (addr
) == ADDR_EXPR
)
8541 tree typeo
= TREE_TYPE (obj_arg
);
8542 tree typei
= TREE_TYPE (TREE_OPERAND (addr
, 0));
8543 if (same_type_ignoring_top_level_qualifiers_p (typeo
, typei
))
8544 obj_arg
= TREE_OPERAND (addr
, 0);
8547 fold_non_dependent_expr (exprimm
, complain
,
8548 /*manifestly_const_eval=*/true,
8551 return convert_from_reference (expr
);
8554 /* Give any warnings we noticed during overload resolution. */
8555 if (cand
->warnings
&& (complain
& tf_warning
))
8557 struct candidate_warning
*w
;
8558 for (w
= cand
->warnings
; w
; w
= w
->next
)
8559 joust (cand
, w
->loser
, 1, complain
);
8562 /* Core issue 2327: P0135 doesn't say how to handle the case where the
8563 argument to the copy constructor ends up being a prvalue after
8564 conversion. Let's do the normal processing, but pretend we aren't
8565 actually using the copy constructor. */
8566 bool force_elide
= false;
8567 if (cxx_dialect
>= cxx17
8568 && cand
->num_convs
== 1
8569 && DECL_COMPLETE_CONSTRUCTOR_P (fn
)
8570 && (DECL_COPY_CONSTRUCTOR_P (fn
)
8571 || DECL_MOVE_CONSTRUCTOR_P (fn
))
8572 && conv_binds_ref_to_prvalue (convs
[0]))
8575 goto not_really_used
;
8578 /* OK, we're actually calling this inherited constructor; set its deletedness
8579 appropriately. We can get away with doing this here because calling is
8580 the only way to refer to a constructor. */
8581 if (DECL_INHERITED_CTOR (fn
))
8582 deduce_inheriting_ctor (fn
);
8584 /* Make =delete work with SFINAE. */
8585 if (DECL_DELETED_FN (fn
))
8587 if (complain
& tf_error
)
8589 return error_mark_node
;
8592 if (DECL_FUNCTION_MEMBER_P (fn
))
8595 /* If FN is a template function, two cases must be considered.
8600 template <class T> void f();
8602 template <class T> struct B {
8606 struct C : A, B<int> {
8608 using B<int>::g; // #2
8611 In case #1 where `A::f' is a member template, DECL_ACCESS is
8612 recorded in the primary template but not in its specialization.
8613 We check access of FN using its primary template.
8615 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
8616 because it is a member of class template B, DECL_ACCESS is
8617 recorded in the specialization `B<int>::g'. We cannot use its
8618 primary template because `B<T>::g' and `B<int>::g' may have
8619 different access. */
8620 if (DECL_TEMPLATE_INFO (fn
)
8621 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
8622 access_fn
= DECL_TI_TEMPLATE (fn
);
8625 if (!perform_or_defer_access_check (cand
->access_path
, access_fn
,
8627 return error_mark_node
;
8630 /* If we're checking for implicit delete, don't bother with argument
8632 if (flags
& LOOKUP_SPECULATIVE
)
8634 if (cand
->viable
== 1)
8636 else if (!(complain
& tf_error
))
8637 /* Reject bad conversions now. */
8638 return error_mark_node
;
8639 /* else continue to get conversion error. */
8644 /* N3276 magic doesn't apply to nested calls. */
8645 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
8646 complain
&= ~tf_decltype
;
8647 /* No-Cleanup doesn't apply to nested calls either. */
8648 tsubst_flags_t no_cleanup_complain
= complain
;
8649 complain
&= ~tf_no_cleanup
;
8651 /* Find maximum size of vector to hold converted arguments. */
8652 parmlen
= list_length (parm
);
8653 nargs
= vec_safe_length (args
) + (first_arg
!= NULL_TREE
? 1 : 0);
8654 if (parmlen
> nargs
)
8656 argarray
= XALLOCAVEC (tree
, nargs
);
8658 /* The implicit parameters to a constructor are not considered by overload
8659 resolution, and must be of the proper type. */
8660 if (DECL_CONSTRUCTOR_P (fn
))
8663 if (first_arg
!= NULL_TREE
)
8665 object_arg
= first_arg
;
8666 first_arg
= NULL_TREE
;
8670 object_arg
= (*args
)[arg_index
];
8673 argarray
[j
++] = build_this (object_arg
);
8674 parm
= TREE_CHAIN (parm
);
8675 /* We should never try to call the abstract constructor. */
8676 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn
));
8678 if (DECL_HAS_VTT_PARM_P (fn
))
8680 argarray
[j
++] = (*args
)[arg_index
];
8682 parm
= TREE_CHAIN (parm
);
8685 if (cxx_dialect
< cxx20
8686 && (cand
->flags
& LOOKUP_PREFER_RVALUE
))
8688 /* The implicit move specified in 15.8.3/3 fails "...if the type of
8689 the first parameter of the selected constructor is not an rvalue
8690 reference to the object's type (possibly cv-qualified)...." */
8691 gcc_assert (!(complain
& tf_error
));
8692 tree ptype
= convs
[0]->type
;
8693 /* Allow calling a by-value converting constructor even though it
8694 isn't permitted by the above, because we've allowed it since GCC 5
8695 (PR58051) and it's allowed in C++20. But don't call a copy
8697 if ((TYPE_REF_P (ptype
) && !TYPE_REF_IS_RVALUE (ptype
))
8698 || CONVERSION_RANK (convs
[0]) > cr_exact
)
8699 return error_mark_node
;
8702 /* Bypass access control for 'this' parameter. */
8703 else if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
8705 tree arg
= build_this (first_arg
!= NULL_TREE
8707 : (*args
)[arg_index
]);
8708 tree argtype
= TREE_TYPE (arg
);
8710 if (arg
== error_mark_node
)
8711 return error_mark_node
;
8713 if (convs
[i
]->bad_p
)
8715 if (complain
& tf_error
)
8717 auto_diagnostic_group d
;
8718 if (permerror (input_location
, "passing %qT as %<this%> "
8719 "argument discards qualifiers",
8720 TREE_TYPE (argtype
)))
8721 inform (DECL_SOURCE_LOCATION (fn
), " in call to %qD", fn
);
8724 return error_mark_node
;
8727 /* The class where FN is defined. */
8728 tree ctx
= DECL_CONTEXT (fn
);
8730 /* See if the function member or the whole class type is declared
8731 final and the call can be devirtualized. */
8732 if (DECL_FINAL_P (fn
) || CLASSTYPE_FINAL (ctx
))
8733 flags
|= LOOKUP_NONVIRTUAL
;
8735 /* [class.mfct.non-static]: If a non-static member function of a class
8736 X is called for an object that is not of type X, or of a type
8737 derived from X, the behavior is undefined.
8739 So we can assume that anything passed as 'this' is non-null, and
8740 optimize accordingly. */
8741 /* Check that the base class is accessible. */
8742 if (!accessible_base_p (TREE_TYPE (argtype
),
8743 BINFO_TYPE (cand
->conversion_path
), true))
8745 if (complain
& tf_error
)
8746 error ("%qT is not an accessible base of %qT",
8747 BINFO_TYPE (cand
->conversion_path
),
8748 TREE_TYPE (argtype
));
8750 return error_mark_node
;
8752 /* If fn was found by a using declaration, the conversion path
8753 will be to the derived class, not the base declaring fn. We
8754 must convert to the base. */
8755 tree base_binfo
= cand
->conversion_path
;
8756 if (BINFO_TYPE (base_binfo
) != ctx
)
8758 base_binfo
= lookup_base (base_binfo
, ctx
, ba_unique
, NULL
, complain
);
8759 if (base_binfo
== error_mark_node
)
8760 return error_mark_node
;
8762 tree converted_arg
= build_base_path (PLUS_EXPR
, arg
,
8763 base_binfo
, 1, complain
);
8765 /* If we know the dynamic type of the object, look up the final overrider
8767 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0
8768 && resolves_to_fixed_type_p (arg
))
8770 fn
= lookup_vfn_in_binfo (DECL_VINDEX (fn
), base_binfo
);
8771 flags
|= LOOKUP_NONVIRTUAL
;
8774 argarray
[j
++] = converted_arg
;
8775 parm
= TREE_CHAIN (parm
);
8776 if (first_arg
!= NULL_TREE
)
8777 first_arg
= NULL_TREE
;
8784 gcc_assert (first_arg
== NULL_TREE
);
8785 for (; arg_index
< vec_safe_length (args
) && parm
;
8786 parm
= TREE_CHAIN (parm
), ++arg_index
, ++i
)
8788 tree type
= TREE_VALUE (parm
);
8789 tree arg
= (*args
)[arg_index
];
8790 bool conversion_warning
= true;
8794 /* If the argument is NULL and used to (implicitly) instantiate a
8795 template function (and bind one of the template arguments to
8796 the type of 'long int'), we don't want to warn about passing NULL
8797 to non-pointer argument.
8798 For example, if we have this template function:
8800 template<typename T> void func(T x) {}
8802 we want to warn (when -Wconversion is enabled) in this case:
8808 but not in this case:
8814 if (null_node_p (arg
)
8815 && DECL_TEMPLATE_INFO (fn
)
8816 && cand
->template_decl
8817 && !cand
->explicit_targs
)
8818 conversion_warning
= false;
8820 /* Set user_conv_p on the argument conversions, so rvalue/base handling
8821 knows not to allow any more UDCs. This needs to happen after we
8822 process cand->warnings. */
8823 if (flags
& LOOKUP_NO_CONVERSION
)
8824 conv
->user_conv_p
= true;
8826 tsubst_flags_t arg_complain
= complain
;
8827 if (!conversion_warning
)
8828 arg_complain
&= ~tf_warning
;
8830 val
= convert_like_with_context (conv
, arg
, fn
, i
- is_method
,
8832 val
= convert_for_arg_passing (type
, val
, arg_complain
);
8834 if (val
== error_mark_node
)
8835 return error_mark_node
;
8837 argarray
[j
++] = val
;
8840 /* Default arguments */
8841 for (; parm
&& parm
!= void_list_node
; parm
= TREE_CHAIN (parm
), i
++)
8843 if (TREE_VALUE (parm
) == error_mark_node
)
8844 return error_mark_node
;
8845 val
= convert_default_arg (TREE_VALUE (parm
),
8846 TREE_PURPOSE (parm
),
8849 if (val
== error_mark_node
)
8850 return error_mark_node
;
8851 argarray
[j
++] = val
;
8855 int magic
= magic_varargs_p (fn
);
8856 for (; arg_index
< vec_safe_length (args
); ++arg_index
)
8858 tree a
= (*args
)[arg_index
];
8859 if ((magic
== 3 && arg_index
== 2) || magic
== 2)
8861 /* Do no conversions for certain magic varargs. */
8862 a
= mark_type_use (a
);
8863 if (TREE_CODE (a
) == FUNCTION_DECL
&& reject_gcc_builtin (a
))
8864 return error_mark_node
;
8866 else if (magic
!= 0)
8867 /* For other magic varargs only do decay_conversion. */
8868 a
= decay_conversion (a
, complain
);
8869 else if (DECL_CONSTRUCTOR_P (fn
)
8870 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn
),
8873 /* Avoid infinite recursion trying to call A(...). */
8874 if (complain
& tf_error
)
8875 /* Try to call the actual copy constructor for a good error. */
8876 call_copy_ctor (a
, complain
);
8877 return error_mark_node
;
8880 a
= convert_arg_to_ellipsis (a
, complain
);
8881 if (a
== error_mark_node
)
8882 return error_mark_node
;
8886 gcc_assert (j
<= nargs
);
8889 /* Avoid performing argument transformation if warnings are disabled.
8890 When tf_warning is set and at least one of the warnings is active
8891 the check_function_arguments function might warn about something. */
8893 bool warned_p
= false;
8894 if ((complain
& tf_warning
)
8897 || warn_suggest_attribute_format
8900 tree
*fargs
= (!nargs
? argarray
8901 : (tree
*) alloca (nargs
* sizeof (tree
)));
8902 for (j
= 0; j
< nargs
; j
++)
8904 /* For -Wformat undo the implicit passing by hidden reference
8905 done by convert_arg_to_ellipsis. */
8906 if (TREE_CODE (argarray
[j
]) == ADDR_EXPR
8907 && TYPE_REF_P (TREE_TYPE (argarray
[j
])))
8908 fargs
[j
] = TREE_OPERAND (argarray
[j
], 0);
8910 fargs
[j
] = argarray
[j
];
8913 warned_p
= check_function_arguments (input_location
, fn
, TREE_TYPE (fn
),
8914 nargs
, fargs
, NULL
);
8917 if (DECL_INHERITED_CTOR (fn
))
8919 /* Check for passing ellipsis arguments to an inherited constructor. We
8920 could handle this by open-coding the inherited constructor rather than
8921 defining it, but let's not bother now. */
8922 if (!cp_unevaluated_operand
8924 && cand
->convs
[cand
->num_convs
-1]->ellipsis_p
)
8926 if (complain
& tf_error
)
8928 sorry ("passing arguments to ellipsis of inherited constructor "
8930 inform (DECL_SOURCE_LOCATION (cand
->fn
), "declared here");
8932 return error_mark_node
;
8935 /* A base constructor inheriting from a virtual base doesn't get the
8936 inherited arguments, just this and __vtt. */
8937 if (ctor_omit_inherited_parms (fn
))
8941 /* Avoid actually calling copy constructors and copy assignment operators,
8944 if (! flag_elide_constructors
&& !force_elide
)
8945 /* Do things the hard way. */;
8946 else if (cand
->num_convs
== 1
8947 && (DECL_COPY_CONSTRUCTOR_P (fn
)
8948 || DECL_MOVE_CONSTRUCTOR_P (fn
))
8949 /* It's unsafe to elide the constructor when handling
8950 a noexcept-expression, it may evaluate to the wrong
8951 value (c++/53025). */
8952 && (force_elide
|| cp_noexcept_operand
== 0))
8955 tree arg
= argarray
[num_artificial_parms_for (fn
)];
8957 bool trivial
= trivial_fn_p (fn
);
8959 /* Pull out the real argument, disregarding const-correctness. */
8961 /* Strip the reference binding for the constructor parameter. */
8962 if (CONVERT_EXPR_P (targ
)
8963 && TYPE_REF_P (TREE_TYPE (targ
)))
8964 targ
= TREE_OPERAND (targ
, 0);
8965 /* But don't strip any other reference bindings; binding a temporary to a
8966 reference prevents copy elision. */
8967 while ((CONVERT_EXPR_P (targ
)
8968 && !TYPE_REF_P (TREE_TYPE (targ
)))
8969 || TREE_CODE (targ
) == NON_LVALUE_EXPR
)
8970 targ
= TREE_OPERAND (targ
, 0);
8971 if (TREE_CODE (targ
) == ADDR_EXPR
)
8973 targ
= TREE_OPERAND (targ
, 0);
8974 if (!same_type_ignoring_top_level_qualifiers_p
8975 (TREE_TYPE (TREE_TYPE (arg
)), TREE_TYPE (targ
)))
8984 arg
= cp_build_fold_indirect_ref (arg
);
8986 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a base
8988 if (CHECKING_P
&& cxx_dialect
>= cxx17
)
8989 gcc_assert (TREE_CODE (arg
) != TARGET_EXPR
8991 /* It's from binding the ref parm to a packed field. */
8992 || convs
[0]->need_temporary_p
8994 /* See unsafe_copy_elision_p. */
8995 || DECL_BASE_CONSTRUCTOR_P (fn
));
8998 bool unsafe
= unsafe_copy_elision_p (fa
, arg
);
8999 bool eliding_temp
= (TREE_CODE (arg
) == TARGET_EXPR
&& !unsafe
);
9001 /* [class.copy]: the copy constructor is implicitly defined even if the
9002 implementation elided its use. But don't warn about deprecation when
9003 eliding a temporary, as then no copy is actually performed. */
9004 warning_sentinel
s (warn_deprecated_copy
, eliding_temp
);
9006 /* The language says this isn't called. */;
9009 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
9010 return error_mark_node
;
9011 already_used
= true;
9014 cp_warn_deprecated_use (fn
, complain
);
9016 /* If we're creating a temp and we already have one, don't create a
9017 new one. If we're not creating a temp but we get one, use
9018 INIT_EXPR to collapse the temp into our target. Otherwise, if the
9019 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
9020 temp or an INIT_EXPR otherwise. */
9021 if (is_dummy_object (fa
))
9023 if (TREE_CODE (arg
) == TARGET_EXPR
)
9026 return force_target_expr (DECL_CONTEXT (fn
), arg
, complain
);
9028 else if ((trivial
|| TREE_CODE (arg
) == TARGET_EXPR
)
9031 tree to
= cp_build_fold_indirect_ref (fa
);
9032 val
= build2 (INIT_EXPR
, DECL_CONTEXT (fn
), to
, arg
);
9036 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
9037 && DECL_OVERLOADED_OPERATOR_IS (fn
, NOP_EXPR
)
9038 && trivial_fn_p (fn
))
9040 tree to
= cp_build_fold_indirect_ref (argarray
[0]);
9041 tree type
= TREE_TYPE (to
);
9042 tree as_base
= CLASSTYPE_AS_BASE (type
);
9043 tree arg
= argarray
[1];
9044 location_t loc
= cp_expr_loc_or_input_loc (arg
);
9046 if (is_really_empty_class (type
, /*ignore_vptr*/true))
9048 /* Avoid copying empty classes. */
9049 val
= build2 (COMPOUND_EXPR
, type
, arg
, to
);
9050 TREE_NO_WARNING (val
) = 1;
9052 else if (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (as_base
)))
9054 if (is_std_init_list (type
)
9055 && conv_binds_ref_to_prvalue (convs
[1]))
9056 warning_at (loc
, OPT_Winit_list_lifetime
,
9057 "assignment from temporary %<initializer_list%> does "
9058 "not extend the lifetime of the underlying array");
9059 arg
= cp_build_fold_indirect_ref (arg
);
9060 val
= build2 (MODIFY_EXPR
, TREE_TYPE (to
), to
, arg
);
9064 /* We must only copy the non-tail padding parts. */
9066 tree array_type
, alias_set
;
9068 arg2
= TYPE_SIZE_UNIT (as_base
);
9069 to
= cp_stabilize_reference (to
);
9070 arg0
= cp_build_addr_expr (to
, complain
);
9072 array_type
= build_array_type (unsigned_char_type_node
,
9074 (size_binop (MINUS_EXPR
,
9075 arg2
, size_int (1))));
9076 alias_set
= build_int_cst (build_pointer_type (type
), 0);
9077 t
= build2 (MODIFY_EXPR
, void_type_node
,
9078 build2 (MEM_REF
, array_type
, arg0
, alias_set
),
9079 build2 (MEM_REF
, array_type
, arg
, alias_set
));
9080 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (to
), t
, to
);
9081 TREE_NO_WARNING (val
) = 1;
9084 cp_warn_deprecated_use (fn
, complain
);
9088 else if (trivial_fn_p (fn
))
9090 if (DECL_DESTRUCTOR_P (fn
))
9091 return build_trivial_dtor_call (argarray
[0]);
9092 else if (default_ctor_p (fn
))
9094 if (is_dummy_object (argarray
[0]))
9095 return force_target_expr (DECL_CONTEXT (fn
), void_node
,
9096 no_cleanup_complain
);
9098 return cp_build_fold_indirect_ref (argarray
[0]);
9102 gcc_assert (!force_elide
);
9105 && !mark_used (fn
, complain
))
9106 return error_mark_node
;
9108 /* Warn if the built-in writes to an object of a non-trivial type. */
9109 if (warn_class_memaccess
9110 && vec_safe_length (args
) >= 2
9111 && DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
9112 maybe_warn_class_memaccess (input_location
, fn
, args
);
9114 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0)
9117 tree binfo
= lookup_base (TREE_TYPE (TREE_TYPE (argarray
[0])),
9119 ba_any
, NULL
, complain
);
9120 gcc_assert (binfo
&& binfo
!= error_mark_node
);
9122 argarray
[0] = build_base_path (PLUS_EXPR
, argarray
[0], binfo
, 1,
9124 if (TREE_SIDE_EFFECTS (argarray
[0]))
9125 argarray
[0] = save_expr (argarray
[0]);
9126 t
= build_pointer_type (TREE_TYPE (fn
));
9127 fn
= build_vfn_ref (argarray
[0], DECL_VINDEX (fn
));
9132 /* If FN is marked deprecated, then we've already issued a deprecated-use
9133 warning from mark_used above, so avoid redundantly issuing another one
9134 from build_addr_func. */
9135 warning_sentinel
w (warn_deprecated_decl
);
9137 fn
= build_addr_func (fn
, complain
);
9138 if (fn
== error_mark_node
)
9139 return error_mark_node
;
9142 tree call
= build_cxx_call (fn
, nargs
, argarray
, complain
|decltype_flag
);
9143 if (call
== error_mark_node
)
9145 if (cand
->flags
& LOOKUP_LIST_INIT_CTOR
)
9147 tree c
= extract_call_expr (call
);
9148 /* build_new_op_1 will clear this when appropriate. */
9149 CALL_EXPR_ORDERED_ARGS (c
) = true;
9153 tree c
= extract_call_expr (call
);
9154 if (TREE_CODE (c
) == CALL_EXPR
)
9155 TREE_NO_WARNING (c
) = 1;
9157 if (TREE_CODE (fn
) == ADDR_EXPR
)
9159 tree fndecl
= STRIP_TEMPLATE (TREE_OPERAND (fn
, 0));
9160 if (TREE_CODE (fndecl
) == FUNCTION_DECL
9161 && DECL_IMMEDIATE_FUNCTION_P (fndecl
)
9162 && cp_unevaluated_operand
== 0
9163 && (current_function_decl
== NULL_TREE
9164 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
))
9165 && (current_binding_level
->kind
!= sk_function_parms
9166 || !current_binding_level
->immediate_fn_ctx_p
))
9168 tree obj_arg
= NULL_TREE
;
9169 if (DECL_CONSTRUCTOR_P (fndecl
))
9170 obj_arg
= cand
->first_arg
? cand
->first_arg
: (*args
)[0];
9171 if (obj_arg
&& is_dummy_object (obj_arg
))
9173 call
= build_cplus_new (DECL_CONTEXT (fndecl
), call
, complain
);
9174 obj_arg
= NULL_TREE
;
9176 /* Look through *(const T *)&obj. */
9177 else if (obj_arg
&& TREE_CODE (obj_arg
) == INDIRECT_REF
)
9179 tree addr
= TREE_OPERAND (obj_arg
, 0);
9181 if (TREE_CODE (addr
) == ADDR_EXPR
)
9183 tree typeo
= TREE_TYPE (obj_arg
);
9184 tree typei
= TREE_TYPE (TREE_OPERAND (addr
, 0));
9185 if (same_type_ignoring_top_level_qualifiers_p (typeo
, typei
))
9186 obj_arg
= TREE_OPERAND (addr
, 0);
9189 call
= cxx_constant_value (call
, obj_arg
);
9198 /* Return the DECL of the first non-static subobject of class TYPE
9199 that satisfies the predicate PRED or null if none can be found. */
9201 template <class Predicate
>
9203 first_non_static_field (tree type
, Predicate pred
)
9205 if (!type
|| !CLASS_TYPE_P (type
))
9208 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
9210 if (TREE_CODE (field
) != FIELD_DECL
)
9212 if (TREE_STATIC (field
))
9220 for (tree base_binfo
, binfo
= TYPE_BINFO (type
);
9221 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
9223 tree base
= TREE_TYPE (base_binfo
);
9226 if (tree field
= first_non_static_field (base
, pred
))
9233 struct NonPublicField
9235 bool operator() (const_tree t
)
9237 return DECL_P (t
) && (TREE_PRIVATE (t
) || TREE_PROTECTED (t
));
9241 /* Return the DECL of the first non-public subobject of class TYPE
9242 or null if none can be found. */
9245 first_non_public_field (tree type
)
9247 return first_non_static_field (type
, NonPublicField ());
9250 struct NonTrivialField
9252 bool operator() (const_tree t
)
9254 return !trivial_type_p (DECL_P (t
) ? TREE_TYPE (t
) : t
);
9258 /* Return the DECL of the first non-trivial subobject of class TYPE
9259 or null if none can be found. */
9262 first_non_trivial_field (tree type
)
9264 return first_non_static_field (type
, NonTrivialField ());
9267 } /* unnamed namespace */
9269 /* Return true if all copy and move assignment operator overloads for
9270 class TYPE are trivial and at least one of them is not deleted and,
9271 when ACCESS is set, accessible. Return false otherwise. Set
9272 HASASSIGN to true when the TYPE has a (not necessarily trivial)
9273 copy or move assignment. */
9276 has_trivial_copy_assign_p (tree type
, bool access
, bool *hasassign
)
9278 tree fns
= get_class_binding (type
, assign_op_identifier
);
9279 bool all_trivial
= true;
9281 /* Iterate over overloads of the assignment operator, checking
9282 accessible copy assignments for triviality. */
9284 for (ovl_iterator
oi (fns
); oi
; ++oi
)
9288 /* Skip operators that aren't copy assignments. */
9292 bool accessible
= (!access
|| !(TREE_PRIVATE (f
) || TREE_PROTECTED (f
))
9293 || accessible_p (TYPE_BINFO (type
), f
, true));
9295 /* Skip template assignment operators and deleted functions. */
9296 if (TREE_CODE (f
) != FUNCTION_DECL
|| DECL_DELETED_FN (f
))
9302 if (!accessible
|| !trivial_fn_p (f
))
9303 all_trivial
= false;
9305 /* Break early when both properties have been determined. */
9306 if (*hasassign
&& !all_trivial
)
9310 /* Return true if they're all trivial and one of the expressions
9311 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
9312 tree ref
= cp_build_reference_type (type
, false);
9314 && (is_trivially_xible (MODIFY_EXPR
, type
, type
)
9315 || is_trivially_xible (MODIFY_EXPR
, type
, ref
)));
9318 /* Return true if all copy and move ctor overloads for class TYPE are
9319 trivial and at least one of them is not deleted and, when ACCESS is
9320 set, accessible. Return false otherwise. Set each element of HASCTOR[]
9321 to true when the TYPE has a (not necessarily trivial) default and copy
9322 (or move) ctor, respectively. */
9325 has_trivial_copy_p (tree type
, bool access
, bool hasctor
[2])
9327 tree fns
= get_class_binding (type
, complete_ctor_identifier
);
9328 bool all_trivial
= true;
9330 for (ovl_iterator
oi (fns
); oi
; ++oi
)
9334 /* Skip template constructors. */
9335 if (TREE_CODE (f
) != FUNCTION_DECL
)
9338 bool cpy_or_move_ctor_p
= copy_fn_p (f
);
9340 /* Skip ctors other than default, copy, and move. */
9341 if (!cpy_or_move_ctor_p
&& !default_ctor_p (f
))
9344 if (DECL_DELETED_FN (f
))
9347 bool accessible
= (!access
|| !(TREE_PRIVATE (f
) || TREE_PROTECTED (f
))
9348 || accessible_p (TYPE_BINFO (type
), f
, true));
9351 hasctor
[cpy_or_move_ctor_p
] = true;
9353 if (cpy_or_move_ctor_p
&& (!accessible
|| !trivial_fn_p (f
)))
9354 all_trivial
= false;
9356 /* Break early when both properties have been determined. */
9357 if (hasctor
[0] && hasctor
[1] && !all_trivial
)
9364 /* Issue a warning on a call to the built-in function FNDECL if it is
9365 a raw memory write whose destination is not an object of (something
9366 like) trivial or standard layout type with a non-deleted assignment
9367 and copy ctor. Detects const correctness violations, corrupting
9368 references, virtual table pointers, and bypassing non-trivial
9372 maybe_warn_class_memaccess (location_t loc
, tree fndecl
,
9373 const vec
<tree
, va_gc
> *args
)
9375 /* Except for bcopy where it's second, the destination pointer is
9376 the first argument for all functions handled here. Compute
9377 the index of the destination and source arguments. */
9378 unsigned dstidx
= DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_BCOPY
;
9379 unsigned srcidx
= !dstidx
;
9381 tree dest
= (*args
)[dstidx
];
9382 if (!TREE_TYPE (dest
)
9383 || (TREE_CODE (TREE_TYPE (dest
)) != ARRAY_TYPE
9384 && !INDIRECT_TYPE_P (TREE_TYPE (dest
))))
9387 tree srctype
= NULL_TREE
;
9389 /* Determine the type of the pointed-to object and whether it's
9390 a complete class type. */
9391 tree desttype
= TREE_TYPE (TREE_TYPE (dest
));
9393 if (!desttype
|| !COMPLETE_TYPE_P (desttype
) || !CLASS_TYPE_P (desttype
))
9396 /* Check to see if the raw memory call is made by a non-static member
9397 function with THIS as the destination argument for the destination
9398 type. If so, and if the class has no non-trivial bases or members,
9399 be more permissive. */
9400 if (current_function_decl
9401 && DECL_NONSTATIC_MEMBER_FUNCTION_P (current_function_decl
)
9402 && is_this_parameter (tree_strip_nop_conversions (dest
)))
9404 tree ctx
= DECL_CONTEXT (current_function_decl
);
9405 bool special
= same_type_ignoring_top_level_qualifiers_p (ctx
, desttype
);
9406 tree binfo
= TYPE_BINFO (ctx
);
9409 && !BINFO_VTABLE (binfo
)
9410 && !first_non_trivial_field (desttype
))
9414 /* True if the class is trivial. */
9415 bool trivial
= trivial_type_p (desttype
);
9417 /* Set to true if DESTYPE has an accessible copy assignment. */
9418 bool hasassign
= false;
9419 /* True if all of the class' overloaded copy assignment operators
9420 are all trivial (and not deleted) and at least one of them is
9422 bool trivassign
= has_trivial_copy_assign_p (desttype
, true, &hasassign
);
9424 /* Set to true if DESTTYPE has an accessible default and copy ctor,
9426 bool hasctors
[2] = { false, false };
9428 /* True if all of the class' overloaded copy constructors are all
9429 trivial (and not deleted) and at least one of them is accessible. */
9430 bool trivcopy
= has_trivial_copy_p (desttype
, true, hasctors
);
9432 /* Set FLD to the first private/protected member of the class. */
9433 tree fld
= trivial
? first_non_public_field (desttype
) : NULL_TREE
;
9435 /* The warning format string. */
9436 const char *warnfmt
= NULL
;
9437 /* A suggested alternative to offer instead of the raw memory call.
9438 Empty string when none can be come up with. */
9439 const char *suggest
= "";
9440 bool warned
= false;
9442 switch (DECL_FUNCTION_CODE (fndecl
))
9444 case BUILT_IN_MEMSET
:
9445 if (!integer_zerop (maybe_constant_value ((*args
)[1])))
9447 /* Diagnose setting non-copy-assignable or non-trivial types,
9448 or types with a private member, to (potentially) non-zero
9449 bytes. Since the value of the bytes being written is unknown,
9450 suggest using assignment instead (if one exists). Also warn
9451 for writes into objects for which zero-initialization doesn't
9452 mean all bits clear (pointer-to-member data, where null is all
9453 bits set). Since the value being written is (most likely)
9454 non-zero, simply suggest assignment (but not copy assignment). */
9455 suggest
= "; use assignment instead";
9457 warnfmt
= G_("%qD writing to an object of type %#qT with "
9458 "no trivial copy-assignment");
9460 warnfmt
= G_("%qD writing to an object of non-trivial type %#qT%s");
9463 const char *access
= TREE_PRIVATE (fld
) ? "private" : "protected";
9464 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9465 "%qD writing to an object of type %#qT with "
9467 fndecl
, desttype
, access
, fld
);
9469 else if (!zero_init_p (desttype
))
9470 warnfmt
= G_("%qD writing to an object of type %#qT containing "
9471 "a pointer to data member%s");
9477 case BUILT_IN_BZERO
:
9478 /* Similarly to the above, diagnose clearing non-trivial or non-
9479 standard layout objects, or objects of types with no assignmenmt.
9480 Since the value being written is known to be zero, suggest either
9481 copy assignment, copy ctor, or default ctor as an alternative,
9482 depending on what's available. */
9484 if (hasassign
&& hasctors
[0])
9485 suggest
= G_("; use assignment or value-initialization instead");
9487 suggest
= G_("; use assignment instead");
9488 else if (hasctors
[0])
9489 suggest
= G_("; use value-initialization instead");
9492 warnfmt
= G_("%qD clearing an object of type %#qT with "
9493 "no trivial copy-assignment%s");
9495 warnfmt
= G_("%qD clearing an object of non-trivial type %#qT%s");
9496 else if (!zero_init_p (desttype
))
9497 warnfmt
= G_("%qD clearing an object of type %#qT containing "
9498 "a pointer-to-member%s");
9501 case BUILT_IN_BCOPY
:
9502 case BUILT_IN_MEMCPY
:
9503 case BUILT_IN_MEMMOVE
:
9504 case BUILT_IN_MEMPCPY
:
9505 /* Determine the type of the source object. */
9506 srctype
= TREE_TYPE ((*args
)[srcidx
]);
9507 if (!srctype
|| !INDIRECT_TYPE_P (srctype
))
9508 srctype
= void_type_node
;
9510 srctype
= TREE_TYPE (srctype
);
9512 /* Since it's impossible to determine wheter the byte copy is
9513 being used in place of assignment to an existing object or
9514 as a substitute for initialization, assume it's the former.
9515 Determine the best alternative to use instead depending on
9516 what's not deleted. */
9517 if (hasassign
&& hasctors
[1])
9518 suggest
= G_("; use copy-assignment or copy-initialization instead");
9520 suggest
= G_("; use copy-assignment instead");
9521 else if (hasctors
[1])
9522 suggest
= G_("; use copy-initialization instead");
9525 warnfmt
= G_("%qD writing to an object of type %#qT with no trivial "
9526 "copy-assignment%s");
9527 else if (!trivially_copyable_p (desttype
))
9528 warnfmt
= G_("%qD writing to an object of non-trivially copyable "
9531 warnfmt
= G_("%qD writing to an object with a deleted copy constructor");
9534 && !VOID_TYPE_P (srctype
)
9535 && !is_byte_access_type (srctype
)
9536 && !same_type_ignoring_top_level_qualifiers_p (desttype
,
9539 /* Warn when copying into a non-trivial object from an object
9540 of a different type other than void or char. */
9541 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9542 "%qD copying an object of non-trivial type "
9543 "%#qT from an array of %#qT",
9544 fndecl
, desttype
, srctype
);
9547 && !VOID_TYPE_P (srctype
)
9548 && !is_byte_access_type (srctype
)
9549 && !same_type_ignoring_top_level_qualifiers_p (desttype
,
9552 const char *access
= TREE_PRIVATE (fld
) ? "private" : "protected";
9553 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9554 "%qD copying an object of type %#qT with "
9555 "%qs member %qD from an array of %#qT; use "
9556 "assignment or copy-initialization instead",
9557 fndecl
, desttype
, access
, fld
, srctype
);
9559 else if (!trivial
&& vec_safe_length (args
) > 2)
9561 tree sz
= maybe_constant_value ((*args
)[2]);
9562 if (!tree_fits_uhwi_p (sz
))
9565 /* Finally, warn on partial copies. */
9566 unsigned HOST_WIDE_INT typesize
9567 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype
));
9568 if (unsigned HOST_WIDE_INT partial
= tree_to_uhwi (sz
) % typesize
)
9569 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9570 (typesize
- partial
> 1
9571 ? G_("%qD writing to an object of "
9572 "a non-trivial type %#qT leaves %wu "
9574 : G_("%qD writing to an object of "
9575 "a non-trivial type %#qT leaves %wu "
9577 fndecl
, desttype
, typesize
- partial
);
9581 case BUILT_IN_REALLOC
:
9583 if (!trivially_copyable_p (desttype
))
9584 warnfmt
= G_("%qD moving an object of non-trivially copyable type "
9585 "%#qT; use %<new%> and %<delete%> instead");
9587 warnfmt
= G_("%qD moving an object of type %#qT with deleted copy "
9588 "constructor; use %<new%> and %<delete%> instead");
9589 else if (!get_dtor (desttype
, tf_none
))
9590 warnfmt
= G_("%qD moving an object of type %#qT with deleted "
9594 tree sz
= maybe_constant_value ((*args
)[1]);
9595 if (TREE_CODE (sz
) == INTEGER_CST
9596 && tree_int_cst_lt (sz
, TYPE_SIZE_UNIT (desttype
)))
9597 /* Finally, warn on reallocation into insufficient space. */
9598 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9599 "%qD moving an object of non-trivial type "
9600 "%#qT and size %E into a region of size %E",
9601 fndecl
, desttype
, TYPE_SIZE_UNIT (desttype
),
9613 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9614 warnfmt
, fndecl
, desttype
, suggest
);
9616 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9617 warnfmt
, fndecl
, desttype
);
9621 inform (location_of (desttype
), "%#qT declared here", desttype
);
9624 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
9625 If FN is the result of resolving an overloaded target built-in,
9626 ORIG_FNDECL is the original function decl, otherwise it is null.
9627 This function performs no overload resolution, conversion, or other
9628 high-level operations. */
9631 build_cxx_call (tree fn
, int nargs
, tree
*argarray
,
9632 tsubst_flags_t complain
, tree orig_fndecl
)
9636 /* Remember roughly where this call is. */
9637 location_t loc
= cp_expr_loc_or_input_loc (fn
);
9638 fn
= build_call_a (fn
, nargs
, argarray
);
9639 SET_EXPR_LOCATION (fn
, loc
);
9641 fndecl
= get_callee_fndecl (fn
);
9643 orig_fndecl
= fndecl
;
9645 /* Check that arguments to builtin functions match the expectations. */
9647 && !processing_template_decl
9648 && fndecl_built_in_p (fndecl
))
9652 /* We need to take care that values to BUILT_IN_NORMAL
9654 for (i
= 0; i
< nargs
; i
++)
9655 argarray
[i
] = maybe_constant_value (argarray
[i
]);
9657 if (!check_builtin_function_arguments (EXPR_LOCATION (fn
), vNULL
, fndecl
,
9658 orig_fndecl
, nargs
, argarray
))
9659 return error_mark_node
;
9662 if (VOID_TYPE_P (TREE_TYPE (fn
)))
9665 /* 5.2.2/11: If a function call is a prvalue of object type: if the
9666 function call is either the operand of a decltype-specifier or the
9667 right operand of a comma operator that is the operand of a
9668 decltype-specifier, a temporary object is not introduced for the
9669 prvalue. The type of the prvalue may be incomplete. */
9670 if (!(complain
& tf_decltype
))
9672 fn
= require_complete_type_sfinae (fn
, complain
);
9673 if (fn
== error_mark_node
)
9674 return error_mark_node
;
9676 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn
)))
9678 fn
= build_cplus_new (TREE_TYPE (fn
), fn
, complain
);
9679 maybe_warn_parm_abi (TREE_TYPE (fn
), loc
);
9682 return convert_from_reference (fn
);
9685 /* Returns the value to use for the in-charge parameter when making a
9686 call to a function with the indicated NAME.
9688 FIXME:Can't we find a neater way to do this mapping? */
9691 in_charge_arg_for_name (tree name
)
9693 if (IDENTIFIER_CTOR_P (name
))
9695 if (name
== complete_ctor_identifier
)
9696 return integer_one_node
;
9697 gcc_checking_assert (name
== base_ctor_identifier
);
9701 if (name
== complete_dtor_identifier
)
9702 return integer_two_node
;
9703 else if (name
== deleting_dtor_identifier
)
9704 return integer_three_node
;
9705 gcc_checking_assert (name
== base_dtor_identifier
);
9708 return integer_zero_node
;
9711 /* We've built up a constructor call RET. Complain if it delegates to the
9712 constructor we're currently compiling. */
9715 check_self_delegation (tree ret
)
9717 if (TREE_CODE (ret
) == TARGET_EXPR
)
9718 ret
= TARGET_EXPR_INITIAL (ret
);
9719 tree fn
= cp_get_callee_fndecl_nofold (ret
);
9720 if (fn
&& DECL_ABSTRACT_ORIGIN (fn
) == current_function_decl
)
9721 error ("constructor delegates to itself");
9724 /* Build a call to a constructor, destructor, or an assignment
9725 operator for INSTANCE, an expression with class type. NAME
9726 indicates the special member function to call; *ARGS are the
9727 arguments. ARGS may be NULL. This may change ARGS. BINFO
9728 indicates the base of INSTANCE that is to be passed as the `this'
9729 parameter to the member function called.
9731 FLAGS are the LOOKUP_* flags to use when processing the call.
9733 If NAME indicates a complete object constructor, INSTANCE may be
9734 NULL_TREE. In this case, the caller will call build_cplus_new to
9735 store the newly constructed object into a VAR_DECL. */
9738 build_special_member_call (tree instance
, tree name
, vec
<tree
, va_gc
> **args
,
9739 tree binfo
, int flags
, tsubst_flags_t complain
)
9742 /* The type of the subobject to be constructed or destroyed. */
9744 vec
<tree
, va_gc
> *allocated
= NULL
;
9747 gcc_assert (IDENTIFIER_CDTOR_P (name
) || name
== assign_op_identifier
);
9749 if (error_operand_p (instance
))
9750 return error_mark_node
;
9752 if (IDENTIFIER_DTOR_P (name
))
9754 gcc_assert (args
== NULL
|| vec_safe_is_empty (*args
));
9755 if (!type_build_dtor_call (TREE_TYPE (instance
)))
9756 /* Shortcut to avoid lazy destructor declaration. */
9757 return build_trivial_dtor_call (instance
);
9762 /* Resolve the name. */
9763 if (!complete_type_or_maybe_complain (binfo
, NULL_TREE
, complain
))
9764 return error_mark_node
;
9766 binfo
= TYPE_BINFO (binfo
);
9769 gcc_assert (binfo
!= NULL_TREE
);
9771 class_type
= BINFO_TYPE (binfo
);
9773 /* Handle the special case where INSTANCE is NULL_TREE. */
9774 if (name
== complete_ctor_identifier
&& !instance
)
9775 instance
= build_dummy_object (class_type
);
9778 /* Convert to the base class, if necessary. */
9779 if (!same_type_ignoring_top_level_qualifiers_p
9780 (TREE_TYPE (instance
), BINFO_TYPE (binfo
)))
9782 if (IDENTIFIER_CDTOR_P (name
))
9783 /* For constructors and destructors, either the base is
9784 non-virtual, or it is virtual but we are doing the
9785 conversion from a constructor or destructor for the
9786 complete object. In either case, we can convert
9788 instance
= convert_to_base_statically (instance
, binfo
);
9791 /* However, for assignment operators, we must convert
9792 dynamically if the base is virtual. */
9793 gcc_checking_assert (name
== assign_op_identifier
);
9794 instance
= build_base_path (PLUS_EXPR
, instance
,
9795 binfo
, /*nonnull=*/1, complain
);
9800 gcc_assert (instance
!= NULL_TREE
);
9802 /* In C++17, "If the initializer expression is a prvalue and the
9803 cv-unqualified version of the source type is the same class as the class
9804 of the destination, the initializer expression is used to initialize the
9805 destination object." Handle that here to avoid doing overload
9807 if (cxx_dialect
>= cxx17
9808 && args
&& vec_safe_length (*args
) == 1
9809 && name
== complete_ctor_identifier
)
9811 tree arg
= (**args
)[0];
9813 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
9814 && !TYPE_HAS_LIST_CTOR (class_type
)
9815 && CONSTRUCTOR_NELTS (arg
) == 1)
9816 arg
= CONSTRUCTOR_ELT (arg
, 0)->value
;
9818 if ((TREE_CODE (arg
) == TARGET_EXPR
9819 || TREE_CODE (arg
) == CONSTRUCTOR
)
9820 && (same_type_ignoring_top_level_qualifiers_p
9821 (class_type
, TREE_TYPE (arg
))))
9823 if (is_dummy_object (instance
))
9825 else if (TREE_CODE (arg
) == TARGET_EXPR
)
9826 TARGET_EXPR_DIRECT_INIT_P (arg
) = true;
9828 if ((complain
& tf_error
)
9829 && (flags
& LOOKUP_DELEGATING_CONS
))
9830 check_self_delegation (arg
);
9831 /* Avoid change of behavior on Wunused-var-2.C. */
9832 instance
= mark_lvalue_use (instance
);
9833 return build2 (INIT_EXPR
, class_type
, instance
, arg
);
9837 fns
= lookup_fnfields (binfo
, name
, 1, complain
);
9839 /* When making a call to a constructor or destructor for a subobject
9840 that uses virtual base classes, pass down a pointer to a VTT for
9842 if ((name
== base_ctor_identifier
9843 || name
== base_dtor_identifier
)
9844 && CLASSTYPE_VBASECLASSES (class_type
))
9849 /* If the current function is a complete object constructor
9850 or destructor, then we fetch the VTT directly.
9851 Otherwise, we look it up using the VTT we were given. */
9852 vtt
= DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type
));
9853 vtt
= decay_conversion (vtt
, complain
);
9854 if (vtt
== error_mark_node
)
9855 return error_mark_node
;
9856 vtt
= build_if_in_charge (vtt
, current_vtt_parm
);
9857 if (BINFO_SUBVTT_INDEX (binfo
))
9858 sub_vtt
= fold_build_pointer_plus (vtt
, BINFO_SUBVTT_INDEX (binfo
));
9864 allocated
= make_tree_vector ();
9868 vec_safe_insert (*args
, 0, sub_vtt
);
9871 ret
= build_new_method_call (instance
, fns
, args
,
9872 TYPE_BINFO (BINFO_TYPE (binfo
)),
9876 if (allocated
!= NULL
)
9877 release_tree_vector (allocated
);
9879 if ((complain
& tf_error
)
9880 && (flags
& LOOKUP_DELEGATING_CONS
)
9881 && name
== complete_ctor_identifier
)
9882 check_self_delegation (ret
);
9887 /* Return the NAME, as a C string. The NAME indicates a function that
9888 is a member of TYPE. *FREE_P is set to true if the caller must
9889 free the memory returned.
9891 Rather than go through all of this, we should simply set the names
9892 of constructors and destructors appropriately, and dispense with
9893 ctor_identifier, dtor_identifier, etc. */
9896 name_as_c_string (tree name
, tree type
, bool *free_p
)
9898 const char *pretty_name
;
9900 /* Assume that we will not allocate memory. */
9902 /* Constructors and destructors are special. */
9903 if (IDENTIFIER_CDTOR_P (name
))
9906 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type
)));
9907 /* For a destructor, add the '~'. */
9908 if (IDENTIFIER_DTOR_P (name
))
9910 pretty_name
= concat ("~", pretty_name
, NULL
);
9911 /* Remember that we need to free the memory allocated. */
9915 else if (IDENTIFIER_CONV_OP_P (name
))
9917 pretty_name
= concat ("operator ",
9918 type_as_string_translate (TREE_TYPE (name
),
9919 TFF_PLAIN_IDENTIFIER
),
9921 /* Remember that we need to free the memory allocated. */
9925 pretty_name
= identifier_to_locale (IDENTIFIER_POINTER (name
));
9927 return CONST_CAST (char *, pretty_name
);
9930 /* If CANDIDATES contains exactly one candidate, return it, otherwise
9933 static z_candidate
*
9934 single_z_candidate (z_candidate
*candidates
)
9936 if (candidates
== NULL
)
9939 if (candidates
->next
)
9945 /* If CANDIDATE is invalid due to a bad argument type, return the
9946 pertinent conversion_info.
9948 Otherwise, return NULL. */
9950 static const conversion_info
*
9951 maybe_get_bad_conversion_for_unmatched_call (const z_candidate
*candidate
)
9953 /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
9954 rejection_reason
*r
= candidate
->reason
;
9964 case rr_arg_conversion
:
9965 return &r
->u
.conversion
;
9967 case rr_bad_arg_conversion
:
9968 return &r
->u
.bad_conversion
;
9972 /* Issue an error and note complaining about a bad argument type at a
9973 callsite with a single candidate FNDECL.
9975 ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
9976 case input_location is used).
9977 FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
9978 the formal parameter. */
9981 complain_about_bad_argument (location_t arg_loc
,
9982 tree from_type
, tree to_type
,
9983 tree fndecl
, int parmnum
)
9985 auto_diagnostic_group d
;
9986 range_label_for_type_mismatch
rhs_label (from_type
, to_type
);
9987 range_label
*label
= &rhs_label
;
9988 if (arg_loc
== UNKNOWN_LOCATION
)
9990 arg_loc
= input_location
;
9993 gcc_rich_location
richloc (arg_loc
, label
);
9995 "cannot convert %qH to %qI",
9996 from_type
, to_type
);
9997 maybe_inform_about_fndecl_for_bogus_argument_init (fndecl
,
10001 /* Subroutine of build_new_method_call_1, for where there are no viable
10002 candidates for the call. */
10005 complain_about_no_candidates_for_method_call (tree instance
,
10006 z_candidate
*candidates
,
10007 tree explicit_targs
,
10009 tree optype
, tree name
,
10010 bool skip_first_for_error
,
10011 vec
<tree
, va_gc
> *user_args
)
10013 auto_diagnostic_group d
;
10014 if (!COMPLETE_OR_OPEN_TYPE_P (basetype
))
10015 cxx_incomplete_type_error (instance
, basetype
);
10017 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
10018 basetype
, optype
, build_tree_list_vec (user_args
),
10019 TREE_TYPE (instance
));
10022 /* Special-case for when there's a single candidate that's failing
10023 due to a bad argument type. */
10024 if (z_candidate
*candidate
= single_z_candidate (candidates
))
10025 if (const conversion_info
*conv
10026 = maybe_get_bad_conversion_for_unmatched_call (candidate
))
10028 tree from_type
= conv
->from
;
10029 if (!TYPE_P (conv
->from
))
10030 from_type
= lvalue_type (conv
->from
);
10031 complain_about_bad_argument (conv
->loc
,
10032 from_type
, conv
->to_type
,
10033 candidate
->fn
, conv
->n_arg
);
10037 tree arglist
= build_tree_list_vec (user_args
);
10038 tree errname
= name
;
10039 bool twiddle
= false;
10040 if (IDENTIFIER_CDTOR_P (errname
))
10042 twiddle
= IDENTIFIER_DTOR_P (errname
);
10043 errname
= constructor_name (basetype
);
10045 if (explicit_targs
)
10046 errname
= lookup_template_function (errname
, explicit_targs
);
10047 if (skip_first_for_error
)
10048 arglist
= TREE_CHAIN (arglist
);
10049 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
10050 basetype
, &"~"[!twiddle
], errname
, arglist
,
10051 TREE_TYPE (instance
));
10053 print_z_candidates (location_of (name
), candidates
);
10056 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
10057 be set, upon return, to the function called. ARGS may be NULL.
10058 This may change ARGS. */
10061 build_new_method_call_1 (tree instance
, tree fns
, vec
<tree
, va_gc
> **args
,
10062 tree conversion_path
, int flags
,
10063 tree
*fn_p
, tsubst_flags_t complain
)
10065 struct z_candidate
*candidates
= 0, *cand
;
10066 tree explicit_targs
= NULL_TREE
;
10067 tree basetype
= NULL_TREE
;
10070 tree first_mem_arg
= NULL_TREE
;
10072 bool skip_first_for_error
;
10073 vec
<tree
, va_gc
> *user_args
;
10076 int template_only
= 0;
10078 tree orig_instance
;
10080 vec
<tree
, va_gc
> *orig_args
= NULL
;
10083 gcc_assert (instance
!= NULL_TREE
);
10085 /* We don't know what function we're going to call, yet. */
10089 if (error_operand_p (instance
)
10090 || !fns
|| error_operand_p (fns
))
10091 return error_mark_node
;
10093 if (!BASELINK_P (fns
))
10095 if (complain
& tf_error
)
10096 error ("call to non-function %qD", fns
);
10097 return error_mark_node
;
10100 orig_instance
= instance
;
10103 /* Dismantle the baselink to collect all the information we need. */
10104 if (!conversion_path
)
10105 conversion_path
= BASELINK_BINFO (fns
);
10106 access_binfo
= BASELINK_ACCESS_BINFO (fns
);
10107 optype
= BASELINK_OPTYPE (fns
);
10108 fns
= BASELINK_FUNCTIONS (fns
);
10109 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
10111 explicit_targs
= TREE_OPERAND (fns
, 1);
10112 fns
= TREE_OPERAND (fns
, 0);
10115 gcc_assert (OVL_P (fns
));
10116 fn
= OVL_FIRST (fns
);
10117 name
= DECL_NAME (fn
);
10119 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (instance
));
10120 gcc_assert (CLASS_TYPE_P (basetype
));
10122 user_args
= args
== NULL
? NULL
: *args
;
10123 /* Under DR 147 A::A() is an invalid constructor call,
10124 not a functional cast. */
10125 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
10127 if (! (complain
& tf_error
))
10128 return error_mark_node
;
10130 basetype
= DECL_CONTEXT (fn
);
10131 name
= constructor_name (basetype
);
10132 auto_diagnostic_group d
;
10133 if (permerror (input_location
,
10134 "cannot call constructor %<%T::%D%> directly",
10136 inform (input_location
, "for a function-style cast, remove the "
10137 "redundant %<::%D%>", name
);
10138 call
= build_functional_cast (input_location
, basetype
,
10139 build_tree_list_vec (user_args
),
10144 if (processing_template_decl
)
10146 orig_args
= args
== NULL
? NULL
: make_tree_vector_copy (*args
);
10147 instance
= build_non_dependent_expr (instance
);
10149 make_args_non_dependent (*args
);
10152 /* Process the argument list. */
10153 if (args
!= NULL
&& *args
!= NULL
)
10155 *args
= resolve_args (*args
, complain
);
10157 return error_mark_node
;
10161 /* Consider the object argument to be used even if we end up selecting a
10162 static member function. */
10163 instance
= mark_type_use (instance
);
10165 /* Figure out whether to skip the first argument for the error
10166 message we will display to users if an error occurs. We don't
10167 want to display any compiler-generated arguments. The "this"
10168 pointer hasn't been added yet. However, we must remove the VTT
10169 pointer if this is a call to a base-class constructor or
10171 skip_first_for_error
= false;
10172 if (IDENTIFIER_CDTOR_P (name
))
10174 /* Callers should explicitly indicate whether they want to ctor
10175 the complete object or just the part without virtual bases. */
10176 gcc_assert (name
!= ctor_identifier
);
10178 /* Remove the VTT pointer, if present. */
10179 if ((name
== base_ctor_identifier
|| name
== base_dtor_identifier
)
10180 && CLASSTYPE_VBASECLASSES (basetype
))
10181 skip_first_for_error
= true;
10183 /* It's OK to call destructors and constructors on cv-qualified
10184 objects. Therefore, convert the INSTANCE to the unqualified
10185 type, if necessary. */
10186 if (!same_type_p (basetype
, TREE_TYPE (instance
)))
10188 instance
= build_this (instance
);
10189 instance
= build_nop (build_pointer_type (basetype
), instance
);
10190 instance
= build_fold_indirect_ref (instance
);
10194 gcc_assert (!DECL_DESTRUCTOR_P (fn
) && !DECL_CONSTRUCTOR_P (fn
));
10196 /* For the overload resolution we need to find the actual `this`
10197 that would be captured if the call turns out to be to a
10198 non-static member function. Do not actually capture it at this
10200 if (DECL_CONSTRUCTOR_P (fn
))
10201 /* Constructors don't use the enclosing 'this'. */
10202 first_mem_arg
= instance
;
10204 first_mem_arg
= maybe_resolve_dummy (instance
, false);
10206 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10207 p
= conversion_obstack_alloc (0);
10209 /* The number of arguments artificial parms in ARGS; we subtract one because
10210 there's no 'this' in ARGS. */
10211 unsigned skip
= num_artificial_parms_for (fn
) - 1;
10213 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
10214 initializer, not T({ }). */
10215 if (DECL_CONSTRUCTOR_P (fn
)
10216 && vec_safe_length (user_args
) > skip
10217 && DIRECT_LIST_INIT_P ((*user_args
)[skip
]))
10219 tree init_list
= (*user_args
)[skip
];
10220 tree init
= NULL_TREE
;
10222 gcc_assert (user_args
->length () == skip
+ 1
10223 && !(flags
& LOOKUP_ONLYCONVERTING
));
10225 /* If the initializer list has no elements and T is a class type with
10226 a default constructor, the object is value-initialized. Handle
10227 this here so we don't need to handle it wherever we use
10228 build_special_member_call. */
10229 if (CONSTRUCTOR_NELTS (init_list
) == 0
10230 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
10231 /* For a user-provided default constructor, use the normal
10232 mechanisms so that protected access works. */
10233 && type_has_non_user_provided_default_constructor (basetype
)
10234 && !processing_template_decl
)
10235 init
= build_value_init (basetype
, complain
);
10237 /* If BASETYPE is an aggregate, we need to do aggregate
10239 else if (CP_AGGREGATE_TYPE_P (basetype
))
10241 init
= reshape_init (basetype
, init_list
, complain
);
10242 init
= digest_init (basetype
, init
, complain
);
10247 if (is_dummy_object (instance
))
10248 return get_target_expr_sfinae (init
, complain
);
10249 init
= build2 (INIT_EXPR
, TREE_TYPE (instance
), instance
, init
);
10250 TREE_SIDE_EFFECTS (init
) = true;
10254 /* Otherwise go ahead with overload resolution. */
10255 add_list_candidates (fns
, first_mem_arg
, user_args
,
10256 basetype
, explicit_targs
, template_only
,
10257 conversion_path
, access_binfo
, flags
,
10258 &candidates
, complain
);
10261 add_candidates (fns
, first_mem_arg
, user_args
, optype
,
10262 explicit_targs
, template_only
, conversion_path
,
10263 access_binfo
, flags
, &candidates
, complain
);
10265 any_viable_p
= false;
10266 candidates
= splice_viable (candidates
, false, &any_viable_p
);
10270 /* [dcl.init], 17.6.2.2:
10272 Otherwise, if no constructor is viable, the destination type is
10273 a (possibly cv-qualified) aggregate class A, and the initializer
10274 is a parenthesized expression-list, the object is initialized as
10277 We achieve this by building up a CONSTRUCTOR, as for list-init,
10278 and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
10280 if (DECL_CONSTRUCTOR_P (fn
)
10281 && !(flags
& LOOKUP_ONLYCONVERTING
)
10282 && cxx_dialect
>= cxx20
10283 && CP_AGGREGATE_TYPE_P (basetype
)
10284 && !vec_safe_is_empty (user_args
))
10286 /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
10287 tree list
= build_tree_list_vec (user_args
);
10288 tree ctor
= build_constructor_from_list (init_list_type_node
, list
);
10289 CONSTRUCTOR_IS_DIRECT_INIT (ctor
) = true;
10290 CONSTRUCTOR_IS_PAREN_INIT (ctor
) = true;
10291 if (is_dummy_object (instance
))
10295 ctor
= digest_init (basetype
, ctor
, complain
);
10296 if (ctor
== error_mark_node
)
10297 return error_mark_node
;
10298 ctor
= build2 (INIT_EXPR
, TREE_TYPE (instance
), instance
, ctor
);
10299 TREE_SIDE_EFFECTS (ctor
) = true;
10303 if (complain
& tf_error
)
10304 complain_about_no_candidates_for_method_call (instance
, candidates
,
10305 explicit_targs
, basetype
,
10307 skip_first_for_error
,
10309 call
= error_mark_node
;
10313 cand
= tourney (candidates
, complain
);
10320 if (complain
& tf_error
)
10322 pretty_name
= name_as_c_string (name
, basetype
, &free_p
);
10323 arglist
= build_tree_list_vec (user_args
);
10324 if (skip_first_for_error
)
10325 arglist
= TREE_CHAIN (arglist
);
10326 auto_diagnostic_group d
;
10327 if (!any_strictly_viable (candidates
))
10328 error ("no matching function for call to %<%s(%A)%>",
10329 pretty_name
, arglist
);
10331 error ("call of overloaded %<%s(%A)%> is ambiguous",
10332 pretty_name
, arglist
);
10333 print_z_candidates (location_of (name
), candidates
);
10335 free (pretty_name
);
10337 call
= error_mark_node
;
10344 if (!(flags
& LOOKUP_NONVIRTUAL
)
10345 && DECL_PURE_VIRTUAL_P (fn
)
10346 && instance
== current_class_ref
10347 && (complain
& tf_warning
))
10349 /* This is not an error, it is runtime undefined
10351 if (!current_function_decl
)
10352 warning (0, "pure virtual %q#D called from "
10353 "non-static data member initializer", fn
);
10354 else if (DECL_CONSTRUCTOR_P (current_function_decl
)
10355 || DECL_DESTRUCTOR_P (current_function_decl
))
10356 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl
)
10357 ? G_("pure virtual %q#D called from constructor")
10358 : G_("pure virtual %q#D called from destructor")),
10362 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
10363 && !DECL_CONSTRUCTOR_P (fn
)
10364 && is_dummy_object (instance
))
10366 instance
= maybe_resolve_dummy (instance
, true);
10367 if (instance
== error_mark_node
)
10368 call
= error_mark_node
;
10369 else if (!is_dummy_object (instance
))
10371 /* We captured 'this' in the current lambda now that
10372 we know we really need it. */
10373 cand
->first_arg
= instance
;
10375 else if (any_dependent_bases_p ())
10376 /* We can't tell until instantiation time whether we can use
10377 *this as the implicit object argument. */;
10380 if (complain
& tf_error
)
10381 error ("cannot call member function %qD without object",
10383 call
= error_mark_node
;
10387 if (call
!= error_mark_node
)
10389 /* Now we know what function is being called. */
10392 /* Build the actual CALL_EXPR. */
10393 call
= build_over_call (cand
, flags
, complain
);
10394 /* In an expression of the form `a->f()' where `f' turns
10395 out to be a static member function, `a' is
10396 none-the-less evaluated. */
10397 if (TREE_CODE (TREE_TYPE (fn
)) != METHOD_TYPE
10398 && !is_dummy_object (instance
)
10399 && TREE_SIDE_EFFECTS (instance
))
10401 /* But avoid the implicit lvalue-rvalue conversion when 'a'
10404 if (TREE_THIS_VOLATILE (a
))
10405 a
= build_this (a
);
10406 call
= build2 (COMPOUND_EXPR
, TREE_TYPE (call
), a
, call
);
10408 else if (call
!= error_mark_node
10409 && DECL_DESTRUCTOR_P (cand
->fn
)
10410 && !VOID_TYPE_P (TREE_TYPE (call
)))
10411 /* An explicit call of the form "x->~X()" has type
10412 "void". However, on platforms where destructors
10413 return "this" (i.e., those where
10414 targetm.cxx.cdtor_returns_this is true), such calls
10415 will appear to have a return value of pointer type
10416 to the low-level call machinery. We do not want to
10417 change the low-level machinery, since we want to be
10418 able to optimize "delete f()" on such platforms as
10419 "operator delete(~X(f()))" (rather than generating
10420 "t = f(), ~X(t), operator delete (t)"). */
10421 call
= build_nop (void_type_node
, call
);
10426 if (processing_template_decl
&& call
!= error_mark_node
)
10428 bool cast_to_void
= false;
10430 if (TREE_CODE (call
) == COMPOUND_EXPR
)
10431 call
= TREE_OPERAND (call
, 1);
10432 else if (TREE_CODE (call
) == NOP_EXPR
)
10434 cast_to_void
= true;
10435 call
= TREE_OPERAND (call
, 0);
10437 if (INDIRECT_REF_P (call
))
10438 call
= TREE_OPERAND (call
, 0);
10439 call
= (build_min_non_dep_call_vec
10441 build_min (COMPONENT_REF
, TREE_TYPE (CALL_EXPR_FN (call
)),
10442 orig_instance
, orig_fns
, NULL_TREE
),
10444 SET_EXPR_LOCATION (call
, input_location
);
10445 call
= convert_from_reference (call
);
10447 call
= build_nop (void_type_node
, call
);
10450 /* Free all the conversions we allocated. */
10451 obstack_free (&conversion_obstack
, p
);
10453 if (orig_args
!= NULL
)
10454 release_tree_vector (orig_args
);
10459 /* Wrapper for above. */
10462 build_new_method_call (tree instance
, tree fns
, vec
<tree
, va_gc
> **args
,
10463 tree conversion_path
, int flags
,
10464 tree
*fn_p
, tsubst_flags_t complain
)
10467 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
10468 ret
= build_new_method_call_1 (instance
, fns
, args
, conversion_path
, flags
,
10470 timevar_cond_stop (TV_OVERLOAD
, subtime
);
10474 /* Returns true iff standard conversion sequence ICS1 is a proper
10475 subsequence of ICS2. */
10478 is_subseq (conversion
*ics1
, conversion
*ics2
)
10480 /* We can assume that a conversion of the same code
10481 between the same types indicates a subsequence since we only get
10482 here if the types we are converting from are the same. */
10484 while (ics1
->kind
== ck_rvalue
10485 || ics1
->kind
== ck_lvalue
)
10486 ics1
= next_conversion (ics1
);
10490 while (ics2
->kind
== ck_rvalue
10491 || ics2
->kind
== ck_lvalue
)
10492 ics2
= next_conversion (ics2
);
10494 if (ics2
->kind
== ck_user
10495 || ics2
->kind
== ck_ambig
10496 || ics2
->kind
== ck_aggr
10497 || ics2
->kind
== ck_list
10498 || ics2
->kind
== ck_identity
)
10499 /* At this point, ICS1 cannot be a proper subsequence of
10500 ICS2. We can get a USER_CONV when we are comparing the
10501 second standard conversion sequence of two user conversion
10505 ics2
= next_conversion (ics2
);
10507 while (ics2
->kind
== ck_rvalue
10508 || ics2
->kind
== ck_lvalue
)
10509 ics2
= next_conversion (ics2
);
10511 if (ics2
->kind
== ics1
->kind
10512 && same_type_p (ics2
->type
, ics1
->type
)
10513 && (ics1
->kind
== ck_identity
10514 || same_type_p (next_conversion (ics2
)->type
,
10515 next_conversion (ics1
)->type
)))
10520 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
10521 be any _TYPE nodes. */
10524 is_properly_derived_from (tree derived
, tree base
)
10526 if (!CLASS_TYPE_P (derived
) || !CLASS_TYPE_P (base
))
10529 /* We only allow proper derivation here. The DERIVED_FROM_P macro
10530 considers every class derived from itself. */
10531 return (!same_type_ignoring_top_level_qualifiers_p (derived
, base
)
10532 && DERIVED_FROM_P (base
, derived
));
10535 /* We build the ICS for an implicit object parameter as a pointer
10536 conversion sequence. However, such a sequence should be compared
10537 as if it were a reference conversion sequence. If ICS is the
10538 implicit conversion sequence for an implicit object parameter,
10539 modify it accordingly. */
10542 maybe_handle_implicit_object (conversion
**ics
)
10544 if ((*ics
)->this_p
)
10546 /* [over.match.funcs]
10548 For non-static member functions, the type of the
10549 implicit object parameter is "reference to cv X"
10550 where X is the class of which the function is a
10551 member and cv is the cv-qualification on the member
10552 function declaration. */
10553 conversion
*t
= *ics
;
10554 tree reference_type
;
10556 /* The `this' parameter is a pointer to a class type. Make the
10557 implicit conversion talk about a reference to that same class
10559 reference_type
= TREE_TYPE (t
->type
);
10560 reference_type
= build_reference_type (reference_type
);
10562 if (t
->kind
== ck_qual
)
10563 t
= next_conversion (t
);
10564 if (t
->kind
== ck_ptr
)
10565 t
= next_conversion (t
);
10566 t
= build_identity_conv (TREE_TYPE (t
->type
), NULL_TREE
);
10567 t
= direct_reference_binding (reference_type
, t
);
10569 t
->rvaluedness_matches_p
= 0;
10574 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
10575 and return the initial reference binding conversion. Otherwise,
10576 leave *ICS unchanged and return NULL. */
10578 static conversion
*
10579 maybe_handle_ref_bind (conversion
**ics
)
10581 if ((*ics
)->kind
== ck_ref_bind
)
10583 conversion
*old_ics
= *ics
;
10584 *ics
= next_conversion (old_ics
);
10585 (*ics
)->user_conv_p
= old_ics
->user_conv_p
;
10592 /* Get the expression at the beginning of the conversion chain C. */
10595 conv_get_original_expr (conversion
*c
)
10597 for (; c
; c
= next_conversion (c
))
10598 if (c
->kind
== ck_identity
|| c
->kind
== ck_ambig
|| c
->kind
== ck_aggr
)
10603 /* Return a tree representing the number of elements initialized by the
10604 list-initialization C. The caller must check that C converts to an
10608 nelts_initialized_by_list_init (conversion
*c
)
10610 /* If the array we're converting to has a dimension, we'll use that. */
10611 if (TYPE_DOMAIN (c
->type
))
10612 return array_type_nelts_top (c
->type
);
10615 /* Otherwise, we look at how many elements the constructor we're
10616 initializing from has. */
10617 tree ctor
= conv_get_original_expr (c
);
10618 return size_int (CONSTRUCTOR_NELTS (ctor
));
10622 /* True iff C is a conversion that binds a reference or a pointer to
10623 an array of unknown bound. */
10626 conv_binds_to_array_of_unknown_bound (conversion
*c
)
10628 /* ck_ref_bind won't have the reference stripped. */
10629 tree type
= non_reference (c
->type
);
10630 /* ck_qual won't have the pointer stripped. */
10631 type
= strip_pointer_operator (type
);
10632 return (TREE_CODE (type
) == ARRAY_TYPE
10633 && TYPE_DOMAIN (type
) == NULL_TREE
);
10636 /* Compare two implicit conversion sequences according to the rules set out in
10637 [over.ics.rank]. Return values:
10639 1: ics1 is better than ics2
10640 -1: ics2 is better than ics1
10641 0: ics1 and ics2 are indistinguishable */
10644 compare_ics (conversion
*ics1
, conversion
*ics2
)
10650 tree deref_from_type1
= NULL_TREE
;
10651 tree deref_from_type2
= NULL_TREE
;
10652 tree deref_to_type1
= NULL_TREE
;
10653 tree deref_to_type2
= NULL_TREE
;
10654 conversion_rank rank1
, rank2
;
10656 /* REF_BINDING is nonzero if the result of the conversion sequence
10657 is a reference type. In that case REF_CONV is the reference
10658 binding conversion. */
10659 conversion
*ref_conv1
;
10660 conversion
*ref_conv2
;
10662 /* Compare badness before stripping the reference conversion. */
10663 if (ics1
->bad_p
> ics2
->bad_p
)
10665 else if (ics1
->bad_p
< ics2
->bad_p
)
10668 /* Handle implicit object parameters. */
10669 maybe_handle_implicit_object (&ics1
);
10670 maybe_handle_implicit_object (&ics2
);
10672 /* Handle reference parameters. */
10673 ref_conv1
= maybe_handle_ref_bind (&ics1
);
10674 ref_conv2
= maybe_handle_ref_bind (&ics2
);
10676 /* List-initialization sequence L1 is a better conversion sequence than
10677 list-initialization sequence L2 if L1 converts to
10678 std::initializer_list<X> for some X and L2 does not. */
10679 if (ics1
->kind
== ck_list
&& ics2
->kind
!= ck_list
)
10681 if (ics2
->kind
== ck_list
&& ics1
->kind
!= ck_list
)
10686 When comparing the basic forms of implicit conversion sequences (as
10687 defined in _over.best.ics_)
10689 --a standard conversion sequence (_over.ics.scs_) is a better
10690 conversion sequence than a user-defined conversion sequence
10691 or an ellipsis conversion sequence, and
10693 --a user-defined conversion sequence (_over.ics.user_) is a
10694 better conversion sequence than an ellipsis conversion sequence
10695 (_over.ics.ellipsis_). */
10696 /* Use BAD_CONVERSION_RANK because we already checked for a badness
10697 mismatch. If both ICS are bad, we try to make a decision based on
10698 what would have happened if they'd been good. This is not an
10699 extension, we'll still give an error when we build up the call; this
10700 just helps us give a more helpful error message. */
10701 rank1
= BAD_CONVERSION_RANK (ics1
);
10702 rank2
= BAD_CONVERSION_RANK (ics2
);
10706 else if (rank1
< rank2
)
10709 if (ics1
->ellipsis_p
)
10710 /* Both conversions are ellipsis conversions. */
10713 /* User-defined conversion sequence U1 is a better conversion sequence
10714 than another user-defined conversion sequence U2 if they contain the
10715 same user-defined conversion operator or constructor and if the sec-
10716 ond standard conversion sequence of U1 is better than the second
10717 standard conversion sequence of U2. */
10719 /* Handle list-conversion with the same code even though it isn't always
10720 ranked as a user-defined conversion and it doesn't have a second
10721 standard conversion sequence; it will still have the desired effect.
10722 Specifically, we need to do the reference binding comparison at the
10723 end of this function. */
10725 if (ics1
->user_conv_p
|| ics1
->kind
== ck_list
10726 || ics1
->kind
== ck_aggr
|| ics2
->kind
== ck_aggr
)
10728 conversion
*t1
= strip_standard_conversion (ics1
);
10729 conversion
*t2
= strip_standard_conversion (ics2
);
10731 if (!t1
|| !t2
|| t1
->kind
!= t2
->kind
)
10733 else if (t1
->kind
== ck_user
)
10735 tree f1
= t1
->cand
? t1
->cand
->fn
: t1
->type
;
10736 tree f2
= t2
->cand
? t2
->cand
->fn
: t2
->type
;
10740 /* List-initialization sequence L1 is a better conversion sequence than
10741 list-initialization sequence L2 if
10743 -- L1 and L2 convert to arrays of the same element type, and either
10744 the number of elements n1 initialized by L1 is less than the number
10745 of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
10746 of unknown bound and L1 does not. (Added in CWG 1307 and extended by
10748 else if (t1
->kind
== ck_aggr
10749 && TREE_CODE (t1
->type
) == ARRAY_TYPE
10750 && TREE_CODE (t2
->type
) == ARRAY_TYPE
)
10752 /* The type of the array elements must be the same. */
10753 if (!same_type_p (TREE_TYPE (t1
->type
), TREE_TYPE (t2
->type
)))
10756 tree n1
= nelts_initialized_by_list_init (t1
);
10757 tree n2
= nelts_initialized_by_list_init (t2
);
10758 if (tree_int_cst_lt (n1
, n2
))
10760 else if (tree_int_cst_lt (n2
, n1
))
10762 /* The n1 == n2 case. */
10763 bool c1
= conv_binds_to_array_of_unknown_bound (t1
);
10764 bool c2
= conv_binds_to_array_of_unknown_bound (t2
);
10767 else if (!c1
&& c2
)
10774 /* For ambiguous or aggregate conversions, use the target type as
10775 a proxy for the conversion function. */
10776 if (!same_type_ignoring_top_level_qualifiers_p (t1
->type
, t2
->type
))
10780 /* We can just fall through here, after setting up
10781 FROM_TYPE1 and FROM_TYPE2. */
10782 from_type1
= t1
->type
;
10783 from_type2
= t2
->type
;
10790 /* We're dealing with two standard conversion sequences.
10794 Standard conversion sequence S1 is a better conversion
10795 sequence than standard conversion sequence S2 if
10797 --S1 is a proper subsequence of S2 (comparing the conversion
10798 sequences in the canonical form defined by _over.ics.scs_,
10799 excluding any Lvalue Transformation; the identity
10800 conversion sequence is considered to be a subsequence of
10801 any non-identity conversion sequence */
10804 while (t1
->kind
!= ck_identity
)
10805 t1
= next_conversion (t1
);
10806 from_type1
= t1
->type
;
10809 while (t2
->kind
!= ck_identity
)
10810 t2
= next_conversion (t2
);
10811 from_type2
= t2
->type
;
10814 /* One sequence can only be a subsequence of the other if they start with
10815 the same type. They can start with different types when comparing the
10816 second standard conversion sequence in two user-defined conversion
10818 if (same_type_p (from_type1
, from_type2
))
10820 if (is_subseq (ics1
, ics2
))
10822 if (is_subseq (ics2
, ics1
))
10830 --the rank of S1 is better than the rank of S2 (by the rules
10833 Standard conversion sequences are ordered by their ranks: an Exact
10834 Match is a better conversion than a Promotion, which is a better
10835 conversion than a Conversion.
10837 Two conversion sequences with the same rank are indistinguishable
10838 unless one of the following rules applies:
10840 --A conversion that does not a convert a pointer, pointer to member,
10841 or std::nullptr_t to bool is better than one that does.
10843 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
10844 so that we do not have to check it explicitly. */
10845 if (ics1
->rank
< ics2
->rank
)
10847 else if (ics2
->rank
< ics1
->rank
)
10850 to_type1
= ics1
->type
;
10851 to_type2
= ics2
->type
;
10853 /* A conversion from scalar arithmetic type to complex is worse than a
10854 conversion between scalar arithmetic types. */
10855 if (same_type_p (from_type1
, from_type2
)
10856 && ARITHMETIC_TYPE_P (from_type1
)
10857 && ARITHMETIC_TYPE_P (to_type1
)
10858 && ARITHMETIC_TYPE_P (to_type2
)
10859 && ((TREE_CODE (to_type1
) == COMPLEX_TYPE
)
10860 != (TREE_CODE (to_type2
) == COMPLEX_TYPE
)))
10862 if (TREE_CODE (to_type1
) == COMPLEX_TYPE
)
10868 if (TYPE_PTR_P (from_type1
)
10869 && TYPE_PTR_P (from_type2
)
10870 && TYPE_PTR_P (to_type1
)
10871 && TYPE_PTR_P (to_type2
))
10873 deref_from_type1
= TREE_TYPE (from_type1
);
10874 deref_from_type2
= TREE_TYPE (from_type2
);
10875 deref_to_type1
= TREE_TYPE (to_type1
);
10876 deref_to_type2
= TREE_TYPE (to_type2
);
10878 /* The rules for pointers to members A::* are just like the rules
10879 for pointers A*, except opposite: if B is derived from A then
10880 A::* converts to B::*, not vice versa. For that reason, we
10881 switch the from_ and to_ variables here. */
10882 else if ((TYPE_PTRDATAMEM_P (from_type1
) && TYPE_PTRDATAMEM_P (from_type2
)
10883 && TYPE_PTRDATAMEM_P (to_type1
) && TYPE_PTRDATAMEM_P (to_type2
))
10884 || (TYPE_PTRMEMFUNC_P (from_type1
)
10885 && TYPE_PTRMEMFUNC_P (from_type2
)
10886 && TYPE_PTRMEMFUNC_P (to_type1
)
10887 && TYPE_PTRMEMFUNC_P (to_type2
)))
10889 deref_to_type1
= TYPE_PTRMEM_CLASS_TYPE (from_type1
);
10890 deref_to_type2
= TYPE_PTRMEM_CLASS_TYPE (from_type2
);
10891 deref_from_type1
= TYPE_PTRMEM_CLASS_TYPE (to_type1
);
10892 deref_from_type2
= TYPE_PTRMEM_CLASS_TYPE (to_type2
);
10895 if (deref_from_type1
!= NULL_TREE
10896 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1
))
10897 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2
)))
10899 /* This was one of the pointer or pointer-like conversions.
10903 --If class B is derived directly or indirectly from class A,
10904 conversion of B* to A* is better than conversion of B* to
10905 void*, and conversion of A* to void* is better than
10906 conversion of B* to void*. */
10907 if (VOID_TYPE_P (deref_to_type1
)
10908 && VOID_TYPE_P (deref_to_type2
))
10910 if (is_properly_derived_from (deref_from_type1
,
10913 else if (is_properly_derived_from (deref_from_type2
,
10917 else if (VOID_TYPE_P (deref_to_type1
)
10918 || VOID_TYPE_P (deref_to_type2
))
10920 if (same_type_p (deref_from_type1
, deref_from_type2
))
10922 if (VOID_TYPE_P (deref_to_type2
))
10924 if (is_properly_derived_from (deref_from_type1
,
10928 /* We know that DEREF_TO_TYPE1 is `void' here. */
10929 else if (is_properly_derived_from (deref_from_type1
,
10934 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1
))
10935 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2
)))
10939 --If class B is derived directly or indirectly from class A
10940 and class C is derived directly or indirectly from B,
10942 --conversion of C* to B* is better than conversion of C* to
10945 --conversion of B* to A* is better than conversion of C* to
10947 if (same_type_p (deref_from_type1
, deref_from_type2
))
10949 if (is_properly_derived_from (deref_to_type1
,
10952 else if (is_properly_derived_from (deref_to_type2
,
10956 else if (same_type_p (deref_to_type1
, deref_to_type2
))
10958 if (is_properly_derived_from (deref_from_type2
,
10961 else if (is_properly_derived_from (deref_from_type1
,
10967 else if (CLASS_TYPE_P (non_reference (from_type1
))
10968 && same_type_p (from_type1
, from_type2
))
10970 tree from
= non_reference (from_type1
);
10974 --binding of an expression of type C to a reference of type
10975 B& is better than binding an expression of type C to a
10976 reference of type A&
10978 --conversion of C to B is better than conversion of C to A, */
10979 if (is_properly_derived_from (from
, to_type1
)
10980 && is_properly_derived_from (from
, to_type2
))
10982 if (is_properly_derived_from (to_type1
, to_type2
))
10984 else if (is_properly_derived_from (to_type2
, to_type1
))
10988 else if (CLASS_TYPE_P (non_reference (to_type1
))
10989 && same_type_p (to_type1
, to_type2
))
10991 tree to
= non_reference (to_type1
);
10995 --binding of an expression of type B to a reference of type
10996 A& is better than binding an expression of type C to a
10997 reference of type A&,
10999 --conversion of B to A is better than conversion of C to A */
11000 if (is_properly_derived_from (from_type1
, to
)
11001 && is_properly_derived_from (from_type2
, to
))
11003 if (is_properly_derived_from (from_type2
, from_type1
))
11005 else if (is_properly_derived_from (from_type1
, from_type2
))
11012 --S1 and S2 differ only in their qualification conversion and yield
11013 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
11014 qualification signature of type T1 is a proper subset of the cv-
11015 qualification signature of type T2 */
11016 if (ics1
->kind
== ck_qual
11017 && ics2
->kind
== ck_qual
11018 && same_type_p (from_type1
, from_type2
))
11020 int result
= comp_cv_qual_signature (to_type1
, to_type2
);
11027 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
11028 to an implicit object parameter of a non-static member function
11029 declared without a ref-qualifier, and either S1 binds an lvalue
11030 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
11031 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
11032 draft standard, 13.3.3.2)
11034 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
11035 types to which the references refer are the same type except for
11036 top-level cv-qualifiers, and the type to which the reference
11037 initialized by S2 refers is more cv-qualified than the type to
11038 which the reference initialized by S1 refers.
11040 DR 1328 [over.match.best]: the context is an initialization by
11041 conversion function for direct reference binding (13.3.1.6) of a
11042 reference to function type, the return type of F1 is the same kind of
11043 reference (i.e. lvalue or rvalue) as the reference being initialized,
11044 and the return type of F2 is not. */
11046 if (ref_conv1
&& ref_conv2
)
11048 if (!ref_conv1
->this_p
&& !ref_conv2
->this_p
11049 && (ref_conv1
->rvaluedness_matches_p
11050 != ref_conv2
->rvaluedness_matches_p
)
11051 && (same_type_p (ref_conv1
->type
, ref_conv2
->type
)
11052 || (TYPE_REF_IS_RVALUE (ref_conv1
->type
)
11053 != TYPE_REF_IS_RVALUE (ref_conv2
->type
))))
11055 if (ref_conv1
->bad_p
11056 && !same_type_p (TREE_TYPE (ref_conv1
->type
),
11057 TREE_TYPE (ref_conv2
->type
)))
11058 /* Don't prefer a bad conversion that drops cv-quals to a bad
11059 conversion with the wrong rvalueness. */
11061 return (ref_conv1
->rvaluedness_matches_p
11062 - ref_conv2
->rvaluedness_matches_p
);
11065 if (same_type_ignoring_top_level_qualifiers_p (to_type1
, to_type2
))
11069 void f (int(&)[]), // (1)
11070 f (int(&)[1]), // (2)
11073 (2) is better than (1), but (3) should be equal to (1) and to
11074 (2). For that reason we don't use ck_qual for (1) which would
11075 give it the cr_exact rank while (3) remains ck_identity.
11076 Therefore we compare (1) and (2) here. For (1) we'll have
11078 ck_ref_bind <- ck_identity
11081 so to handle this we must look at ref_conv. */
11082 bool c1
= conv_binds_to_array_of_unknown_bound (ref_conv1
);
11083 bool c2
= conv_binds_to_array_of_unknown_bound (ref_conv2
);
11086 else if (!c1
&& c2
)
11089 int q1
= cp_type_quals (TREE_TYPE (ref_conv1
->type
));
11090 int q2
= cp_type_quals (TREE_TYPE (ref_conv2
->type
));
11091 if (ref_conv1
->bad_p
)
11093 /* Prefer the one that drops fewer cv-quals. */
11094 tree ftype
= next_conversion (ref_conv1
)->type
;
11095 int fquals
= cp_type_quals (ftype
);
11099 return comp_cv_qualification (q2
, q1
);
11106 -- A conversion that promotes an enumeration whose underlying type
11107 is fixed to its underlying type is better than one that promotes to
11108 the promoted underlying type, if the two are different. */
11109 if (ics1
->rank
== cr_promotion
11110 && ics2
->rank
== cr_promotion
11111 && UNSCOPED_ENUM_P (from_type1
)
11112 && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1
)
11113 && same_type_p (from_type1
, from_type2
))
11115 tree utype
= ENUM_UNDERLYING_TYPE (from_type1
);
11116 tree prom
= type_promotes_to (from_type1
);
11117 if (!same_type_p (utype
, prom
))
11119 if (same_type_p (to_type1
, utype
)
11120 && same_type_p (to_type2
, prom
))
11122 else if (same_type_p (to_type2
, utype
)
11123 && same_type_p (to_type1
, prom
))
11128 /* Neither conversion sequence is better than the other. */
11132 /* The source type for this standard conversion sequence. */
11135 source_type (conversion
*t
)
11137 return strip_standard_conversion (t
)->type
;
11140 /* Note a warning about preferring WINNER to LOSER. We do this by storing
11141 a pointer to LOSER and re-running joust to produce the warning if WINNER
11142 is actually used. */
11145 add_warning (struct z_candidate
*winner
, struct z_candidate
*loser
)
11147 candidate_warning
*cw
= (candidate_warning
*)
11148 conversion_obstack_alloc (sizeof (candidate_warning
));
11150 cw
->next
= winner
->warnings
;
11151 winner
->warnings
= cw
;
11154 /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
11155 prvalue returned from a conversion function, replace CAND with the candidate
11156 for the conversion and return true. Otherwise, return false. */
11159 joust_maybe_elide_copy (z_candidate
*&cand
)
11161 tree fn
= cand
->fn
;
11162 if (!DECL_COPY_CONSTRUCTOR_P (fn
) && !DECL_MOVE_CONSTRUCTOR_P (fn
))
11164 conversion
*conv
= cand
->convs
[0];
11165 gcc_checking_assert (conv
->kind
== ck_ref_bind
);
11166 conv
= next_conversion (conv
);
11167 if (conv
->kind
== ck_user
&& !TYPE_REF_P (conv
->type
))
11169 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
11170 (conv
->type
, DECL_CONTEXT (fn
)));
11171 z_candidate
*uc
= conv
->cand
;
11172 if (DECL_CONV_FN_P (uc
->fn
))
11181 /* True if the defining declarations of the two candidates have equivalent
11185 cand_parms_match (z_candidate
*c1
, z_candidate
*c2
)
11191 if (identifier_p (fn1
) || identifier_p (fn2
))
11193 /* We don't look at c1->template_decl because that's only set for primary
11194 templates, not e.g. non-template member functions of class templates. */
11195 tree t1
= most_general_template (fn1
);
11196 tree t2
= most_general_template (fn2
);
11203 fn1
= DECL_TEMPLATE_RESULT (t1
);
11204 fn2
= DECL_TEMPLATE_RESULT (t2
);
11206 return compparms (TYPE_ARG_TYPES (TREE_TYPE (fn1
)),
11207 TYPE_ARG_TYPES (TREE_TYPE (fn2
)));
11210 /* Compare two candidates for overloading as described in
11211 [over.match.best]. Return values:
11213 1: cand1 is better than cand2
11214 -1: cand2 is better than cand1
11215 0: cand1 and cand2 are indistinguishable */
11218 joust (struct z_candidate
*cand1
, struct z_candidate
*cand2
, bool warn
,
11219 tsubst_flags_t complain
)
11222 int off1
= 0, off2
= 0;
11226 /* Candidates that involve bad conversions are always worse than those
11228 if (cand1
->viable
> cand2
->viable
)
11230 if (cand1
->viable
< cand2
->viable
)
11233 /* If we have two pseudo-candidates for conversions to the same type,
11234 or two candidates for the same function, arbitrarily pick one. */
11235 if (cand1
->fn
== cand2
->fn
11236 && cand1
->reversed () == cand2
->reversed ()
11237 && (IS_TYPE_OR_DECL_P (cand1
->fn
)))
11240 /* Prefer a non-deleted function over an implicitly deleted move
11241 constructor or assignment operator. This differs slightly from the
11242 wording for issue 1402 (which says the move op is ignored by overload
11243 resolution), but this way produces better error messages. */
11244 if (TREE_CODE (cand1
->fn
) == FUNCTION_DECL
11245 && TREE_CODE (cand2
->fn
) == FUNCTION_DECL
11246 && DECL_DELETED_FN (cand1
->fn
) != DECL_DELETED_FN (cand2
->fn
))
11248 if (DECL_DELETED_FN (cand1
->fn
) && DECL_DEFAULTED_FN (cand1
->fn
)
11249 && move_fn_p (cand1
->fn
))
11251 if (DECL_DELETED_FN (cand2
->fn
) && DECL_DEFAULTED_FN (cand2
->fn
)
11252 && move_fn_p (cand2
->fn
))
11256 /* a viable function F1
11257 is defined to be a better function than another viable function F2 if
11258 for all arguments i, ICSi(F1) is not a worse conversion sequence than
11259 ICSi(F2), and then */
11261 /* for some argument j, ICSj(F1) is a better conversion sequence than
11264 /* For comparing static and non-static member functions, we ignore
11265 the implicit object parameter of the non-static function. The
11266 standard says to pretend that the static function has an object
11267 parm, but that won't work with operator overloading. */
11268 len
= cand1
->num_convs
;
11269 if (len
!= cand2
->num_convs
)
11271 int static_1
= DECL_STATIC_FUNCTION_P (cand1
->fn
);
11272 int static_2
= DECL_STATIC_FUNCTION_P (cand2
->fn
);
11274 if (DECL_CONSTRUCTOR_P (cand1
->fn
)
11275 && is_list_ctor (cand1
->fn
) != is_list_ctor (cand2
->fn
))
11276 /* We're comparing a near-match list constructor and a near-match
11277 non-list constructor. Just treat them as unordered. */
11280 gcc_assert (static_1
!= static_2
);
11291 /* Handle C++17 copy elision in [over.match.ctor] (direct-init) context. The
11292 standard currently says that only constructors are candidates, but if one
11293 copies a prvalue returned by a conversion function we want to treat the
11294 conversion as the candidate instead.
11296 Clang does something similar, as discussed at
11297 http://lists.isocpp.org/core/2017/10/3166.php
11298 http://lists.isocpp.org/core/2019/03/5721.php */
11299 int elided_tiebreaker
= 0;
11300 if (len
== 1 && cxx_dialect
>= cxx17
11301 && DECL_P (cand1
->fn
)
11302 && DECL_COMPLETE_CONSTRUCTOR_P (cand1
->fn
)
11303 && !(cand1
->flags
& LOOKUP_ONLYCONVERTING
))
11305 bool elided1
= joust_maybe_elide_copy (cand1
);
11306 bool elided2
= joust_maybe_elide_copy (cand2
);
11307 /* As a tiebreaker below we will prefer a constructor to a conversion
11308 operator exposed this way. */
11309 elided_tiebreaker
= elided2
- elided1
;
11312 for (i
= 0; i
< len
; ++i
)
11314 conversion
*t1
= cand1
->convs
[i
+ off1
];
11315 conversion
*t2
= cand2
->convs
[i
+ off2
];
11316 int comp
= compare_ics (t1
, t2
);
11320 if ((complain
& tf_warning
)
11322 && (CONVERSION_RANK (t1
) + CONVERSION_RANK (t2
)
11323 == cr_std
+ cr_promotion
)
11324 && t1
->kind
== ck_std
11325 && t2
->kind
== ck_std
11326 && TREE_CODE (t1
->type
) == INTEGER_TYPE
11327 && TREE_CODE (t2
->type
) == INTEGER_TYPE
11328 && (TYPE_PRECISION (t1
->type
)
11329 == TYPE_PRECISION (t2
->type
))
11330 && (TYPE_UNSIGNED (next_conversion (t1
)->type
)
11331 || (TREE_CODE (next_conversion (t1
)->type
)
11332 == ENUMERAL_TYPE
)))
11334 tree type
= next_conversion (t1
)->type
;
11336 struct z_candidate
*w
, *l
;
11338 type1
= t1
->type
, type2
= t2
->type
,
11339 w
= cand1
, l
= cand2
;
11341 type1
= t2
->type
, type2
= t1
->type
,
11342 w
= cand2
, l
= cand1
;
11346 warning (OPT_Wsign_promo
, "passing %qT chooses %qT over %qT",
11347 type
, type1
, type2
);
11348 warning (OPT_Wsign_promo
, " in call to %qD", w
->fn
);
11351 add_warning (w
, l
);
11354 if (winner
&& comp
!= winner
)
11356 /* Ambiguity between normal and reversed comparison operators
11357 with the same parameter types; prefer the normal one. */
11358 if ((cand1
->reversed () != cand2
->reversed ())
11359 && cand_parms_match (cand1
, cand2
))
11360 return cand1
->reversed () ? -1 : 1;
11369 /* warn about confusing overload resolution for user-defined conversions,
11370 either between a constructor and a conversion op, or between two
11372 if ((complain
& tf_warning
)
11373 /* In C++17, the constructor might have been elided, which means that
11374 an originally null ->second_conv could become non-null. */
11375 && winner
&& warn_conversion
&& cand1
->second_conv
&& cand2
->second_conv
11376 && (!DECL_CONSTRUCTOR_P (cand1
->fn
) || !DECL_CONSTRUCTOR_P (cand2
->fn
))
11377 && winner
!= compare_ics (cand1
->second_conv
, cand2
->second_conv
))
11379 struct z_candidate
*w
, *l
;
11380 bool give_warning
= false;
11383 w
= cand1
, l
= cand2
;
11385 w
= cand2
, l
= cand1
;
11387 /* We don't want to complain about `X::operator T1 ()'
11388 beating `X::operator T2 () const', when T2 is a no less
11389 cv-qualified version of T1. */
11390 if (DECL_CONTEXT (w
->fn
) == DECL_CONTEXT (l
->fn
)
11391 && !DECL_CONSTRUCTOR_P (w
->fn
) && !DECL_CONSTRUCTOR_P (l
->fn
))
11393 tree t
= TREE_TYPE (TREE_TYPE (l
->fn
));
11394 tree f
= TREE_TYPE (TREE_TYPE (w
->fn
));
11396 if (TREE_CODE (t
) == TREE_CODE (f
) && INDIRECT_TYPE_P (t
))
11401 if (!comp_ptr_ttypes (t
, f
))
11402 give_warning
= true;
11405 give_warning
= true;
11411 tree source
= source_type (w
->convs
[0]);
11412 if (INDIRECT_TYPE_P (source
))
11413 source
= TREE_TYPE (source
);
11414 auto_diagnostic_group d
;
11415 if (warning (OPT_Wconversion
, "choosing %qD over %qD", w
->fn
, l
->fn
)
11416 && warning (OPT_Wconversion
, " for conversion from %qH to %qI",
11417 source
, w
->second_conv
->type
))
11419 inform (input_location
, " because conversion sequence "
11420 "for the argument is better");
11424 add_warning (w
, l
);
11430 /* Put this tiebreaker first, so that we don't try to look at second_conv of
11431 a constructor candidate that doesn't have one. */
11432 if (elided_tiebreaker
)
11433 return elided_tiebreaker
;
11435 /* DR 495 moved this tiebreaker above the template ones. */
11436 /* or, if not that,
11437 the context is an initialization by user-defined conversion (see
11438 _dcl.init_ and _over.match.user_) and the standard conversion
11439 sequence from the return type of F1 to the destination type (i.e.,
11440 the type of the entity being initialized) is a better conversion
11441 sequence than the standard conversion sequence from the return type
11442 of F2 to the destination type. */
11444 if (cand1
->second_conv
)
11446 winner
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
11451 /* or, if not that,
11452 F1 is a non-template function and F2 is a template function
11455 if (!cand1
->template_decl
&& cand2
->template_decl
)
11457 else if (cand1
->template_decl
&& !cand2
->template_decl
)
11460 /* or, if not that,
11461 F1 and F2 are template functions and the function template for F1 is
11462 more specialized than the template for F2 according to the partial
11465 if (cand1
->template_decl
&& cand2
->template_decl
)
11467 winner
= more_specialized_fn
11468 (TI_TEMPLATE (cand1
->template_decl
),
11469 TI_TEMPLATE (cand2
->template_decl
),
11470 /* [temp.func.order]: The presence of unused ellipsis and default
11471 arguments has no effect on the partial ordering of function
11472 templates. add_function_candidate() will not have
11473 counted the "this" argument for constructors. */
11474 cand1
->num_convs
+ DECL_CONSTRUCTOR_P (cand1
->fn
));
11479 /* Concepts: F1 and F2 are non-template functions with the same
11480 parameter-type-lists, and F1 is more constrained than F2 according to the
11481 partial ordering of constraints described in 13.5.4. */
11483 if (flag_concepts
&& DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
11484 && !cand1
->template_decl
&& !cand2
->template_decl
11485 && cand_parms_match (cand1
, cand2
))
11487 winner
= more_constrained (cand1
->fn
, cand2
->fn
);
11492 /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
11493 rewritten candidates, and F2 is a synthesized candidate with reversed
11494 order of parameters and F1 is not. */
11495 if (cand1
->rewritten ())
11497 if (!cand2
->rewritten ())
11499 if (!cand1
->reversed () && cand2
->reversed ())
11501 if (cand1
->reversed () && !cand2
->reversed ())
11504 else if (cand2
->rewritten ())
11507 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
11508 if (deduction_guide_p (cand1
->fn
))
11510 gcc_assert (deduction_guide_p (cand2
->fn
));
11511 /* We distinguish between candidates from an explicit deduction guide and
11512 candidates built from a constructor based on DECL_ARTIFICIAL. */
11513 int art1
= DECL_ARTIFICIAL (cand1
->fn
);
11514 int art2
= DECL_ARTIFICIAL (cand2
->fn
);
11516 return art2
- art1
;
11520 /* Prefer the special copy guide over a declared copy/move
11522 if (copy_guide_p (cand1
->fn
))
11524 if (copy_guide_p (cand2
->fn
))
11527 /* Prefer a candidate generated from a non-template constructor. */
11528 int tg1
= template_guide_p (cand1
->fn
);
11529 int tg2
= template_guide_p (cand2
->fn
);
11535 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
11536 for all arguments the corresponding parameters of F1 and F2 have the same
11537 type (CWG 2273/2277). */
11538 if (DECL_P (cand1
->fn
) && DECL_CLASS_SCOPE_P (cand1
->fn
)
11539 && !DECL_CONV_FN_P (cand1
->fn
)
11540 && DECL_P (cand2
->fn
) && DECL_CLASS_SCOPE_P (cand2
->fn
)
11541 && !DECL_CONV_FN_P (cand2
->fn
))
11543 tree base1
= DECL_CONTEXT (strip_inheriting_ctors (cand1
->fn
));
11544 tree base2
= DECL_CONTEXT (strip_inheriting_ctors (cand2
->fn
));
11546 bool used1
= false;
11547 bool used2
= false;
11548 if (base1
== base2
)
11549 /* No difference. */;
11550 else if (DERIVED_FROM_P (base1
, base2
))
11552 else if (DERIVED_FROM_P (base2
, base1
))
11555 if (int diff
= used2
- used1
)
11557 for (i
= 0; i
< len
; ++i
)
11559 conversion
*t1
= cand1
->convs
[i
+ off1
];
11560 conversion
*t2
= cand2
->convs
[i
+ off2
];
11561 if (!same_type_p (t1
->type
, t2
->type
))
11569 /* Check whether we can discard a builtin candidate, either because we
11570 have two identical ones or matching builtin and non-builtin candidates.
11572 (Pedantically in the latter case the builtin which matched the user
11573 function should not be added to the overload set, but we spot it here.
11576 ... the builtin candidates include ...
11577 - do not have the same parameter type list as any non-template
11578 non-member candidate. */
11580 if (identifier_p (cand1
->fn
) || identifier_p (cand2
->fn
))
11582 for (i
= 0; i
< len
; ++i
)
11583 if (!same_type_p (cand1
->convs
[i
]->type
,
11584 cand2
->convs
[i
]->type
))
11586 if (i
== cand1
->num_convs
)
11588 if (cand1
->fn
== cand2
->fn
)
11589 /* Two built-in candidates; arbitrarily pick one. */
11591 else if (identifier_p (cand1
->fn
))
11592 /* cand1 is built-in; prefer cand2. */
11595 /* cand2 is built-in; prefer cand1. */
11600 /* For candidates of a multi-versioned function, make the version with
11601 the highest priority win. This version will be checked for dispatching
11602 first. If this version can be inlined into the caller, the front-end
11603 will simply make a direct call to this function. */
11605 if (TREE_CODE (cand1
->fn
) == FUNCTION_DECL
11606 && DECL_FUNCTION_VERSIONED (cand1
->fn
)
11607 && TREE_CODE (cand2
->fn
) == FUNCTION_DECL
11608 && DECL_FUNCTION_VERSIONED (cand2
->fn
))
11610 tree f1
= TREE_TYPE (cand1
->fn
);
11611 tree f2
= TREE_TYPE (cand2
->fn
);
11612 tree p1
= TYPE_ARG_TYPES (f1
);
11613 tree p2
= TYPE_ARG_TYPES (f2
);
11615 /* Check if cand1->fn and cand2->fn are versions of the same function. It
11616 is possible that cand1->fn and cand2->fn are function versions but of
11617 different functions. Check types to see if they are versions of the same
11619 if (compparms (p1
, p2
)
11620 && same_type_p (TREE_TYPE (f1
), TREE_TYPE (f2
)))
11622 /* Always make the version with the higher priority, more
11623 specialized, win. */
11624 gcc_assert (targetm
.compare_version_priority
);
11625 if (targetm
.compare_version_priority (cand1
->fn
, cand2
->fn
) >= 0)
11632 /* If the two function declarations represent the same function (this can
11633 happen with declarations in multiple scopes and arg-dependent lookup),
11634 arbitrarily choose one. But first make sure the default args we're
11636 if (DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
11637 && equal_functions (cand1
->fn
, cand2
->fn
))
11639 tree parms1
= TYPE_ARG_TYPES (TREE_TYPE (cand1
->fn
));
11640 tree parms2
= TYPE_ARG_TYPES (TREE_TYPE (cand2
->fn
));
11642 gcc_assert (!DECL_CONSTRUCTOR_P (cand1
->fn
));
11644 for (i
= 0; i
< len
; ++i
)
11646 /* Don't crash if the fn is variadic. */
11649 parms1
= TREE_CHAIN (parms1
);
11650 parms2
= TREE_CHAIN (parms2
);
11654 parms1
= TREE_CHAIN (parms1
);
11656 parms2
= TREE_CHAIN (parms2
);
11658 for (; parms1
; ++i
)
11660 if (!cp_tree_equal (TREE_PURPOSE (parms1
),
11661 TREE_PURPOSE (parms2
)))
11665 if (complain
& tf_error
)
11667 auto_diagnostic_group d
;
11668 if (permerror (input_location
,
11669 "default argument mismatch in "
11670 "overload resolution"))
11672 inform (DECL_SOURCE_LOCATION (cand1
->fn
),
11673 " candidate 1: %q#F", cand1
->fn
);
11674 inform (DECL_SOURCE_LOCATION (cand2
->fn
),
11675 " candidate 2: %q#F", cand2
->fn
);
11682 add_warning (cand1
, cand2
);
11685 parms1
= TREE_CHAIN (parms1
);
11686 parms2
= TREE_CHAIN (parms2
);
11694 /* Extension: If the worst conversion for one candidate is better than the
11695 worst conversion for the other, take the first. */
11696 if (!pedantic
&& (complain
& tf_warning_or_error
))
11698 conversion_rank rank1
= cr_identity
, rank2
= cr_identity
;
11699 struct z_candidate
*w
= 0, *l
= 0;
11701 for (i
= 0; i
< len
; ++i
)
11703 if (CONVERSION_RANK (cand1
->convs
[i
+off1
]) > rank1
)
11704 rank1
= CONVERSION_RANK (cand1
->convs
[i
+off1
]);
11705 if (CONVERSION_RANK (cand2
->convs
[i
+ off2
]) > rank2
)
11706 rank2
= CONVERSION_RANK (cand2
->convs
[i
+ off2
]);
11709 winner
= 1, w
= cand1
, l
= cand2
;
11711 winner
= -1, w
= cand2
, l
= cand1
;
11714 /* Don't choose a deleted function over ambiguity. */
11715 if (DECL_P (w
->fn
) && DECL_DELETED_FN (w
->fn
))
11719 auto_diagnostic_group d
;
11720 if (pedwarn (input_location
, 0,
11721 "ISO C++ says that these are ambiguous, even "
11722 "though the worst conversion for the first is "
11723 "better than the worst conversion for the second:"))
11725 print_z_candidate (input_location
, N_("candidate 1:"), w
);
11726 print_z_candidate (input_location
, N_("candidate 2:"), l
);
11730 add_warning (w
, l
);
11735 gcc_assert (!winner
);
11739 /* Given a list of candidates for overloading, find the best one, if any.
11740 This algorithm has a worst case of O(2n) (winner is last), and a best
11741 case of O(n/2) (totally ambiguous); much better than a sorting
11744 static struct z_candidate
*
11745 tourney (struct z_candidate
*candidates
, tsubst_flags_t complain
)
11747 struct z_candidate
*champ
= candidates
, *challenger
;
11749 int champ_compared_to_predecessor
= 0;
11751 /* Walk through the list once, comparing each current champ to the next
11752 candidate, knocking out a candidate or two with each comparison. */
11754 for (challenger
= champ
->next
; challenger
; )
11756 fate
= joust (champ
, challenger
, 0, complain
);
11758 challenger
= challenger
->next
;
11763 champ
= challenger
->next
;
11766 champ_compared_to_predecessor
= 0;
11770 champ
= challenger
;
11771 champ_compared_to_predecessor
= 1;
11774 challenger
= champ
->next
;
11778 /* Make sure the champ is better than all the candidates it hasn't yet
11779 been compared to. */
11781 for (challenger
= candidates
;
11782 challenger
!= champ
11783 && !(champ_compared_to_predecessor
&& challenger
->next
== champ
);
11784 challenger
= challenger
->next
)
11786 fate
= joust (champ
, challenger
, 0, complain
);
11794 /* Returns nonzero if things of type FROM can be converted to TO. */
11797 can_convert (tree to
, tree from
, tsubst_flags_t complain
)
11799 tree arg
= NULL_TREE
;
11800 /* implicit_conversion only considers user-defined conversions
11801 if it has an expression for the call argument list. */
11802 if (CLASS_TYPE_P (from
) || CLASS_TYPE_P (to
))
11803 arg
= build1 (CAST_EXPR
, from
, NULL_TREE
);
11804 return can_convert_arg (to
, from
, arg
, LOOKUP_IMPLICIT
, complain
);
11807 /* Returns nonzero if things of type FROM can be converted to TO with a
11808 standard conversion. */
11811 can_convert_standard (tree to
, tree from
, tsubst_flags_t complain
)
11813 return can_convert_arg (to
, from
, NULL_TREE
, LOOKUP_IMPLICIT
, complain
);
11816 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
11819 can_convert_arg (tree to
, tree from
, tree arg
, int flags
,
11820 tsubst_flags_t complain
)
11826 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11827 p
= conversion_obstack_alloc (0);
11828 /* We want to discard any access checks done for this test,
11829 as we might not be in the appropriate access context and
11830 we'll do the check again when we actually perform the
11832 push_deferring_access_checks (dk_deferred
);
11834 t
= implicit_conversion (to
, from
, arg
, /*c_cast_p=*/false,
11836 ok_p
= (t
&& !t
->bad_p
);
11838 /* Discard the access checks now. */
11839 pop_deferring_access_checks ();
11840 /* Free all the conversions we allocated. */
11841 obstack_free (&conversion_obstack
, p
);
11846 /* Like can_convert_arg, but allows dubious conversions as well. */
11849 can_convert_arg_bad (tree to
, tree from
, tree arg
, int flags
,
11850 tsubst_flags_t complain
)
11855 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11856 p
= conversion_obstack_alloc (0);
11857 /* Try to perform the conversion. */
11858 t
= implicit_conversion (to
, from
, arg
, /*c_cast_p=*/false,
11860 /* Free all the conversions we allocated. */
11861 obstack_free (&conversion_obstack
, p
);
11866 /* Convert EXPR to TYPE. Return the converted expression.
11868 Note that we allow bad conversions here because by the time we get to
11869 this point we are committed to doing the conversion. If we end up
11870 doing a bad conversion, convert_like will complain. */
11873 perform_implicit_conversion_flags (tree type
, tree expr
,
11874 tsubst_flags_t complain
, int flags
)
11878 location_t loc
= cp_expr_loc_or_input_loc (expr
);
11880 if (TYPE_REF_P (type
))
11881 expr
= mark_lvalue_use (expr
);
11883 expr
= mark_rvalue_use (expr
);
11885 if (error_operand_p (expr
))
11886 return error_mark_node
;
11888 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11889 p
= conversion_obstack_alloc (0);
11891 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
11892 /*c_cast_p=*/false,
11897 if (complain
& tf_error
)
11898 implicit_conversion_error (loc
, type
, expr
);
11899 expr
= error_mark_node
;
11901 else if (processing_template_decl
&& conv
->kind
!= ck_identity
)
11903 /* In a template, we are only concerned about determining the
11904 type of non-dependent expressions, so we do not have to
11905 perform the actual conversion. But for initializers, we
11906 need to be able to perform it at instantiation
11907 (or instantiate_non_dependent_expr) time. */
11908 expr
= build1 (IMPLICIT_CONV_EXPR
, type
, expr
);
11909 if (!(flags
& LOOKUP_ONLYCONVERTING
))
11910 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr
) = true;
11911 if (flags
& LOOKUP_NO_NARROWING
)
11912 IMPLICIT_CONV_EXPR_BRACED_INIT (expr
) = true;
11915 expr
= convert_like (conv
, expr
, complain
);
11917 /* Free all the conversions we allocated. */
11918 obstack_free (&conversion_obstack
, p
);
11924 perform_implicit_conversion (tree type
, tree expr
, tsubst_flags_t complain
)
11926 return perform_implicit_conversion_flags (type
, expr
, complain
,
11930 /* Convert EXPR to TYPE (as a direct-initialization) if that is
11931 permitted. If the conversion is valid, the converted expression is
11932 returned. Otherwise, NULL_TREE is returned, except in the case
11933 that TYPE is a class type; in that case, an error is issued. If
11934 C_CAST_P is true, then this direct-initialization is taking
11935 place as part of a static_cast being attempted as part of a C-style
11939 perform_direct_initialization_if_possible (tree type
,
11942 tsubst_flags_t complain
)
11947 if (type
== error_mark_node
|| error_operand_p (expr
))
11948 return error_mark_node
;
11951 If the destination type is a (possibly cv-qualified) class type:
11953 -- If the initialization is direct-initialization ...,
11954 constructors are considered.
11956 -- If overload resolution is successful, the selected constructor
11957 is called to initialize the object, with the initializer expression
11958 or expression-list as its argument(s).
11960 -- Otherwise, if no constructor is viable, the destination type is
11961 a (possibly cv-qualified) aggregate class A, and the initializer is
11962 a parenthesized expression-list, the object is initialized as
11964 if (CLASS_TYPE_P (type
))
11966 releasing_vec
args (make_tree_vector_single (expr
));
11967 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
11968 &args
, type
, LOOKUP_NORMAL
, complain
);
11969 return build_cplus_new (type
, expr
, complain
);
11972 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11973 p
= conversion_obstack_alloc (0);
11975 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
11977 LOOKUP_NORMAL
, complain
);
11978 if (!conv
|| conv
->bad_p
)
11980 else if (processing_template_decl
&& conv
->kind
!= ck_identity
)
11982 /* In a template, we are only concerned about determining the
11983 type of non-dependent expressions, so we do not have to
11984 perform the actual conversion. But for initializers, we
11985 need to be able to perform it at instantiation
11986 (or instantiate_non_dependent_expr) time. */
11987 expr
= build1 (IMPLICIT_CONV_EXPR
, type
, expr
);
11988 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr
) = true;
11991 expr
= convert_like (conv
, expr
, NULL_TREE
, 0,
11992 /*issue_conversion_warnings=*/false,
11993 c_cast_p
, complain
);
11995 /* Free all the conversions we allocated. */
11996 obstack_free (&conversion_obstack
, p
);
12001 /* When initializing a reference that lasts longer than a full-expression,
12002 this special rule applies:
12006 The temporary to which the reference is bound or the temporary
12007 that is the complete object to which the reference is bound
12008 persists for the lifetime of the reference.
12010 The temporaries created during the evaluation of the expression
12011 initializing the reference, except the temporary to which the
12012 reference is bound, are destroyed at the end of the
12013 full-expression in which they are created.
12015 In that case, we store the converted expression into a new
12016 VAR_DECL in a new scope.
12018 However, we want to be careful not to create temporaries when
12019 they are not required. For example, given:
12022 struct D : public B {};
12026 there is no need to copy the return value from "f"; we can just
12027 extend its lifetime. Similarly, given:
12030 struct T { operator S(); };
12034 we can extend the lifetime of the return value of the conversion
12037 The next several functions are involved in this lifetime extension. */
12039 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
12040 reference is being bound to a temporary. Create and return a new
12041 VAR_DECL with the indicated TYPE; this variable will store the value to
12042 which the reference is bound. */
12045 make_temporary_var_for_ref_to_temp (tree decl
, tree type
)
12047 tree var
= create_temporary_var (type
);
12049 /* Register the variable. */
12051 && (TREE_STATIC (decl
) || CP_DECL_THREAD_LOCAL_P (decl
)))
12053 /* Namespace-scope or local static; give it a mangled name. */
12055 /* If an initializer is visible to multiple translation units, those
12056 translation units must agree on the addresses of the
12057 temporaries. Therefore the temporaries must be given a consistent name
12058 and vague linkage. The mangled name of a temporary is the name of the
12059 non-temporary object in whose initializer they appear, prefixed with
12060 GR and suffixed with a sequence number mangled using the usual rules
12061 for a seq-id. Temporaries are numbered with a pre-order, depth-first,
12062 left-to-right walk of the complete initializer. */
12063 copy_linkage (var
, decl
);
12065 tree name
= mangle_ref_init_variable (decl
);
12066 DECL_NAME (var
) = name
;
12067 SET_DECL_ASSEMBLER_NAME (var
, name
);
12070 /* Create a new cleanup level if necessary. */
12071 maybe_push_cleanup_level (type
);
12073 return pushdecl (var
);
12076 /* EXPR is the initializer for a variable DECL of reference or
12077 std::initializer_list type. Create, push and return a new VAR_DECL
12078 for the initializer so that it will live as long as DECL. Any
12079 cleanup for the new variable is returned through CLEANUP, and the
12080 code to initialize the new variable is returned through INITP. */
12083 set_up_extended_ref_temp (tree decl
, tree expr
, vec
<tree
, va_gc
> **cleanups
,
12084 tree
*initp
, tree
*cond_guard
)
12090 /* Create the temporary variable. */
12091 type
= TREE_TYPE (expr
);
12092 var
= make_temporary_var_for_ref_to_temp (decl
, type
);
12093 layout_decl (var
, 0);
12094 /* If the rvalue is the result of a function call it will be
12095 a TARGET_EXPR. If it is some other construct (such as a
12096 member access expression where the underlying object is
12097 itself the result of a function call), turn it into a
12098 TARGET_EXPR here. It is important that EXPR be a
12099 TARGET_EXPR below since otherwise the INIT_EXPR will
12100 attempt to make a bitwise copy of EXPR to initialize
12102 if (TREE_CODE (expr
) != TARGET_EXPR
)
12103 expr
= get_target_expr (expr
);
12105 if (TREE_CODE (decl
) == FIELD_DECL
12106 && extra_warnings
&& !TREE_NO_WARNING (decl
))
12108 warning (OPT_Wextra
, "a temporary bound to %qD only persists "
12109 "until the constructor exits", decl
);
12110 TREE_NO_WARNING (decl
) = true;
12113 /* Recursively extend temps in this initializer. */
12114 TARGET_EXPR_INITIAL (expr
)
12115 = extend_ref_init_temps (decl
, TARGET_EXPR_INITIAL (expr
), cleanups
,
12118 /* Any reference temp has a non-trivial initializer. */
12119 DECL_NONTRIVIALLY_INITIALIZED_P (var
) = true;
12121 /* If the initializer is constant, put it in DECL_INITIAL so we get
12122 static initialization and use in constant expressions. */
12123 init
= maybe_constant_init (expr
);
12124 /* As in store_init_value. */
12125 init
= cp_fully_fold (init
);
12126 if (TREE_CONSTANT (init
))
12128 if (literal_type_p (type
) && CP_TYPE_CONST_NON_VOLATILE_P (type
))
12130 /* 5.19 says that a constant expression can include an
12131 lvalue-rvalue conversion applied to "a glvalue of literal type
12132 that refers to a non-volatile temporary object initialized
12133 with a constant expression". Rather than try to communicate
12134 that this VAR_DECL is a temporary, just mark it constexpr. */
12135 DECL_DECLARED_CONSTEXPR_P (var
) = true;
12136 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var
) = true;
12137 TREE_CONSTANT (var
) = true;
12138 TREE_READONLY (var
) = true;
12140 DECL_INITIAL (var
) = init
;
12144 /* Create the INIT_EXPR that will initialize the temporary
12146 init
= split_nonconstant_init (var
, expr
);
12147 if (at_function_scope_p ())
12149 add_decl_expr (var
);
12151 if (TREE_STATIC (var
))
12152 init
= add_stmt_to_compound (init
, register_dtor_fn (var
));
12155 tree cleanup
= cxx_maybe_build_cleanup (var
, tf_warning_or_error
);
12158 if (cond_guard
&& cleanup
!= error_mark_node
)
12160 if (*cond_guard
== NULL_TREE
)
12162 *cond_guard
= build_local_temp (boolean_type_node
);
12163 add_decl_expr (*cond_guard
);
12164 tree set
= cp_build_modify_expr (UNKNOWN_LOCATION
,
12165 *cond_guard
, NOP_EXPR
,
12166 boolean_false_node
,
12167 tf_warning_or_error
);
12168 finish_expr_stmt (set
);
12170 cleanup
= build3 (COND_EXPR
, void_type_node
,
12171 *cond_guard
, cleanup
, NULL_TREE
);
12173 vec_safe_push (*cleanups
, cleanup
);
12177 /* We must be careful to destroy the temporary only
12178 after its initialization has taken place. If the
12179 initialization throws an exception, then the
12180 destructor should not be run. We cannot simply
12181 transform INIT into something like:
12183 (INIT, ({ CLEANUP_STMT; }))
12185 because emit_local_var always treats the
12186 initializer as a full-expression. Thus, the
12187 destructor would run too early; it would run at the
12188 end of initializing the reference variable, rather
12189 than at the end of the block enclosing the
12190 reference variable.
12192 The solution is to pass back a cleanup expression
12193 which the caller is responsible for attaching to
12194 the statement tree. */
12198 rest_of_decl_compilation (var
, /*toplev=*/1, at_eof
);
12199 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
12201 if (CP_DECL_THREAD_LOCAL_P (var
))
12202 tls_aggregates
= tree_cons (NULL_TREE
, var
,
12205 static_aggregates
= tree_cons (NULL_TREE
, var
,
12206 static_aggregates
);
12209 /* Check whether the dtor is callable. */
12210 cxx_maybe_build_cleanup (var
, tf_warning_or_error
);
12212 /* Avoid -Wunused-variable warning (c++/38958). */
12213 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
12215 TREE_USED (decl
) = DECL_READ_P (decl
) = true;
12221 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
12222 initializing a variable of that TYPE. */
12225 initialize_reference (tree type
, tree expr
,
12226 int flags
, tsubst_flags_t complain
)
12230 location_t loc
= cp_expr_loc_or_input_loc (expr
);
12232 if (type
== error_mark_node
|| error_operand_p (expr
))
12233 return error_mark_node
;
12235 /* Get the high-water mark for the CONVERSION_OBSTACK. */
12236 p
= conversion_obstack_alloc (0);
12238 conv
= reference_binding (type
, TREE_TYPE (expr
), expr
, /*c_cast_p=*/false,
12240 /* If this conversion failed, we're in C++20, and we have something like
12241 A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
12242 if ((!conv
|| conv
->bad_p
)
12243 && (flags
& LOOKUP_AGGREGATE_PAREN_INIT
))
12245 tree e
= build_constructor_single (init_list_type_node
, NULL_TREE
, expr
);
12246 CONSTRUCTOR_IS_DIRECT_INIT (e
) = true;
12247 CONSTRUCTOR_IS_PAREN_INIT (e
) = true;
12248 conversion
*c
= reference_binding (type
, TREE_TYPE (e
), e
,
12249 /*c_cast_p=*/false, flags
, complain
);
12250 /* If this worked, use it. */
12251 if (c
&& !c
->bad_p
)
12252 expr
= e
, conv
= c
;
12254 if (!conv
|| conv
->bad_p
)
12256 if (complain
& tf_error
)
12259 convert_like (conv
, expr
, complain
);
12260 else if (!CP_TYPE_CONST_P (TREE_TYPE (type
))
12261 && !TYPE_REF_IS_RVALUE (type
)
12262 && !lvalue_p (expr
))
12263 error_at (loc
, "invalid initialization of non-const reference of "
12264 "type %qH from an rvalue of type %qI",
12265 type
, TREE_TYPE (expr
));
12267 error_at (loc
, "invalid initialization of reference of type "
12268 "%qH from expression of type %qI", type
,
12271 return error_mark_node
;
12274 if (conv
->kind
== ck_ref_bind
)
12275 /* Perform the conversion. */
12276 expr
= convert_like (conv
, expr
, complain
);
12277 else if (conv
->kind
== ck_ambig
)
12278 /* We gave an error in build_user_type_conversion_1. */
12279 expr
= error_mark_node
;
12281 gcc_unreachable ();
12283 /* Free all the conversions we allocated. */
12284 obstack_free (&conversion_obstack
, p
);
12289 /* If *P is an xvalue expression, prevent temporary lifetime extension if it
12290 gets used to initialize a reference. */
12293 prevent_lifetime_extension (tree t
)
12296 while (TREE_CODE (*p
) == COMPOUND_EXPR
)
12297 p
= &TREE_OPERAND (*p
, 1);
12298 while (handled_component_p (*p
))
12299 p
= &TREE_OPERAND (*p
, 0);
12300 /* Change a TARGET_EXPR from prvalue to xvalue. */
12301 if (TREE_CODE (*p
) == TARGET_EXPR
)
12302 *p
= build2 (COMPOUND_EXPR
, TREE_TYPE (*p
), *p
,
12303 move (TARGET_EXPR_SLOT (*p
)));
12307 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
12308 which is bound either to a reference or a std::initializer_list. */
12311 extend_ref_init_temps_1 (tree decl
, tree init
, vec
<tree
, va_gc
> **cleanups
,
12317 if (TREE_CODE (sub
) == COMPOUND_EXPR
)
12319 TREE_OPERAND (sub
, 1)
12320 = extend_ref_init_temps_1 (decl
, TREE_OPERAND (sub
, 1), cleanups
,
12324 if (TREE_CODE (sub
) == COND_EXPR
)
12326 tree cur_cond_guard
= NULL_TREE
;
12327 if (TREE_OPERAND (sub
, 1))
12328 TREE_OPERAND (sub
, 1)
12329 = extend_ref_init_temps_1 (decl
, TREE_OPERAND (sub
, 1), cleanups
,
12331 if (cur_cond_guard
)
12333 tree set
= cp_build_modify_expr (UNKNOWN_LOCATION
, cur_cond_guard
,
12334 NOP_EXPR
, boolean_true_node
,
12335 tf_warning_or_error
);
12336 TREE_OPERAND (sub
, 1)
12337 = cp_build_compound_expr (set
, TREE_OPERAND (sub
, 1),
12338 tf_warning_or_error
);
12340 cur_cond_guard
= NULL_TREE
;
12341 if (TREE_OPERAND (sub
, 2))
12342 TREE_OPERAND (sub
, 2)
12343 = extend_ref_init_temps_1 (decl
, TREE_OPERAND (sub
, 2), cleanups
,
12345 if (cur_cond_guard
)
12347 tree set
= cp_build_modify_expr (UNKNOWN_LOCATION
, cur_cond_guard
,
12348 NOP_EXPR
, boolean_true_node
,
12349 tf_warning_or_error
);
12350 TREE_OPERAND (sub
, 2)
12351 = cp_build_compound_expr (set
, TREE_OPERAND (sub
, 2),
12352 tf_warning_or_error
);
12356 if (TREE_CODE (sub
) != ADDR_EXPR
)
12358 /* Deal with binding to a subobject. */
12359 for (p
= &TREE_OPERAND (sub
, 0);
12360 TREE_CODE (*p
) == COMPONENT_REF
|| TREE_CODE (*p
) == ARRAY_REF
; )
12361 p
= &TREE_OPERAND (*p
, 0);
12362 if (TREE_CODE (*p
) == TARGET_EXPR
)
12364 tree subinit
= NULL_TREE
;
12365 *p
= set_up_extended_ref_temp (decl
, *p
, cleanups
, &subinit
, cond_guard
);
12366 recompute_tree_invariant_for_addr_expr (sub
);
12368 init
= fold_convert (TREE_TYPE (init
), sub
);
12370 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), subinit
, init
);
12375 /* INIT is part of the initializer for DECL. If there are any
12376 reference or initializer lists being initialized, extend their
12377 lifetime to match that of DECL. */
12380 extend_ref_init_temps (tree decl
, tree init
, vec
<tree
, va_gc
> **cleanups
,
12383 tree type
= TREE_TYPE (init
);
12384 if (processing_template_decl
)
12386 if (TYPE_REF_P (type
))
12387 init
= extend_ref_init_temps_1 (decl
, init
, cleanups
, cond_guard
);
12391 if (TREE_CODE (ctor
) == TARGET_EXPR
)
12392 ctor
= TARGET_EXPR_INITIAL (ctor
);
12393 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
12395 /* [dcl.init] When initializing an aggregate from a parenthesized list
12396 of values... a temporary object bound to a reference does not have
12397 its lifetime extended. */
12398 if (CONSTRUCTOR_IS_PAREN_INIT (ctor
))
12401 if (is_std_init_list (type
))
12403 /* The temporary array underlying a std::initializer_list
12404 is handled like a reference temporary. */
12405 tree array
= CONSTRUCTOR_ELT (ctor
, 0)->value
;
12406 array
= extend_ref_init_temps_1 (decl
, array
, cleanups
,
12408 CONSTRUCTOR_ELT (ctor
, 0)->value
= array
;
12413 constructor_elt
*p
;
12414 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
12415 FOR_EACH_VEC_SAFE_ELT (elts
, i
, p
)
12416 p
->value
= extend_ref_init_temps (decl
, p
->value
, cleanups
,
12419 recompute_constructor_flags (ctor
);
12420 if (decl_maybe_constant_var_p (decl
) && TREE_CONSTANT (ctor
))
12421 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
12428 /* Returns true iff an initializer for TYPE could contain temporaries that
12429 need to be extended because they are bound to references or
12430 std::initializer_list. */
12433 type_has_extended_temps (tree type
)
12435 type
= strip_array_types (type
);
12436 if (TYPE_REF_P (type
))
12438 if (CLASS_TYPE_P (type
))
12440 if (is_std_init_list (type
))
12442 for (tree f
= next_initializable_field (TYPE_FIELDS (type
));
12443 f
; f
= next_initializable_field (DECL_CHAIN (f
)))
12444 if (type_has_extended_temps (TREE_TYPE (f
)))
12450 /* Returns true iff TYPE is some variant of std::initializer_list. */
12453 is_std_init_list (tree type
)
12455 if (!TYPE_P (type
))
12457 if (cxx_dialect
== cxx98
)
12459 /* Look through typedefs. */
12460 type
= TYPE_MAIN_VARIANT (type
);
12461 return (CLASS_TYPE_P (type
)
12462 && CP_TYPE_CONTEXT (type
) == std_node
12463 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
)));
12466 /* Returns true iff DECL is a list constructor: i.e. a constructor which
12467 will accept an argument list of a single std::initializer_list<T>. */
12470 is_list_ctor (tree decl
)
12472 tree args
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
12475 if (!args
|| args
== void_list_node
)
12478 arg
= non_reference (TREE_VALUE (args
));
12479 if (!is_std_init_list (arg
))
12482 args
= TREE_CHAIN (args
);
12484 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
12485 /* There are more non-defaulted parms. */
12491 #include "gt-cp-call.h"