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 /* The `this', `in_chrg' and VTT arguments to constructors are not
2224 considered in overload resolution. */
2225 if (DECL_CONSTRUCTOR_P (fn
))
2227 if (ctor_omit_inherited_parms (fn
))
2228 /* Bring back parameters omitted from an inherited ctor. */
2229 parmlist
= FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn
));
2231 parmlist
= skip_artificial_parms_for (fn
, parmlist
);
2232 skip
= num_artificial_parms_for (fn
);
2233 if (skip
> 0 && first_arg
!= NULL_TREE
)
2236 first_arg
= NULL_TREE
;
2242 len
= vec_safe_length (args
) - skip
+ (first_arg
!= NULL_TREE
? 1 : 0);
2244 convs
= alloc_conversions (len
);
2246 /* 13.3.2 - Viable functions [over.match.viable]
2247 First, to be a viable function, a candidate function shall have enough
2248 parameters to agree in number with the arguments in the list.
2250 We need to check this first; otherwise, checking the ICSes might cause
2251 us to produce an ill-formed template instantiation. */
2253 parmnode
= parmlist
;
2254 for (i
= 0; i
< len
; ++i
)
2256 if (parmnode
== NULL_TREE
|| parmnode
== void_list_node
)
2258 parmnode
= TREE_CHAIN (parmnode
);
2261 if ((i
< len
&& parmnode
)
2262 || !sufficient_parms_p (parmnode
))
2264 int remaining
= remaining_arguments (parmnode
);
2266 reason
= arity_rejection (first_arg
, i
+ remaining
, len
);
2269 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2270 parameter of type "reference to cv C" (including such a constructor
2271 instantiated from a template) is excluded from the set of candidate
2272 functions when used to construct an object of type D with an argument list
2273 containing a single argument if C is reference-related to D. */
2274 if (viable
&& len
== 1 && parmlist
&& DECL_CONSTRUCTOR_P (fn
)
2275 && flag_new_inheriting_ctors
2276 && DECL_INHERITED_CTOR (fn
))
2278 tree ptype
= non_reference (TREE_VALUE (parmlist
));
2279 tree dtype
= DECL_CONTEXT (fn
);
2280 tree btype
= DECL_INHERITED_CTOR_BASE (fn
);
2281 if (reference_related_p (ptype
, dtype
)
2282 && reference_related_p (btype
, ptype
))
2285 reason
= inherited_ctor_rejection ();
2289 /* Second, for a function to be viable, its constraints must be
2291 if (flag_concepts
&& viable
&& !constraints_satisfied_p (fn
))
2293 reason
= constraint_failure ();
2297 /* When looking for a function from a subobject from an implicit
2298 copy/move constructor/operator=, don't consider anything that takes (a
2299 reference to) an unrelated type. See c++/44909 and core 1092. */
2300 if (viable
&& parmlist
&& (flags
& LOOKUP_DEFAULTED
))
2302 if (DECL_CONSTRUCTOR_P (fn
))
2304 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
2305 && DECL_OVERLOADED_OPERATOR_IS (fn
, NOP_EXPR
))
2311 parmnode
= chain_index (i
-1, parmlist
);
2312 if (!reference_related_p (non_reference (TREE_VALUE (parmnode
)),
2317 /* This only applies at the top level. */
2318 flags
&= ~LOOKUP_DEFAULTED
;
2324 /* Third, for F to be a viable function, there shall exist for each
2325 argument an implicit conversion sequence that converts that argument
2326 to the corresponding parameter of F. */
2328 parmnode
= parmlist
;
2330 for (i
= 0; i
< len
; ++i
)
2332 tree argtype
, to_type
;
2337 if (parmnode
== void_list_node
)
2342 /* Already set during deduction. */
2343 parmnode
= TREE_CHAIN (parmnode
);
2347 if (i
== 0 && first_arg
!= NULL_TREE
)
2350 arg
= CONST_CAST_TREE (
2351 (*args
)[i
+ skip
- (first_arg
!= NULL_TREE
? 1 : 0)]);
2352 argtype
= lvalue_type (arg
);
2354 is_this
= (i
== 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2355 && ! DECL_CONSTRUCTOR_P (fn
));
2359 tree parmtype
= TREE_VALUE (parmnode
);
2361 parmnode
= TREE_CHAIN (parmnode
);
2363 /* The type of the implicit object parameter ('this') for
2364 overload resolution is not always the same as for the
2365 function itself; conversion functions are considered to
2366 be members of the class being converted, and functions
2367 introduced by a using-declaration are considered to be
2368 members of the class that uses them.
2370 Since build_over_call ignores the ICS for the `this'
2371 parameter, we can just change the parm type. */
2372 if (ctype
&& is_this
)
2374 parmtype
= cp_build_qualified_type
2375 (ctype
, cp_type_quals (TREE_TYPE (parmtype
)));
2376 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn
)))
2378 /* If the function has a ref-qualifier, the implicit
2379 object parameter has reference type. */
2380 bool rv
= FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn
));
2381 parmtype
= cp_build_reference_type (parmtype
, rv
);
2382 /* The special handling of 'this' conversions in compare_ics
2383 does not apply if there is a ref-qualifier. */
2388 parmtype
= build_pointer_type (parmtype
);
2389 /* We don't use build_this here because we don't want to
2390 capture the object argument until we've chosen a
2391 non-static member function. */
2392 arg
= build_address (arg
);
2393 argtype
= lvalue_type (arg
);
2397 int lflags
= conv_flags (i
, len
-skip
, fn
, arg
, flags
);
2399 t
= implicit_conversion (parmtype
, argtype
, arg
,
2400 /*c_cast_p=*/false, lflags
, complain
);
2405 t
= build_identity_conv (argtype
, arg
);
2406 t
->ellipsis_p
= true;
2417 reason
= arg_conversion_rejection (first_arg
, i
, argtype
, to_type
,
2418 EXPR_LOCATION (arg
));
2425 reason
= bad_arg_conversion_rejection (first_arg
, i
, arg
, to_type
,
2426 EXPR_LOCATION (arg
));
2432 return add_candidate (candidates
, fn
, orig_first_arg
, args
, len
, convs
,
2433 access_path
, conversion_path
, viable
, reason
, flags
);
2436 /* Create an overload candidate for the conversion function FN which will
2437 be invoked for expression OBJ, producing a pointer-to-function which
2438 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2439 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2440 passed on to implicit_conversion.
2442 Actually, we don't really care about FN; we care about the type it
2443 converts to. There may be multiple conversion functions that will
2444 convert to that type, and we rely on build_user_type_conversion_1 to
2445 choose the best one; so when we create our candidate, we record the type
2446 instead of the function. */
2448 static struct z_candidate
*
2449 add_conv_candidate (struct z_candidate
**candidates
, tree fn
, tree obj
,
2450 const vec
<tree
, va_gc
> *arglist
,
2451 tree access_path
, tree conversion_path
,
2452 tsubst_flags_t complain
)
2454 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
2455 int i
, len
, viable
, flags
;
2456 tree parmlist
, parmnode
;
2458 struct rejection_reason
*reason
;
2460 for (parmlist
= totype
; TREE_CODE (parmlist
) != FUNCTION_TYPE
; )
2461 parmlist
= TREE_TYPE (parmlist
);
2462 parmlist
= TYPE_ARG_TYPES (parmlist
);
2464 len
= vec_safe_length (arglist
) + 1;
2465 convs
= alloc_conversions (len
);
2466 parmnode
= parmlist
;
2468 flags
= LOOKUP_IMPLICIT
;
2471 /* Don't bother looking up the same type twice. */
2472 if (*candidates
&& (*candidates
)->fn
== totype
)
2475 for (i
= 0; i
< len
; ++i
)
2477 tree arg
, argtype
, convert_type
= NULL_TREE
;
2483 arg
= (*arglist
)[i
- 1];
2484 argtype
= lvalue_type (arg
);
2488 t
= build_identity_conv (argtype
, NULL_TREE
);
2489 t
= build_conv (ck_user
, totype
, t
);
2490 /* Leave the 'cand' field null; we'll figure out the conversion in
2491 convert_like if this candidate is chosen. */
2492 convert_type
= totype
;
2494 else if (parmnode
== void_list_node
)
2498 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
,
2499 /*c_cast_p=*/false, flags
, complain
);
2500 convert_type
= TREE_VALUE (parmnode
);
2504 t
= build_identity_conv (argtype
, arg
);
2505 t
->ellipsis_p
= true;
2506 convert_type
= argtype
;
2516 reason
= bad_arg_conversion_rejection (NULL_TREE
, i
, arg
, convert_type
,
2517 EXPR_LOCATION (arg
));
2524 parmnode
= TREE_CHAIN (parmnode
);
2528 || ! sufficient_parms_p (parmnode
))
2530 int remaining
= remaining_arguments (parmnode
);
2532 reason
= arity_rejection (NULL_TREE
, i
+ remaining
, len
);
2535 return add_candidate (candidates
, totype
, obj
, arglist
, len
, convs
,
2536 access_path
, conversion_path
, viable
, reason
, flags
);
2540 build_builtin_candidate (struct z_candidate
**candidates
, tree fnname
,
2541 tree type1
, tree type2
, const vec
<tree
,va_gc
> &args
,
2542 tree
*argtypes
, int flags
, tsubst_flags_t complain
)
2549 struct rejection_reason
*reason
= NULL
;
2554 num_convs
= args
.length ();
2555 convs
= alloc_conversions (num_convs
);
2557 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2558 conversion ops are allowed. We handle that here by just checking for
2559 boolean_type_node because other operators don't ask for it. COND_EXPR
2560 also does contextual conversion to bool for the first operand, but we
2561 handle that in build_conditional_expr, and type1 here is operand 2. */
2562 if (type1
!= boolean_type_node
)
2563 flags
|= LOOKUP_ONLYCONVERTING
;
2565 for (unsigned i
= 0; i
< 2 && i
< num_convs
; ++i
)
2567 t
= implicit_conversion (types
[i
], argtypes
[i
], args
[i
],
2568 /*c_cast_p=*/false, flags
, complain
);
2572 /* We need something for printing the candidate. */
2573 t
= build_identity_conv (types
[i
], NULL_TREE
);
2574 reason
= arg_conversion_rejection (NULL_TREE
, i
, argtypes
[i
],
2575 types
[i
], EXPR_LOCATION (args
[i
]));
2580 reason
= bad_arg_conversion_rejection (NULL_TREE
, i
, args
[i
],
2582 EXPR_LOCATION (args
[i
]));
2587 /* For COND_EXPR we rearranged the arguments; undo that now. */
2590 convs
[2] = convs
[1];
2591 convs
[1] = convs
[0];
2592 t
= implicit_conversion (boolean_type_node
, argtypes
[2], args
[2],
2593 /*c_cast_p=*/false, flags
,
2600 reason
= arg_conversion_rejection (NULL_TREE
, 0, argtypes
[2],
2602 EXPR_LOCATION (args
[2]));
2606 add_candidate (candidates
, fnname
, /*first_arg=*/NULL_TREE
, /*args=*/NULL
,
2608 /*access_path=*/NULL_TREE
,
2609 /*conversion_path=*/NULL_TREE
,
2610 viable
, reason
, flags
);
2614 is_complete (tree t
)
2616 return COMPLETE_TYPE_P (complete_type (t
));
2619 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2622 promoted_arithmetic_type_p (tree type
)
2626 In this section, the term promoted integral type is used to refer
2627 to those integral types which are preserved by integral promotion
2628 (including e.g. int and long but excluding e.g. char).
2629 Similarly, the term promoted arithmetic type refers to promoted
2630 integral types plus floating types. */
2631 return ((CP_INTEGRAL_TYPE_P (type
)
2632 && same_type_p (type_promotes_to (type
), type
))
2633 || TREE_CODE (type
) == REAL_TYPE
);
2636 /* Create any builtin operator overload candidates for the operator in
2637 question given the converted operand types TYPE1 and TYPE2. The other
2638 args are passed through from add_builtin_candidates to
2639 build_builtin_candidate.
2641 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2642 If CODE is requires candidates operands of the same type of the kind
2643 of which TYPE1 and TYPE2 are, we add both candidates
2644 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2647 add_builtin_candidate (struct z_candidate
**candidates
, enum tree_code code
,
2648 enum tree_code code2
, tree fnname
, tree type1
,
2649 tree type2
, vec
<tree
,va_gc
> &args
, tree
*argtypes
,
2650 int flags
, tsubst_flags_t complain
)
2654 case POSTINCREMENT_EXPR
:
2655 case POSTDECREMENT_EXPR
:
2656 args
[1] = integer_zero_node
;
2657 type2
= integer_type_node
;
2666 /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2667 and VQ is either volatile or empty, there exist candidate operator
2668 functions of the form
2669 VQ T& operator++(VQ T&);
2670 T operator++(VQ T&, int);
2671 5 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 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2677 type, and VQ is either volatile or empty, there exist candidate operator
2678 functions of the form
2679 T*VQ& operator++(T*VQ&);
2680 T*VQ& operator--(T*VQ&);
2681 T* operator++(T*VQ&, int);
2682 T* operator--(T*VQ&, int); */
2684 case POSTDECREMENT_EXPR
:
2685 case PREDECREMENT_EXPR
:
2686 if (TREE_CODE (type1
) == BOOLEAN_TYPE
)
2689 case POSTINCREMENT_EXPR
:
2690 case PREINCREMENT_EXPR
:
2691 /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2693 if (TREE_CODE (type1
) == BOOLEAN_TYPE
&& cxx_dialect
>= cxx17
)
2695 if (ARITHMETIC_TYPE_P (type1
) || TYPE_PTROB_P (type1
))
2697 type1
= build_reference_type (type1
);
2702 /* 7 For every cv-qualified or cv-unqualified object type T, there
2703 exist candidate operator functions of the form
2708 8 For every function type T that does not have cv-qualifiers or
2709 a ref-qualifier, there exist candidate operator functions of the form
2710 T& operator*(T*); */
2713 if (TYPE_PTR_P (type1
)
2714 && (TYPE_PTROB_P (type1
)
2715 || TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
))
2719 /* 9 For every type T, there exist candidate operator functions of the form
2722 10 For every floating-point or promoted integral type T, there exist
2723 candidate operator functions of the form
2727 case UNARY_PLUS_EXPR
: /* unary + */
2728 if (TYPE_PTR_P (type1
))
2732 if (ARITHMETIC_TYPE_P (type1
))
2736 /* 11 For every promoted integral type T, there exist candidate operator
2737 functions of the form
2741 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
))
2745 /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
2746 is the same type as C2 or is a derived class of C2, and T is an object
2747 type or a function type there exist candidate operator functions of the
2749 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2750 where CV12 is the union of CV1 and CV2. */
2753 if (TYPE_PTR_P (type1
) && TYPE_PTRMEM_P (type2
))
2755 tree c1
= TREE_TYPE (type1
);
2756 tree c2
= TYPE_PTRMEM_CLASS_TYPE (type2
);
2758 if (MAYBE_CLASS_TYPE_P (c1
) && DERIVED_FROM_P (c2
, c1
)
2759 && (TYPE_PTRMEMFUNC_P (type2
)
2760 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2
))))
2765 /* 13 For every pair of types L and R, where each of L and R is a floating-point
2766 or promoted integral type, there exist candidate operator functions of the
2772 bool operator<(L, R);
2773 bool operator>(L, R);
2774 bool operator<=(L, R);
2775 bool operator>=(L, R);
2776 bool operator==(L, R);
2777 bool operator!=(L, R);
2778 where LR is the result of the usual arithmetic conversions between
2781 14 For every integral type T there exists a candidate operator function of
2784 std::strong_ordering operator<=>(T, T);
2786 15 For every pair of floating-point types L and R, there exists a candidate
2787 operator function of the form
2789 std::partial_ordering operator<=>(L, R);
2791 16 For every cv-qualified or cv-unqualified object type T there exist
2792 candidate operator functions of the form
2793 T* operator+(T*, std::ptrdiff_t);
2794 T& operator[](T*, std::ptrdiff_t);
2795 T* operator-(T*, std::ptrdiff_t);
2796 T* operator+(std::ptrdiff_t, T*);
2797 T& operator[](std::ptrdiff_t, T*);
2799 17 For every T, where T is a pointer to object type, there exist candidate
2800 operator functions of the form
2801 std::ptrdiff_t operator-(T, T);
2803 18 For every T, where T is an enumeration type or a pointer type, there
2804 exist candidate operator functions of the form
2805 bool operator<(T, T);
2806 bool operator>(T, T);
2807 bool operator<=(T, T);
2808 bool operator>=(T, T);
2809 bool operator==(T, T);
2810 bool operator!=(T, T);
2811 R operator<=>(T, T);
2813 where R is the result type specified in [expr.spaceship].
2815 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
2816 there exist candidate operator functions of the form
2817 bool operator==(T, T);
2818 bool operator!=(T, T); */
2821 if (TYPE_PTROB_P (type1
) && TYPE_PTROB_P (type2
))
2823 if (TYPE_PTROB_P (type1
)
2824 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2826 type2
= ptrdiff_type_node
;
2831 case TRUNC_DIV_EXPR
:
2832 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2836 /* This isn't exactly what's specified above for operator<=>, but it's
2837 close enough. In particular, we don't care about the return type
2838 specified above; it doesn't participate in overload resolution and it
2839 doesn't affect the semantics of the built-in operator. */
2840 case SPACESHIP_EXPR
:
2843 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
2844 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
)))
2846 if (NULLPTR_TYPE_P (type1
) && NULLPTR_TYPE_P (type2
))
2848 if (TYPE_PTRMEM_P (type1
) && null_ptr_cst_p (args
[1]))
2853 if (TYPE_PTRMEM_P (type2
) && null_ptr_cst_p (args
[0]))
2865 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2867 if (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
2869 if (TREE_CODE (type1
) == ENUMERAL_TYPE
2870 && TREE_CODE (type2
) == ENUMERAL_TYPE
)
2872 if (TYPE_PTR_P (type1
)
2873 && null_ptr_cst_p (args
[1]))
2878 if (null_ptr_cst_p (args
[0])
2879 && TYPE_PTR_P (type2
))
2887 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2891 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
) && TYPE_PTROB_P (type2
))
2893 type1
= ptrdiff_type_node
;
2896 if (TYPE_PTROB_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2898 type2
= ptrdiff_type_node
;
2903 /* 18For every pair of promoted integral types L and R, there exist candi-
2904 date operator functions of the form
2911 where LR is the result of the usual arithmetic conversions between
2914 case TRUNC_MOD_EXPR
:
2920 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2924 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2925 type, VQ is either volatile or empty, and R is a promoted arithmetic
2926 type, there exist candidate operator functions of the form
2927 VQ L& operator=(VQ L&, R);
2928 VQ L& operator*=(VQ L&, R);
2929 VQ L& operator/=(VQ L&, R);
2930 VQ L& operator+=(VQ L&, R);
2931 VQ L& operator-=(VQ L&, R);
2933 20For every pair T, VQ), where T is any type and VQ is either volatile
2934 or empty, there exist candidate operator functions of the form
2935 T*VQ& operator=(T*VQ&, T*);
2937 21For every pair T, VQ), where T is a pointer to member type and VQ is
2938 either volatile or empty, there exist candidate operator functions of
2940 VQ T& operator=(VQ T&, T);
2942 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2943 unqualified complete object type, VQ is either volatile or empty, and
2944 I is a promoted integral type, there exist candidate operator func-
2946 T*VQ& operator+=(T*VQ&, I);
2947 T*VQ& operator-=(T*VQ&, I);
2949 23For every triple L, VQ, R), where L is an integral or enumeration
2950 type, VQ is either volatile or empty, and R is a promoted integral
2951 type, there exist candidate operator functions of the form
2953 VQ L& operator%=(VQ L&, R);
2954 VQ L& operator<<=(VQ L&, R);
2955 VQ L& operator>>=(VQ L&, R);
2956 VQ L& operator&=(VQ L&, R);
2957 VQ L& operator^=(VQ L&, R);
2958 VQ L& operator|=(VQ L&, R); */
2965 if (TYPE_PTROB_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2967 type2
= ptrdiff_type_node
;
2972 case TRUNC_DIV_EXPR
:
2973 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2977 case TRUNC_MOD_EXPR
:
2983 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1
) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2
))
2988 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2990 if ((TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
))
2991 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
2992 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
2993 || ((TYPE_PTRMEMFUNC_P (type1
)
2994 || TYPE_PTR_P (type1
))
2995 && null_ptr_cst_p (args
[1])))
3005 type1
= build_reference_type (type1
);
3011 For every pair of promoted arithmetic types L and R, there
3012 exist candidate operator functions of the form
3014 LR operator?(bool, L, R);
3016 where LR is the result of the usual arithmetic conversions
3017 between types L and R.
3019 For every type T, where T is a pointer or pointer-to-member
3020 type, there exist candidate operator functions of the form T
3021 operator?(bool, T, T); */
3023 if (promoted_arithmetic_type_p (type1
)
3024 && promoted_arithmetic_type_p (type2
))
3028 /* Otherwise, the types should be pointers. */
3029 if (!TYPE_PTR_OR_PTRMEM_P (type1
) || !TYPE_PTR_OR_PTRMEM_P (type2
))
3032 /* We don't check that the two types are the same; the logic
3033 below will actually create two candidates; one in which both
3034 parameter types are TYPE1, and one in which both parameter
3040 if (ARITHMETIC_TYPE_P (type1
))
3048 /* Make sure we don't create builtin candidates with dependent types. */
3049 bool u1
= uses_template_parms (type1
);
3050 bool u2
= type2
? uses_template_parms (type2
) : false;
3053 /* Try to recover if one of the types is non-dependent. But if
3054 there's only one type, there's nothing we can do. */
3057 /* And we lose if both are dependent. */
3060 /* Or if they have different forms. */
3061 if (TREE_CODE (type1
) != TREE_CODE (type2
))
3070 /* If we're dealing with two pointer types or two enumeral types,
3071 we need candidates for both of them. */
3072 if (type2
&& !same_type_p (type1
, type2
)
3073 && TREE_CODE (type1
) == TREE_CODE (type2
)
3074 && (TYPE_REF_P (type1
)
3075 || (TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
3076 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
3077 || TYPE_PTRMEMFUNC_P (type1
)
3078 || MAYBE_CLASS_TYPE_P (type1
)
3079 || TREE_CODE (type1
) == ENUMERAL_TYPE
))
3081 if (TYPE_PTR_OR_PTRMEM_P (type1
))
3083 tree cptype
= composite_pointer_type (input_location
,
3089 if (cptype
!= error_mark_node
)
3091 build_builtin_candidate
3092 (candidates
, fnname
, cptype
, cptype
, args
, argtypes
,
3098 build_builtin_candidate
3099 (candidates
, fnname
, type1
, type1
, args
, argtypes
, flags
, complain
);
3100 build_builtin_candidate
3101 (candidates
, fnname
, type2
, type2
, args
, argtypes
, flags
, complain
);
3105 build_builtin_candidate
3106 (candidates
, fnname
, type1
, type2
, args
, argtypes
, flags
, complain
);
3110 type_decays_to (tree type
)
3112 if (TREE_CODE (type
) == ARRAY_TYPE
)
3113 return build_pointer_type (TREE_TYPE (type
));
3114 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3115 return build_pointer_type (type
);
3119 /* There are three conditions of builtin candidates:
3121 1) bool-taking candidates. These are the same regardless of the input.
3122 2) pointer-pair taking candidates. These are generated for each type
3123 one of the input types converts to.
3124 3) arithmetic candidates. According to the standard, we should generate
3125 all of these, but I'm trying not to...
3127 Here we generate a superset of the possible candidates for this particular
3128 case. That is a subset of the full set the standard defines, plus some
3129 other cases which the standard disallows. add_builtin_candidate will
3130 filter out the invalid set. */
3133 add_builtin_candidates (struct z_candidate
**candidates
, enum tree_code code
,
3134 enum tree_code code2
, tree fnname
,
3135 vec
<tree
, va_gc
> *argv
,
3136 int flags
, tsubst_flags_t complain
)
3140 tree type
, argtypes
[3], t
;
3141 /* TYPES[i] is the set of possible builtin-operator parameter types
3142 we will consider for the Ith argument. */
3143 vec
<tree
, va_gc
> *types
[2];
3145 vec
<tree
, va_gc
> &args
= *argv
;
3146 unsigned len
= args
.length ();
3148 for (unsigned i
= 0; i
< len
; ++i
)
3151 argtypes
[i
] = unlowered_expr_type (args
[i
]);
3153 argtypes
[i
] = NULL_TREE
;
3158 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3159 and VQ is either volatile or empty, there exist candidate operator
3160 functions of the form
3161 VQ T& operator++(VQ T&); */
3163 case POSTINCREMENT_EXPR
:
3164 case PREINCREMENT_EXPR
:
3165 case POSTDECREMENT_EXPR
:
3166 case PREDECREMENT_EXPR
:
3171 /* 24There also exist candidate operator functions of the form
3172 bool operator!(bool);
3173 bool operator&&(bool, bool);
3174 bool operator||(bool, bool); */
3176 case TRUTH_NOT_EXPR
:
3177 build_builtin_candidate
3178 (candidates
, fnname
, boolean_type_node
,
3179 NULL_TREE
, args
, argtypes
, flags
, complain
);
3182 case TRUTH_ORIF_EXPR
:
3183 case TRUTH_ANDIF_EXPR
:
3184 build_builtin_candidate
3185 (candidates
, fnname
, boolean_type_node
,
3186 boolean_type_node
, args
, argtypes
, flags
, complain
);
3202 case SPACESHIP_EXPR
:
3210 types
[0] = make_tree_vector ();
3211 types
[1] = make_tree_vector ();
3215 for (unsigned i
= 0; i
< len
; ++i
)
3217 if (MAYBE_CLASS_TYPE_P (argtypes
[i
]))
3221 if (i
== 0 && code
== MODIFY_EXPR
&& code2
== NOP_EXPR
)
3224 convs
= lookup_conversions (argtypes
[i
]);
3226 if (code
== COND_EXPR
)
3228 if (lvalue_p (args
[i
]))
3229 vec_safe_push (types
[i
], build_reference_type (argtypes
[i
]));
3231 vec_safe_push (types
[i
], TYPE_MAIN_VARIANT (argtypes
[i
]));
3237 for (; convs
; convs
= TREE_CHAIN (convs
))
3239 type
= TREE_TYPE (convs
);
3242 && (!TYPE_REF_P (type
)
3243 || CP_TYPE_CONST_P (TREE_TYPE (type
))))
3246 if (code
== COND_EXPR
&& TYPE_REF_P (type
))
3247 vec_safe_push (types
[i
], type
);
3249 type
= non_reference (type
);
3250 if (i
!= 0 || ! ref1
)
3252 type
= cv_unqualified (type_decays_to (type
));
3253 if (enum_p
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
3254 vec_safe_push (types
[i
], type
);
3255 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
3256 type
= type_promotes_to (type
);
3259 if (! vec_member (type
, types
[i
]))
3260 vec_safe_push (types
[i
], type
);
3265 if (code
== COND_EXPR
&& lvalue_p (args
[i
]))
3266 vec_safe_push (types
[i
], build_reference_type (argtypes
[i
]));
3267 type
= non_reference (argtypes
[i
]);
3268 if (i
!= 0 || ! ref1
)
3270 type
= cv_unqualified (type_decays_to (type
));
3271 if (enum_p
&& UNSCOPED_ENUM_P (type
))
3272 vec_safe_push (types
[i
], type
);
3273 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type
))
3274 type
= type_promotes_to (type
);
3276 vec_safe_push (types
[i
], type
);
3280 /* Run through the possible parameter types of both arguments,
3281 creating candidates with those parameter types. */
3282 FOR_EACH_VEC_ELT_REVERSE (*(types
[0]), ix
, t
)
3287 if (!types
[1]->is_empty ())
3288 FOR_EACH_VEC_ELT_REVERSE (*(types
[1]), jx
, u
)
3289 add_builtin_candidate
3290 (candidates
, code
, code2
, fnname
, t
,
3291 u
, args
, argtypes
, flags
, complain
);
3293 add_builtin_candidate
3294 (candidates
, code
, code2
, fnname
, t
,
3295 NULL_TREE
, args
, argtypes
, flags
, complain
);
3298 release_tree_vector (types
[0]);
3299 release_tree_vector (types
[1]);
3303 /* If TMPL can be successfully instantiated as indicated by
3304 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3306 TMPL is the template. EXPLICIT_TARGS are any explicit template
3307 arguments. ARGLIST is the arguments provided at the call-site.
3308 This does not change ARGLIST. The RETURN_TYPE is the desired type
3309 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3310 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3311 CTYPE are ignored, and OBJ is as for add_conv_candidate. */
3313 static struct z_candidate
*
3314 add_template_candidate_real (struct z_candidate
**candidates
, tree tmpl
,
3315 tree ctype
, tree explicit_targs
, tree first_arg
,
3316 const vec
<tree
, va_gc
> *arglist
, tree return_type
,
3317 tree access_path
, tree conversion_path
,
3318 int flags
, tree obj
, unification_kind_t strict
,
3319 tsubst_flags_t complain
)
3321 int ntparms
= DECL_NTPARMS (tmpl
);
3322 tree targs
= make_tree_vec (ntparms
);
3323 unsigned int len
= vec_safe_length (arglist
);
3324 unsigned int nargs
= (first_arg
== NULL_TREE
? 0 : 1) + len
;
3325 unsigned int skip_without_in_chrg
= 0;
3326 tree first_arg_without_in_chrg
= first_arg
;
3327 tree
*args_without_in_chrg
;
3328 unsigned int nargs_without_in_chrg
;
3329 unsigned int ia
, ix
;
3331 struct z_candidate
*cand
;
3333 struct rejection_reason
*reason
= NULL
;
3335 conversion
**convs
= NULL
;
3337 /* We don't do deduction on the in-charge parameter, the VTT
3338 parameter or 'this'. */
3339 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl
))
3341 if (first_arg_without_in_chrg
!= NULL_TREE
)
3342 first_arg_without_in_chrg
= NULL_TREE
;
3343 else if (return_type
&& strict
== DEDUCE_CALL
)
3344 /* We're deducing for a call to the result of a template conversion
3345 function, so the args don't contain 'this'; leave them alone. */;
3347 ++skip_without_in_chrg
;
3350 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl
)
3351 || DECL_BASE_CONSTRUCTOR_P (tmpl
))
3352 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl
)))
3354 if (first_arg_without_in_chrg
!= NULL_TREE
)
3355 first_arg_without_in_chrg
= NULL_TREE
;
3357 ++skip_without_in_chrg
;
3360 if (len
< skip_without_in_chrg
)
3363 if (DECL_CONSTRUCTOR_P (tmpl
) && nargs
== 2
3364 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg
),
3365 TREE_TYPE ((*arglist
)[0])))
3367 /* 12.8/6 says, "A declaration of a constructor for a class X is
3368 ill-formed if its first parameter is of type (optionally cv-qualified)
3369 X and either there are no other parameters or else all other
3370 parameters have default arguments. A member function template is never
3371 instantiated to produce such a constructor signature."
3373 So if we're trying to copy an object of the containing class, don't
3374 consider a template constructor that has a first parameter type that
3375 is just a template parameter, as we would deduce a signature that we
3376 would then reject in the code below. */
3377 if (tree firstparm
= FUNCTION_FIRST_USER_PARMTYPE (tmpl
))
3379 firstparm
= TREE_VALUE (firstparm
);
3380 if (PACK_EXPANSION_P (firstparm
))
3381 firstparm
= PACK_EXPANSION_PATTERN (firstparm
);
3382 if (TREE_CODE (firstparm
) == TEMPLATE_TYPE_PARM
)
3384 gcc_assert (!explicit_targs
);
3385 reason
= invalid_copy_with_fn_template_rejection ();
3391 nargs_without_in_chrg
= ((first_arg_without_in_chrg
!= NULL_TREE
? 1 : 0)
3392 + (len
- skip_without_in_chrg
));
3393 args_without_in_chrg
= XALLOCAVEC (tree
, nargs_without_in_chrg
);
3395 if (first_arg_without_in_chrg
!= NULL_TREE
)
3397 args_without_in_chrg
[ia
] = first_arg_without_in_chrg
;
3400 for (ix
= skip_without_in_chrg
;
3401 vec_safe_iterate (arglist
, ix
, &arg
);
3404 args_without_in_chrg
[ia
] = arg
;
3407 gcc_assert (ia
== nargs_without_in_chrg
);
3409 errs
= errorcount
+sorrycount
;
3411 convs
= alloc_conversions (nargs
);
3412 fn
= fn_type_unification (tmpl
, explicit_targs
, targs
,
3413 args_without_in_chrg
,
3414 nargs_without_in_chrg
,
3415 return_type
, strict
, flags
, convs
,
3416 false, complain
& tf_decltype
);
3418 if (fn
== error_mark_node
)
3420 /* Don't repeat unification later if it already resulted in errors. */
3421 if (errorcount
+sorrycount
== errs
)
3422 reason
= template_unification_rejection (tmpl
, explicit_targs
,
3423 targs
, args_without_in_chrg
,
3424 nargs_without_in_chrg
,
3425 return_type
, strict
, flags
);
3427 reason
= template_unification_error_rejection ();
3431 /* Now the explicit specifier might have been deduced; check if this
3432 declaration is explicit. If it is and we're ignoring non-converting
3433 constructors, don't add this function to the set of candidates. */
3434 if ((flags
& LOOKUP_ONLYCONVERTING
) && DECL_NONCONVERTING_P (fn
))
3437 if (DECL_CONSTRUCTOR_P (fn
) && nargs
== 2)
3439 tree arg_types
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
3440 if (arg_types
&& same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types
)),
3443 /* We're trying to produce a constructor with a prohibited signature,
3444 as discussed above; handle here any cases we didn't catch then,
3446 reason
= invalid_copy_with_fn_template_rejection ();
3451 if (obj
!= NULL_TREE
)
3452 /* Aha, this is a conversion function. */
3453 cand
= add_conv_candidate (candidates
, fn
, obj
, arglist
,
3454 access_path
, conversion_path
, complain
);
3456 cand
= add_function_candidate (candidates
, fn
, ctype
,
3457 first_arg
, arglist
, access_path
,
3458 conversion_path
, flags
, convs
, complain
);
3459 if (DECL_TI_TEMPLATE (fn
) != tmpl
)
3460 /* This situation can occur if a member template of a template
3461 class is specialized. Then, instantiate_template might return
3462 an instantiation of the specialization, in which case the
3463 DECL_TI_TEMPLATE field will point at the original
3464 specialization. For example:
3466 template <class T> struct S { template <class U> void f(U);
3467 template <> void f(int) {}; };
3471 Here, TMPL will be template <class U> S<double>::f(U).
3472 And, instantiate template will give us the specialization
3473 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3474 for this will point at template <class T> template <> S<T>::f(int),
3475 so that we can find the definition. For the purposes of
3476 overload resolution, however, we want the original TMPL. */
3477 cand
->template_decl
= build_template_info (tmpl
, targs
);
3479 cand
->template_decl
= DECL_TEMPLATE_INFO (fn
);
3480 cand
->explicit_targs
= explicit_targs
;
3484 return add_candidate (candidates
, tmpl
, first_arg
, arglist
, nargs
, NULL
,
3485 access_path
, conversion_path
, 0, reason
, flags
);
3489 static struct z_candidate
*
3490 add_template_candidate (struct z_candidate
**candidates
, tree tmpl
, tree ctype
,
3491 tree explicit_targs
, tree first_arg
,
3492 const vec
<tree
, va_gc
> *arglist
, tree return_type
,
3493 tree access_path
, tree conversion_path
, int flags
,
3494 unification_kind_t strict
, tsubst_flags_t complain
)
3497 add_template_candidate_real (candidates
, tmpl
, ctype
,
3498 explicit_targs
, first_arg
, arglist
,
3499 return_type
, access_path
, conversion_path
,
3500 flags
, NULL_TREE
, strict
, complain
);
3503 /* Create an overload candidate for the conversion function template TMPL,
3504 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3505 pointer-to-function which will in turn be called with the argument list
3506 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3507 passed on to implicit_conversion. */
3509 static struct z_candidate
*
3510 add_template_conv_candidate (struct z_candidate
**candidates
, tree tmpl
,
3512 const vec
<tree
, va_gc
> *arglist
,
3513 tree return_type
, tree access_path
,
3514 tree conversion_path
, tsubst_flags_t complain
)
3516 /* Making this work broke PR 71117 and 85118, so until the committee resolves
3517 core issue 2189, let's disable this candidate if there are any call
3523 add_template_candidate_real (candidates
, tmpl
, NULL_TREE
, NULL_TREE
,
3524 NULL_TREE
, arglist
, return_type
, access_path
,
3525 conversion_path
, 0, obj
, DEDUCE_CALL
,
3529 /* The CANDS are the set of candidates that were considered for
3530 overload resolution. Return the set of viable candidates, or CANDS
3531 if none are viable. If any of the candidates were viable, set
3532 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3533 considered viable only if it is strictly viable. */
3535 static struct z_candidate
*
3536 splice_viable (struct z_candidate
*cands
,
3540 struct z_candidate
*viable
;
3541 struct z_candidate
**last_viable
;
3542 struct z_candidate
**cand
;
3543 bool found_strictly_viable
= false;
3545 /* Be strict inside templates, since build_over_call won't actually
3546 do the conversions to get pedwarns. */
3547 if (processing_template_decl
)
3551 last_viable
= &viable
;
3552 *any_viable_p
= false;
3557 struct z_candidate
*c
= *cand
;
3559 && (c
->viable
== 1 || TREE_CODE (c
->fn
) == TEMPLATE_DECL
))
3561 /* Be strict in the presence of a viable candidate. Also if
3562 there are template candidates, so that we get deduction errors
3563 for them instead of silently preferring a bad conversion. */
3565 if (viable
&& !found_strictly_viable
)
3567 /* Put any spliced near matches back onto the main list so
3568 that we see them if there is no strict match. */
3569 *any_viable_p
= false;
3570 *last_viable
= cands
;
3573 last_viable
= &viable
;
3577 if (strict_p
? c
->viable
== 1 : c
->viable
)
3582 last_viable
= &c
->next
;
3583 *any_viable_p
= true;
3585 found_strictly_viable
= true;
3591 return viable
? viable
: cands
;
3595 any_strictly_viable (struct z_candidate
*cands
)
3597 for (; cands
; cands
= cands
->next
)
3598 if (cands
->viable
== 1)
3603 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3604 words, it is about to become the "this" pointer for a member
3605 function call. Take the address of the object. */
3608 build_this (tree obj
)
3610 /* In a template, we are only concerned about the type of the
3611 expression, so we can take a shortcut. */
3612 if (processing_template_decl
)
3613 return build_address (obj
);
3615 return cp_build_addr_expr (obj
, tf_warning_or_error
);
3618 /* Returns true iff functions are equivalent. Equivalent functions are
3619 not '==' only if one is a function-local extern function or if
3620 both are extern "C". */
3623 equal_functions (tree fn1
, tree fn2
)
3625 if (TREE_CODE (fn1
) != TREE_CODE (fn2
))
3627 if (TREE_CODE (fn1
) == TEMPLATE_DECL
)
3629 if (DECL_LOCAL_DECL_P (fn1
) || DECL_LOCAL_DECL_P (fn2
)
3630 || DECL_EXTERN_C_FUNCTION_P (fn1
))
3631 return decls_match (fn1
, fn2
);
3635 /* Print information about a candidate FN being rejected due to INFO. */
3638 print_conversion_rejection (location_t loc
, struct conversion_info
*info
,
3641 tree from
= info
->from
;
3643 from
= lvalue_type (from
);
3644 if (info
->n_arg
== -1)
3646 /* Conversion of implicit `this' argument failed. */
3647 if (!TYPE_P (info
->from
))
3648 /* A bad conversion for 'this' must be discarding cv-quals. */
3649 inform (loc
, " passing %qT as %<this%> "
3650 "argument discards qualifiers",
3653 inform (loc
, " no known conversion for implicit "
3654 "%<this%> parameter from %qH to %qI",
3655 from
, info
->to_type
);
3657 else if (!TYPE_P (info
->from
))
3659 if (info
->n_arg
>= 0)
3660 inform (loc
, " conversion of argument %d would be ill-formed:",
3662 perform_implicit_conversion (info
->to_type
, info
->from
,
3663 tf_warning_or_error
);
3665 else if (info
->n_arg
== -2)
3666 /* Conversion of conversion function return value failed. */
3667 inform (loc
, " no known conversion from %qH to %qI",
3668 from
, info
->to_type
);
3671 if (TREE_CODE (fn
) == FUNCTION_DECL
)
3672 loc
= get_fndecl_argument_location (fn
, info
->n_arg
);
3673 inform (loc
, " no known conversion for argument %d from %qH to %qI",
3674 info
->n_arg
+ 1, from
, info
->to_type
);
3678 /* Print information about a candidate with WANT parameters and we found
3682 print_arity_information (location_t loc
, unsigned int have
, unsigned int want
)
3684 inform_n (loc
, want
,
3685 " candidate expects %d argument, %d provided",
3686 " candidate expects %d arguments, %d provided",
3690 /* Print information about one overload candidate CANDIDATE. MSGSTR
3691 is the text to print before the candidate itself.
3693 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3694 to have been run through gettext by the caller. This wart makes
3695 life simpler in print_z_candidates and for the translators. */
3698 print_z_candidate (location_t loc
, const char *msgstr
,
3699 struct z_candidate
*candidate
)
3701 const char *msg
= (msgstr
== NULL
3703 : ACONCAT ((_(msgstr
), " ", NULL
)));
3704 tree fn
= candidate
->fn
;
3705 if (flag_new_inheriting_ctors
)
3706 fn
= strip_inheriting_ctors (fn
);
3707 location_t cloc
= location_of (fn
);
3709 if (identifier_p (fn
))
3712 if (candidate
->num_convs
== 3)
3713 inform (cloc
, "%s%<%D(%T, %T, %T)%> (built-in)", msg
, fn
,
3714 candidate
->convs
[0]->type
,
3715 candidate
->convs
[1]->type
,
3716 candidate
->convs
[2]->type
);
3717 else if (candidate
->num_convs
== 2)
3718 inform (cloc
, "%s%<%D(%T, %T)%> (built-in)", msg
, fn
,
3719 candidate
->convs
[0]->type
,
3720 candidate
->convs
[1]->type
);
3722 inform (cloc
, "%s%<%D(%T)%> (built-in)", msg
, fn
,
3723 candidate
->convs
[0]->type
);
3725 else if (TYPE_P (fn
))
3726 inform (cloc
, "%s%qT (conversion)", msg
, fn
);
3727 else if (candidate
->viable
== -1)
3728 inform (cloc
, "%s%#qD (near match)", msg
, fn
);
3729 else if (DECL_DELETED_FN (fn
))
3730 inform (cloc
, "%s%#qD (deleted)", msg
, fn
);
3731 else if (candidate
->reversed ())
3732 inform (cloc
, "%s%#qD (reversed)", msg
, fn
);
3733 else if (candidate
->rewritten ())
3734 inform (cloc
, "%s%#qD (rewritten)", msg
, fn
);
3736 inform (cloc
, "%s%#qD", msg
, fn
);
3737 if (fn
!= candidate
->fn
)
3739 cloc
= location_of (candidate
->fn
);
3740 inform (cloc
, " inherited here");
3742 /* Give the user some information about why this candidate failed. */
3743 if (candidate
->reason
!= NULL
)
3745 struct rejection_reason
*r
= candidate
->reason
;
3750 print_arity_information (cloc
, r
->u
.arity
.actual
,
3751 r
->u
.arity
.expected
);
3753 case rr_arg_conversion
:
3754 print_conversion_rejection (cloc
, &r
->u
.conversion
, fn
);
3756 case rr_bad_arg_conversion
:
3757 print_conversion_rejection (cloc
, &r
->u
.bad_conversion
, fn
);
3759 case rr_explicit_conversion
:
3760 inform (cloc
, " return type %qT of explicit conversion function "
3761 "cannot be converted to %qT with a qualification "
3762 "conversion", r
->u
.conversion
.from
,
3763 r
->u
.conversion
.to_type
);
3765 case rr_template_conversion
:
3766 inform (cloc
, " conversion from return type %qT of template "
3767 "conversion function specialization to %qT is not an "
3768 "exact match", r
->u
.conversion
.from
,
3769 r
->u
.conversion
.to_type
);
3771 case rr_template_unification
:
3772 /* We use template_unification_error_rejection if unification caused
3773 actual non-SFINAE errors, in which case we don't need to repeat
3775 if (r
->u
.template_unification
.tmpl
== NULL_TREE
)
3777 inform (cloc
, " substitution of deduced template arguments "
3778 "resulted in errors seen above");
3781 /* Re-run template unification with diagnostics. */
3782 inform (cloc
, " template argument deduction/substitution failed:");
3783 fn_type_unification (r
->u
.template_unification
.tmpl
,
3784 r
->u
.template_unification
.explicit_targs
,
3786 (r
->u
.template_unification
.num_targs
)),
3787 r
->u
.template_unification
.args
,
3788 r
->u
.template_unification
.nargs
,
3789 r
->u
.template_unification
.return_type
,
3790 r
->u
.template_unification
.strict
,
3791 r
->u
.template_unification
.flags
,
3794 case rr_invalid_copy
:
3796 " a constructor taking a single argument of its own "
3797 "class type is invalid");
3799 case rr_constraint_failure
:
3800 diagnose_constraints (cloc
, fn
, NULL_TREE
);
3802 case rr_inherited_ctor
:
3803 inform (cloc
, " an inherited constructor is not a candidate for "
3804 "initialization from an expression of the same or derived "
3809 /* This candidate didn't have any issues or we failed to
3810 handle a particular code. Either way... */
3817 print_z_candidates (location_t loc
, struct z_candidate
*candidates
)
3819 struct z_candidate
*cand1
;
3820 struct z_candidate
**cand2
;
3825 /* Remove non-viable deleted candidates. */
3827 for (cand2
= &cand1
; *cand2
; )
3829 if (TREE_CODE ((*cand2
)->fn
) == FUNCTION_DECL
3830 && !(*cand2
)->viable
3831 && DECL_DELETED_FN ((*cand2
)->fn
))
3832 *cand2
= (*cand2
)->next
;
3834 cand2
= &(*cand2
)->next
;
3836 /* ...if there are any non-deleted ones. */
3840 /* There may be duplicates in the set of candidates. We put off
3841 checking this condition as long as possible, since we have no way
3842 to eliminate duplicates from a set of functions in less than n^2
3843 time. Now we are about to emit an error message, so it is more
3844 permissible to go slowly. */
3845 for (cand1
= candidates
; cand1
; cand1
= cand1
->next
)
3847 tree fn
= cand1
->fn
;
3848 /* Skip builtin candidates and conversion functions. */
3851 cand2
= &cand1
->next
;
3854 if (DECL_P ((*cand2
)->fn
)
3855 && equal_functions (fn
, (*cand2
)->fn
))
3856 *cand2
= (*cand2
)->next
;
3858 cand2
= &(*cand2
)->next
;
3862 for (; candidates
; candidates
= candidates
->next
)
3863 print_z_candidate (loc
, N_("candidate:"), candidates
);
3866 /* USER_SEQ is a user-defined conversion sequence, beginning with a
3867 USER_CONV. STD_SEQ is the standard conversion sequence applied to
3868 the result of the conversion function to convert it to the final
3869 desired type. Merge the two sequences into a single sequence,
3870 and return the merged sequence. */
3873 merge_conversion_sequences (conversion
*user_seq
, conversion
*std_seq
)
3876 bool bad
= user_seq
->bad_p
;
3878 gcc_assert (user_seq
->kind
== ck_user
);
3880 /* Find the end of the second conversion sequence. */
3881 for (t
= &std_seq
; (*t
)->kind
!= ck_identity
; t
= &((*t
)->u
.next
))
3883 /* The entire sequence is a user-conversion sequence. */
3884 (*t
)->user_conv_p
= true;
3889 if ((*t
)->rvaluedness_matches_p
)
3890 /* We're binding a reference directly to the result of the conversion.
3891 build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
3892 type, but we want it back. */
3893 user_seq
->type
= TREE_TYPE (TREE_TYPE (user_seq
->cand
->fn
));
3895 /* Replace the identity conversion with the user conversion
3902 /* Handle overload resolution for initializing an object of class type from
3903 an initializer list. First we look for a suitable constructor that
3904 takes a std::initializer_list; if we don't find one, we then look for a
3905 non-list constructor.
3907 Parameters are as for add_candidates, except that the arguments are in
3908 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
3909 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
3912 add_list_candidates (tree fns
, tree first_arg
,
3913 const vec
<tree
, va_gc
> *args
, tree totype
,
3914 tree explicit_targs
, bool template_only
,
3915 tree conversion_path
, tree access_path
,
3917 struct z_candidate
**candidates
,
3918 tsubst_flags_t complain
)
3920 gcc_assert (*candidates
== NULL
);
3922 /* We're looking for a ctor for list-initialization. */
3923 flags
|= LOOKUP_LIST_INIT_CTOR
;
3924 /* And we don't allow narrowing conversions. We also use this flag to
3925 avoid the copy constructor call for copy-list-initialization. */
3926 flags
|= LOOKUP_NO_NARROWING
;
3928 unsigned nart
= num_artificial_parms_for (OVL_FIRST (fns
)) - 1;
3929 tree init_list
= (*args
)[nart
];
3931 /* Always use the default constructor if the list is empty (DR 990). */
3932 if (CONSTRUCTOR_NELTS (init_list
) == 0
3933 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype
))
3935 /* If the class has a list ctor, try passing the list as a single
3936 argument first, but only consider list ctors. */
3937 else if (TYPE_HAS_LIST_CTOR (totype
))
3939 flags
|= LOOKUP_LIST_ONLY
;
3940 add_candidates (fns
, first_arg
, args
, NULL_TREE
,
3941 explicit_targs
, template_only
, conversion_path
,
3942 access_path
, flags
, candidates
, complain
);
3943 if (any_strictly_viable (*candidates
))
3946 else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list
)
3947 && !CP_AGGREGATE_TYPE_P (totype
))
3949 if (complain
& tf_error
)
3950 error ("designated initializers cannot be used with a "
3951 "non-aggregate type %qT", totype
);
3955 /* Expand the CONSTRUCTOR into a new argument vec. */
3956 vec
<tree
, va_gc
> *new_args
;
3957 vec_alloc (new_args
, nart
+ CONSTRUCTOR_NELTS (init_list
));
3958 for (unsigned i
= 0; i
< nart
; ++i
)
3959 new_args
->quick_push ((*args
)[i
]);
3960 for (unsigned i
= 0; i
< CONSTRUCTOR_NELTS (init_list
); ++i
)
3961 new_args
->quick_push (CONSTRUCTOR_ELT (init_list
, i
)->value
);
3963 /* We aren't looking for list-ctors anymore. */
3964 flags
&= ~LOOKUP_LIST_ONLY
;
3965 /* We allow more user-defined conversions within an init-list. */
3966 flags
&= ~LOOKUP_NO_CONVERSION
;
3968 add_candidates (fns
, first_arg
, new_args
, NULL_TREE
,
3969 explicit_targs
, template_only
, conversion_path
,
3970 access_path
, flags
, candidates
, complain
);
3973 /* Returns the best overload candidate to perform the requested
3974 conversion. This function is used for three the overloading situations
3975 described in [over.match.copy], [over.match.conv], and [over.match.ref].
3976 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
3977 per [dcl.init.ref], so we ignore temporary bindings. */
3979 static struct z_candidate
*
3980 build_user_type_conversion_1 (tree totype
, tree expr
, int flags
,
3981 tsubst_flags_t complain
)
3983 struct z_candidate
*candidates
, *cand
;
3985 tree ctors
= NULL_TREE
;
3986 tree conv_fns
= NULL_TREE
;
3987 conversion
*conv
= NULL
;
3988 tree first_arg
= NULL_TREE
;
3989 vec
<tree
, va_gc
> *args
= NULL
;
3996 fromtype
= TREE_TYPE (expr
);
3998 /* We represent conversion within a hierarchy using RVALUE_CONV and
3999 BASE_CONV, as specified by [over.best.ics]; these become plain
4000 constructor calls, as specified in [dcl.init]. */
4001 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype
) || !MAYBE_CLASS_TYPE_P (totype
)
4002 || !DERIVED_FROM_P (totype
, fromtype
));
4004 if (CLASS_TYPE_P (totype
))
4005 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4006 creating a garbage BASELINK; constructors can't be inherited. */
4007 ctors
= get_class_binding (totype
, complete_ctor_identifier
);
4009 if (MAYBE_CLASS_TYPE_P (fromtype
))
4011 tree to_nonref
= non_reference (totype
);
4012 if (same_type_ignoring_top_level_qualifiers_p (to_nonref
, fromtype
) ||
4013 (CLASS_TYPE_P (to_nonref
) && CLASS_TYPE_P (fromtype
)
4014 && DERIVED_FROM_P (to_nonref
, fromtype
)))
4016 /* [class.conv.fct] A conversion function is never used to
4017 convert a (possibly cv-qualified) object to the (possibly
4018 cv-qualified) same object type (or a reference to it), to a
4019 (possibly cv-qualified) base class of that type (or a
4020 reference to it)... */
4023 conv_fns
= lookup_conversions (fromtype
);
4027 flags
|= LOOKUP_NO_CONVERSION
;
4028 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
4029 flags
|= LOOKUP_NO_NARROWING
;
4031 /* It's OK to bind a temporary for converting constructor arguments, but
4032 not in converting the return value of a conversion operator. */
4033 convflags
= ((flags
& LOOKUP_NO_TEMP_BIND
) | LOOKUP_NO_CONVERSION
4034 | (flags
& LOOKUP_NO_NARROWING
));
4035 flags
&= ~LOOKUP_NO_TEMP_BIND
;
4039 int ctorflags
= flags
;
4041 first_arg
= build_dummy_object (totype
);
4043 /* We should never try to call the abstract or base constructor
4045 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors
))
4046 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors
)));
4048 args
= make_tree_vector_single (expr
);
4049 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
4051 /* List-initialization. */
4052 add_list_candidates (ctors
, first_arg
, args
, totype
, NULL_TREE
,
4053 false, TYPE_BINFO (totype
), TYPE_BINFO (totype
),
4054 ctorflags
, &candidates
, complain
);
4058 add_candidates (ctors
, first_arg
, args
, NULL_TREE
, NULL_TREE
, false,
4059 TYPE_BINFO (totype
), TYPE_BINFO (totype
),
4060 ctorflags
, &candidates
, complain
);
4063 for (cand
= candidates
; cand
; cand
= cand
->next
)
4065 cand
->second_conv
= build_identity_conv (totype
, NULL_TREE
);
4067 /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
4068 set, then this is copy-initialization. In that case, "The
4069 result of the call is then used to direct-initialize the
4070 object that is the destination of the copy-initialization."
4073 We represent this in the conversion sequence with an
4074 rvalue conversion, which means a constructor call. */
4075 if (!TYPE_REF_P (totype
)
4076 && !(convflags
& LOOKUP_NO_TEMP_BIND
))
4078 = build_conv (ck_rvalue
, totype
, cand
->second_conv
);
4084 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
4085 first_arg
= CONSTRUCTOR_ELT (expr
, 0)->value
;
4090 for (; conv_fns
; conv_fns
= TREE_CHAIN (conv_fns
))
4092 tree conversion_path
= TREE_PURPOSE (conv_fns
);
4093 struct z_candidate
*old_candidates
;
4095 /* If we are called to convert to a reference type, we are trying to
4096 find a direct binding, so don't even consider temporaries. If
4097 we don't find a direct binding, the caller will try again to
4098 look for a temporary binding. */
4099 if (TYPE_REF_P (totype
))
4100 convflags
|= LOOKUP_NO_TEMP_BIND
;
4102 old_candidates
= candidates
;
4103 add_candidates (TREE_VALUE (conv_fns
), first_arg
, NULL
, totype
,
4105 conversion_path
, TYPE_BINFO (fromtype
),
4106 flags
, &candidates
, complain
);
4108 for (cand
= candidates
; cand
!= old_candidates
; cand
= cand
->next
)
4110 if (cand
->viable
== 0)
4111 /* Already rejected, don't change to -1. */
4114 tree rettype
= TREE_TYPE (TREE_TYPE (cand
->fn
));
4116 = implicit_conversion (totype
,
4119 /*c_cast_p=*/false, convflags
,
4122 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4123 copy-initialization. In that case, "The result of the
4124 call is then used to direct-initialize the object that is
4125 the destination of the copy-initialization." [dcl.init]
4127 We represent this in the conversion sequence with an
4128 rvalue conversion, which means a constructor call. But
4129 don't add a second rvalue conversion if there's already
4130 one there. Which there really shouldn't be, but it's
4131 harmless since we'd add it here anyway. */
4132 if (ics
&& MAYBE_CLASS_TYPE_P (totype
) && ics
->kind
!= ck_rvalue
4133 && !(convflags
& LOOKUP_NO_TEMP_BIND
))
4134 ics
= build_conv (ck_rvalue
, totype
, ics
);
4136 cand
->second_conv
= ics
;
4141 cand
->reason
= arg_conversion_rejection (NULL_TREE
, -2,
4143 EXPR_LOCATION (expr
));
4145 else if (TYPE_REF_P (totype
) && !ics
->rvaluedness_matches_p
4146 /* Limit this to non-templates for now (PR90546). */
4147 && !cand
->template_decl
4148 && TREE_CODE (TREE_TYPE (totype
)) != FUNCTION_TYPE
)
4150 /* If we are called to convert to a reference type, we are trying
4151 to find a direct binding per [over.match.ref], so rvaluedness
4152 must match for non-functions. */
4155 else if (DECL_NONCONVERTING_P (cand
->fn
)
4156 && ics
->rank
> cr_exact
)
4158 /* 13.3.1.5: For direct-initialization, those explicit
4159 conversion functions that are not hidden within S and
4160 yield type T or a type that can be converted to type T
4161 with a qualification conversion (4.4) are also candidate
4163 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4164 I've raised this issue with the committee. --jason 9/2011 */
4166 cand
->reason
= explicit_conversion_rejection (rettype
, totype
);
4168 else if (cand
->viable
== 1 && ics
->bad_p
)
4172 = bad_arg_conversion_rejection (NULL_TREE
, -2,
4174 EXPR_LOCATION (expr
));
4176 else if (primary_template_specialization_p (cand
->fn
)
4177 && ics
->rank
> cr_exact
)
4179 /* 13.3.3.1.2: If the user-defined conversion is specified by
4180 a specialization of a conversion function template, the
4181 second standard conversion sequence shall have exact match
4184 cand
->reason
= template_conversion_rejection (rettype
, totype
);
4189 candidates
= splice_viable (candidates
, false, &any_viable_p
);
4193 release_tree_vector (args
);
4197 cand
= tourney (candidates
, complain
);
4200 if (complain
& tf_error
)
4202 auto_diagnostic_group d
;
4203 error_at (cp_expr_loc_or_input_loc (expr
),
4204 "conversion from %qH to %qI is ambiguous",
4206 print_z_candidates (location_of (expr
), candidates
);
4209 cand
= candidates
; /* any one will do */
4210 cand
->second_conv
= build_ambiguous_conv (totype
, expr
);
4211 cand
->second_conv
->user_conv_p
= true;
4212 if (!any_strictly_viable (candidates
))
4213 cand
->second_conv
->bad_p
= true;
4214 if (flags
& LOOKUP_ONLYCONVERTING
)
4215 cand
->second_conv
->need_temporary_p
= true;
4216 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4217 ambiguous conversion is no worse than another user-defined
4224 if (!DECL_CONSTRUCTOR_P (cand
->fn
))
4225 convtype
= non_reference (TREE_TYPE (TREE_TYPE (cand
->fn
)));
4226 else if (cand
->second_conv
->kind
== ck_rvalue
)
4227 /* DR 5: [in the first step of copy-initialization]...if the function
4228 is a constructor, the call initializes a temporary of the
4229 cv-unqualified version of the destination type. */
4230 convtype
= cv_unqualified (totype
);
4233 /* Build the user conversion sequence. */
4237 build_identity_conv (TREE_TYPE (expr
), expr
));
4239 if (cand
->viable
== -1)
4242 /* We're performing the maybe-rvalue overload resolution and
4243 a conversion function is in play. Reject converting the return
4244 value of the conversion function to a base class. */
4245 if ((flags
& LOOKUP_PREFER_RVALUE
) && !DECL_CONSTRUCTOR_P (cand
->fn
))
4246 for (conversion
*t
= cand
->second_conv
; t
; t
= next_conversion (t
))
4247 if (t
->kind
== ck_base
)
4250 /* Remember that this was a list-initialization. */
4251 if (flags
& LOOKUP_NO_NARROWING
)
4252 conv
->check_narrowing
= true;
4254 /* Combine it with the second conversion sequence. */
4255 cand
->second_conv
= merge_conversion_sequences (conv
,
4261 /* Wrapper for above. */
4264 build_user_type_conversion (tree totype
, tree expr
, int flags
,
4265 tsubst_flags_t complain
)
4267 struct z_candidate
*cand
;
4270 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
4271 cand
= build_user_type_conversion_1 (totype
, expr
, flags
, complain
);
4275 if (cand
->second_conv
->kind
== ck_ambig
)
4276 ret
= error_mark_node
;
4279 expr
= convert_like (cand
->second_conv
, expr
, complain
);
4280 ret
= convert_from_reference (expr
);
4286 timevar_cond_stop (TV_OVERLOAD
, subtime
);
4290 /* Give a helpful diagnostic when implicit_conversion fails. */
4293 implicit_conversion_error (location_t loc
, tree type
, tree expr
)
4295 tsubst_flags_t complain
= tf_warning_or_error
;
4297 /* If expr has unknown type, then it is an overloaded function.
4298 Call instantiate_type to get good error messages. */
4299 if (TREE_TYPE (expr
) == unknown_type_node
)
4300 instantiate_type (type
, expr
, complain
);
4301 else if (invalid_nonstatic_memfn_p (loc
, expr
, complain
))
4302 /* We gave an error. */;
4303 else if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
4304 && CONSTRUCTOR_IS_DESIGNATED_INIT (expr
)
4305 && !CP_AGGREGATE_TYPE_P (type
))
4306 error_at (loc
, "designated initializers cannot be used with a "
4307 "non-aggregate type %qT", type
);
4310 range_label_for_type_mismatch
label (TREE_TYPE (expr
), type
);
4311 gcc_rich_location
rich_loc (loc
, &label
);
4312 error_at (&rich_loc
, "could not convert %qE from %qH to %qI",
4313 expr
, TREE_TYPE (expr
), type
);
4317 /* Worker for build_converted_constant_expr. */
4320 build_converted_constant_expr_internal (tree type
, tree expr
,
4321 int flags
, tsubst_flags_t complain
)
4326 location_t loc
= cp_expr_loc_or_input_loc (expr
);
4328 if (error_operand_p (expr
))
4329 return error_mark_node
;
4331 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4332 p
= conversion_obstack_alloc (0);
4334 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
4335 /*c_cast_p=*/false, flags
, complain
);
4337 /* A converted constant expression of type T is an expression, implicitly
4338 converted to type T, where the converted expression is a constant
4339 expression and the implicit conversion sequence contains only
4341 * user-defined conversions,
4342 * lvalue-to-rvalue conversions (7.1),
4343 * array-to-pointer conversions (7.2),
4344 * function-to-pointer conversions (7.3),
4345 * qualification conversions (7.5),
4346 * integral promotions (7.6),
4347 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4348 * null pointer conversions (7.11) from std::nullptr_t,
4349 * null member pointer conversions (7.12) from std::nullptr_t, and
4350 * function pointer conversions (7.13),
4352 and where the reference binding (if any) binds directly. */
4354 for (conversion
*c
= conv
;
4355 c
&& c
->kind
!= ck_identity
;
4356 c
= next_conversion (c
))
4360 /* A conversion function is OK. If it isn't constexpr, we'll
4361 complain later that the argument isn't constant. */
4363 /* List-initialization is OK. */
4365 /* The lvalue-to-rvalue conversion is OK. */
4367 /* Array-to-pointer and function-to-pointer. */
4369 /* Function pointer conversions. */
4371 /* Qualification conversions. */
4376 if (c
->need_temporary_p
)
4378 if (complain
& tf_error
)
4379 error_at (loc
, "initializing %qH with %qI in converted "
4380 "constant expression does not bind directly",
4381 type
, next_conversion (c
)->type
);
4390 t
= next_conversion (c
)->type
;
4391 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t
)
4392 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4393 /* Integral promotion or conversion. */
4395 if (NULLPTR_TYPE_P (t
))
4396 /* Conversion from nullptr to pointer or pointer-to-member. */
4399 if (complain
& tf_error
)
4400 error_at (loc
, "conversion from %qH to %qI in a "
4401 "converted constant expression", t
, type
);
4410 /* Avoid confusing convert_nontype_argument by introducing
4411 a redundant conversion to the same reference type. */
4412 if (conv
&& conv
->kind
== ck_ref_bind
4413 && REFERENCE_REF_P (expr
))
4415 tree ref
= TREE_OPERAND (expr
, 0);
4416 if (same_type_p (type
, TREE_TYPE (ref
)))
4422 /* Don't copy a class in a template. */
4423 if (CLASS_TYPE_P (type
) && conv
->kind
== ck_rvalue
4424 && processing_template_decl
)
4425 conv
= next_conversion (conv
);
4427 conv
->check_narrowing
= true;
4428 conv
->check_narrowing_const_only
= true;
4429 expr
= convert_like (conv
, expr
, complain
);
4433 if (complain
& tf_error
)
4434 implicit_conversion_error (loc
, type
, expr
);
4435 expr
= error_mark_node
;
4438 /* Free all the conversions we allocated. */
4439 obstack_free (&conversion_obstack
, p
);
4444 /* Subroutine of convert_nontype_argument.
4446 EXPR is an expression used in a context that requires a converted
4447 constant-expression, such as a template non-type parameter. Do any
4448 necessary conversions (that are permitted for converted
4449 constant-expressions) to convert it to the desired type.
4451 This function doesn't consider explicit conversion functions. If
4452 you mean to use "a contextually converted constant expression of type
4453 bool", use build_converted_constant_bool_expr.
4455 If conversion is successful, returns the converted expression;
4456 otherwise, returns error_mark_node. */
4459 build_converted_constant_expr (tree type
, tree expr
, tsubst_flags_t complain
)
4461 return build_converted_constant_expr_internal (type
, expr
, LOOKUP_IMPLICIT
,
4465 /* Used to create "a contextually converted constant expression of type
4466 bool". This differs from build_converted_constant_expr in that it
4467 also considers explicit conversion functions. */
4470 build_converted_constant_bool_expr (tree expr
, tsubst_flags_t complain
)
4472 return build_converted_constant_expr_internal (boolean_type_node
, expr
,
4473 LOOKUP_NORMAL
, complain
);
4476 /* Do any initial processing on the arguments to a function call. */
4479 resolve_args (vec
<tree
, va_gc
> *args
, tsubst_flags_t complain
)
4484 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
4486 if (error_operand_p (arg
))
4488 else if (VOID_TYPE_P (TREE_TYPE (arg
)))
4490 if (complain
& tf_error
)
4491 error_at (cp_expr_loc_or_input_loc (arg
),
4492 "invalid use of void expression");
4495 else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg
), arg
, complain
))
4501 /* Perform overload resolution on FN, which is called with the ARGS.
4503 Return the candidate function selected by overload resolution, or
4504 NULL if the event that overload resolution failed. In the case
4505 that overload resolution fails, *CANDIDATES will be the set of
4506 candidates considered, and ANY_VIABLE_P will be set to true or
4507 false to indicate whether or not any of the candidates were
4510 The ARGS should already have gone through RESOLVE_ARGS before this
4511 function is called. */
4513 static struct z_candidate
*
4514 perform_overload_resolution (tree fn
,
4515 const vec
<tree
, va_gc
> *args
,
4516 struct z_candidate
**candidates
,
4517 bool *any_viable_p
, tsubst_flags_t complain
)
4519 struct z_candidate
*cand
;
4520 tree explicit_targs
;
4523 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
4525 explicit_targs
= NULL_TREE
;
4529 *any_viable_p
= true;
4532 gcc_assert (OVL_P (fn
) || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
);
4534 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4536 explicit_targs
= TREE_OPERAND (fn
, 1);
4537 fn
= TREE_OPERAND (fn
, 0);
4541 /* Add the various candidate functions. */
4542 add_candidates (fn
, NULL_TREE
, args
, NULL_TREE
,
4543 explicit_targs
, template_only
,
4544 /*conversion_path=*/NULL_TREE
,
4545 /*access_path=*/NULL_TREE
,
4547 candidates
, complain
);
4549 *candidates
= splice_viable (*candidates
, false, any_viable_p
);
4551 cand
= tourney (*candidates
, complain
);
4555 timevar_cond_stop (TV_OVERLOAD
, subtime
);
4559 /* Print an error message about being unable to build a call to FN with
4560 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4561 be located; CANDIDATES is a possibly empty list of such
4565 print_error_for_call_failure (tree fn
, vec
<tree
, va_gc
> *args
,
4566 struct z_candidate
*candidates
)
4568 tree targs
= NULL_TREE
;
4569 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4571 targs
= TREE_OPERAND (fn
, 1);
4572 fn
= TREE_OPERAND (fn
, 0);
4574 tree name
= OVL_NAME (fn
);
4575 location_t loc
= location_of (name
);
4577 name
= lookup_template_function (name
, targs
);
4579 auto_diagnostic_group d
;
4580 if (!any_strictly_viable (candidates
))
4581 error_at (loc
, "no matching function for call to %<%D(%A)%>",
4582 name
, build_tree_list_vec (args
));
4584 error_at (loc
, "call of overloaded %<%D(%A)%> is ambiguous",
4585 name
, build_tree_list_vec (args
));
4587 print_z_candidates (loc
, candidates
);
4590 /* Return an expression for a call to FN (a namespace-scope function,
4591 or a static member function) with the ARGS. This may change
4595 build_new_function_call (tree fn
, vec
<tree
, va_gc
> **args
,
4596 tsubst_flags_t complain
)
4598 struct z_candidate
*candidates
, *cand
;
4603 if (args
!= NULL
&& *args
!= NULL
)
4605 *args
= resolve_args (*args
, complain
);
4607 return error_mark_node
;
4611 tm_malloc_replacement (fn
);
4613 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4614 p
= conversion_obstack_alloc (0);
4616 cand
= perform_overload_resolution (fn
, *args
, &candidates
, &any_viable_p
,
4621 if (complain
& tf_error
)
4623 // If there is a single (non-viable) function candidate,
4624 // let the error be diagnosed by cp_build_function_call_vec.
4625 if (!any_viable_p
&& candidates
&& ! candidates
->next
4626 && (TREE_CODE (candidates
->fn
) == FUNCTION_DECL
))
4627 return cp_build_function_call_vec (candidates
->fn
, args
, complain
);
4629 // Otherwise, emit notes for non-viable candidates.
4630 print_error_for_call_failure (fn
, *args
, candidates
);
4632 result
= error_mark_node
;
4636 result
= build_over_call (cand
, LOOKUP_NORMAL
, complain
);
4641 && TREE_CODE (result
) == CALL_EXPR
4642 && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result
), 0))
4644 result
= coro_validate_builtin_call (result
);
4646 /* Free all the conversions we allocated. */
4647 obstack_free (&conversion_obstack
, p
);
4652 /* Build a call to a global operator new. FNNAME is the name of the
4653 operator (either "operator new" or "operator new[]") and ARGS are
4654 the arguments provided. This may change ARGS. *SIZE points to the
4655 total number of bytes required by the allocation, and is updated if
4656 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4657 be used. If this function determines that no cookie should be
4658 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4659 is not NULL_TREE, it is evaluated before calculating the final
4660 array size, and if it fails, the array size is replaced with
4661 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4662 is non-NULL, it will be set, upon return, to the allocation
4666 build_operator_new_call (tree fnname
, vec
<tree
, va_gc
> **args
,
4667 tree
*size
, tree
*cookie_size
,
4668 tree align_arg
, tree size_check
,
4669 tree
*fn
, tsubst_flags_t complain
)
4671 tree original_size
= *size
;
4673 struct z_candidate
*candidates
;
4674 struct z_candidate
*cand
= NULL
;
4679 /* Set to (size_t)-1 if the size check fails. */
4680 if (size_check
!= NULL_TREE
)
4682 tree errval
= TYPE_MAX_VALUE (sizetype
);
4683 if (cxx_dialect
>= cxx11
&& flag_exceptions
)
4684 errval
= throw_bad_array_new_length ();
4685 *size
= fold_build3 (COND_EXPR
, sizetype
, size_check
,
4686 original_size
, errval
);
4688 vec_safe_insert (*args
, 0, *size
);
4689 *args
= resolve_args (*args
, complain
);
4691 return error_mark_node
;
4697 If this lookup fails to find the name, or if the allocated type
4698 is not a class type, the allocation function's name is looked
4699 up in the global scope.
4701 we disregard block-scope declarations of "operator new". */
4702 fns
= lookup_name (fnname
, LOOK_where::NAMESPACE
);
4703 fns
= lookup_arg_dependent (fnname
, fns
, *args
);
4707 vec
<tree
, va_gc
>* align_args
4708 = vec_copy_and_insert (*args
, align_arg
, 1);
4709 cand
= perform_overload_resolution (fns
, align_args
, &candidates
,
4710 &any_viable_p
, tf_none
);
4713 /* If no aligned allocation function matches, try again without the
4717 /* Figure out what function is being called. */
4719 cand
= perform_overload_resolution (fns
, *args
, &candidates
, &any_viable_p
,
4722 /* If no suitable function could be found, issue an error message
4726 if (complain
& tf_error
)
4727 print_error_for_call_failure (fns
, *args
, candidates
);
4728 return error_mark_node
;
4731 /* If a cookie is required, add some extra space. Whether
4732 or not a cookie is required cannot be determined until
4733 after we know which function was called. */
4736 bool use_cookie
= true;
4739 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (cand
->fn
));
4740 /* Skip the size_t parameter. */
4741 arg_types
= TREE_CHAIN (arg_types
);
4742 /* Check the remaining parameters (if any). */
4744 && TREE_CHAIN (arg_types
) == void_list_node
4745 && same_type_p (TREE_VALUE (arg_types
),
4748 /* If we need a cookie, adjust the number of bytes allocated. */
4751 /* Update the total size. */
4752 *size
= size_binop (PLUS_EXPR
, original_size
, *cookie_size
);
4755 /* Set to (size_t)-1 if the size check fails. */
4756 gcc_assert (size_check
!= NULL_TREE
);
4757 *size
= fold_build3 (COND_EXPR
, sizetype
, size_check
,
4758 *size
, TYPE_MAX_VALUE (sizetype
));
4760 /* Update the argument list to reflect the adjusted size. */
4761 (**args
)[0] = *size
;
4764 *cookie_size
= NULL_TREE
;
4767 /* Tell our caller which function we decided to call. */
4771 /* Build the CALL_EXPR. */
4772 tree ret
= build_over_call (cand
, LOOKUP_NORMAL
, complain
);
4774 /* Set this flag for all callers of this function. In addition to
4775 new-expressions, this is called for allocating coroutine state; treat
4776 that as an implicit new-expression. */
4777 tree call
= extract_call_expr (ret
);
4778 if (TREE_CODE (call
) == CALL_EXPR
)
4779 CALL_FROM_NEW_OR_DELETE_P (call
) = 1;
4784 /* Build a new call to operator(). This may change ARGS. */
4787 build_op_call_1 (tree obj
, vec
<tree
, va_gc
> **args
, tsubst_flags_t complain
)
4789 struct z_candidate
*candidates
= 0, *cand
;
4790 tree fns
, convs
, first_mem_arg
= NULL_TREE
;
4792 tree result
= NULL_TREE
;
4795 obj
= mark_lvalue_use (obj
);
4797 if (error_operand_p (obj
))
4798 return error_mark_node
;
4800 tree type
= TREE_TYPE (obj
);
4802 obj
= prep_operand (obj
);
4804 if (TYPE_PTRMEMFUNC_P (type
))
4806 if (complain
& tf_error
)
4807 /* It's no good looking for an overloaded operator() on a
4808 pointer-to-member-function. */
4809 error ("pointer-to-member function %qE cannot be called without "
4810 "an object; consider using %<.*%> or %<->*%>", obj
);
4811 return error_mark_node
;
4814 if (TYPE_BINFO (type
))
4816 fns
= lookup_fnfields (TYPE_BINFO (type
), call_op_identifier
, 1, complain
);
4817 if (fns
== error_mark_node
)
4818 return error_mark_node
;
4823 if (args
!= NULL
&& *args
!= NULL
)
4825 *args
= resolve_args (*args
, complain
);
4827 return error_mark_node
;
4830 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4831 p
= conversion_obstack_alloc (0);
4835 first_mem_arg
= obj
;
4837 add_candidates (BASELINK_FUNCTIONS (fns
),
4838 first_mem_arg
, *args
, NULL_TREE
,
4840 BASELINK_BINFO (fns
), BASELINK_ACCESS_BINFO (fns
),
4841 LOOKUP_NORMAL
, &candidates
, complain
);
4844 convs
= lookup_conversions (type
);
4846 for (; convs
; convs
= TREE_CHAIN (convs
))
4848 tree totype
= TREE_TYPE (convs
);
4850 if (TYPE_PTRFN_P (totype
)
4851 || TYPE_REFFN_P (totype
)
4852 || (TYPE_REF_P (totype
)
4853 && TYPE_PTRFN_P (TREE_TYPE (totype
))))
4854 for (ovl_iterator
iter (TREE_VALUE (convs
)); iter
; ++iter
)
4858 if (DECL_NONCONVERTING_P (fn
))
4861 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4862 add_template_conv_candidate
4863 (&candidates
, fn
, obj
, *args
, totype
,
4864 /*access_path=*/NULL_TREE
,
4865 /*conversion_path=*/NULL_TREE
, complain
);
4867 add_conv_candidate (&candidates
, fn
, obj
,
4868 *args
, /*conversion_path=*/NULL_TREE
,
4869 /*access_path=*/NULL_TREE
, complain
);
4873 /* Be strict here because if we choose a bad conversion candidate, the
4874 errors we get won't mention the call context. */
4875 candidates
= splice_viable (candidates
, true, &any_viable_p
);
4878 if (complain
& tf_error
)
4880 auto_diagnostic_group d
;
4881 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj
),
4882 build_tree_list_vec (*args
));
4883 print_z_candidates (location_of (TREE_TYPE (obj
)), candidates
);
4885 result
= error_mark_node
;
4889 cand
= tourney (candidates
, complain
);
4892 if (complain
& tf_error
)
4894 auto_diagnostic_group d
;
4895 error ("call of %<(%T) (%A)%> is ambiguous",
4896 TREE_TYPE (obj
), build_tree_list_vec (*args
));
4897 print_z_candidates (location_of (TREE_TYPE (obj
)), candidates
);
4899 result
= error_mark_node
;
4901 else if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
4902 && DECL_OVERLOADED_OPERATOR_P (cand
->fn
)
4903 && DECL_OVERLOADED_OPERATOR_IS (cand
->fn
, CALL_EXPR
))
4904 result
= build_over_call (cand
, LOOKUP_NORMAL
, complain
);
4907 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
4908 obj
= convert_like_with_context (cand
->convs
[0], obj
, cand
->fn
,
4912 gcc_checking_assert (TYPE_P (cand
->fn
));
4913 obj
= convert_like (cand
->convs
[0], obj
, complain
);
4915 obj
= convert_from_reference (obj
);
4916 result
= cp_build_function_call_vec (obj
, args
, complain
);
4920 /* Free all the conversions we allocated. */
4921 obstack_free (&conversion_obstack
, p
);
4926 /* Wrapper for above. */
4929 build_op_call (tree obj
, vec
<tree
, va_gc
> **args
, tsubst_flags_t complain
)
4932 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
4933 ret
= build_op_call_1 (obj
, args
, complain
);
4934 timevar_cond_stop (TV_OVERLOAD
, subtime
);
4938 /* Called by op_error to prepare format strings suitable for the error
4939 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
4940 and a suffix (controlled by NTYPES). */
4943 op_error_string (const char *errmsg
, int ntypes
, bool match
)
4947 const char *msgp
= concat (match
? G_("ambiguous overload for ")
4948 : G_("no match for "), errmsg
, NULL
);
4951 msg
= concat (msgp
, G_(" (operand types are %qT, %qT, and %qT)"), NULL
);
4952 else if (ntypes
== 2)
4953 msg
= concat (msgp
, G_(" (operand types are %qT and %qT)"), NULL
);
4955 msg
= concat (msgp
, G_(" (operand type is %qT)"), NULL
);
4961 op_error (const op_location_t
&loc
,
4962 enum tree_code code
, enum tree_code code2
,
4963 tree arg1
, tree arg2
, tree arg3
, bool match
)
4965 bool assop
= code
== MODIFY_EXPR
;
4966 const char *opname
= OVL_OP_INFO (assop
, assop
? code2
: code
)->name
;
4971 if (flag_diagnostics_show_caret
)
4972 error_at (loc
, op_error_string (G_("ternary %<operator?:%>"),
4974 TREE_TYPE (arg1
), TREE_TYPE (arg2
), TREE_TYPE (arg3
));
4976 error_at (loc
, op_error_string (G_("ternary %<operator?:%> "
4977 "in %<%E ? %E : %E%>"), 3, match
),
4979 TREE_TYPE (arg1
), TREE_TYPE (arg2
), TREE_TYPE (arg3
));
4982 case POSTINCREMENT_EXPR
:
4983 case POSTDECREMENT_EXPR
:
4984 if (flag_diagnostics_show_caret
)
4985 error_at (loc
, op_error_string (G_("%<operator%s%>"), 1, match
),
4986 opname
, TREE_TYPE (arg1
));
4988 error_at (loc
, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
4990 opname
, arg1
, opname
, TREE_TYPE (arg1
));
4994 if (flag_diagnostics_show_caret
)
4995 error_at (loc
, op_error_string (G_("%<operator[]%>"), 2, match
),
4996 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
4998 error_at (loc
, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5000 arg1
, arg2
, TREE_TYPE (arg1
), TREE_TYPE (arg2
));
5005 if (flag_diagnostics_show_caret
)
5006 error_at (loc
, op_error_string (G_("%qs"), 1, match
),
5007 opname
, TREE_TYPE (arg1
));
5009 error_at (loc
, op_error_string (G_("%qs in %<%s %E%>"), 1, match
),
5010 opname
, opname
, arg1
, TREE_TYPE (arg1
));
5014 if (flag_diagnostics_show_caret
)
5015 error_at (loc
, op_error_string (G_("%<operator %s%>"), 1, match
),
5016 opname
, TREE_TYPE (arg1
));
5018 error_at (loc
, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5020 opname
, opname
, arg1
, TREE_TYPE (arg1
));
5025 if (flag_diagnostics_show_caret
)
5027 binary_op_rich_location
richloc (loc
, arg1
, arg2
, true);
5029 op_error_string (G_("%<operator%s%>"), 2, match
),
5030 opname
, TREE_TYPE (arg1
), TREE_TYPE (arg2
));
5033 error_at (loc
, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5035 opname
, arg1
, opname
, arg2
,
5036 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
5038 if (flag_diagnostics_show_caret
)
5039 error_at (loc
, op_error_string (G_("%<operator%s%>"), 1, match
),
5040 opname
, TREE_TYPE (arg1
));
5042 error_at (loc
, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5044 opname
, opname
, arg1
, TREE_TYPE (arg1
));
5049 /* Return the implicit conversion sequence that could be used to
5050 convert E1 to E2 in [expr.cond]. */
5053 conditional_conversion (tree e1
, tree e2
, tsubst_flags_t complain
)
5055 tree t1
= non_reference (TREE_TYPE (e1
));
5056 tree t2
= non_reference (TREE_TYPE (e2
));
5062 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5063 implicitly converted (clause _conv_) to the type "lvalue reference to
5064 T2", subject to the constraint that in the conversion the
5065 reference must bind directly (_dcl.init.ref_) to an lvalue.
5067 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5068 implicitly converted to the type "rvalue reference to T2", subject to
5069 the constraint that the reference must bind directly. */
5072 tree rtype
= cp_build_reference_type (t2
, !lvalue_p (e2
));
5073 conv
= implicit_conversion (rtype
,
5077 LOOKUP_NO_TEMP_BIND
|LOOKUP_NO_RVAL_BIND
5078 |LOOKUP_ONLYCONVERTING
,
5080 if (conv
&& !conv
->bad_p
)
5084 /* If E2 is a prvalue or if neither of the conversions above can be done
5085 and at least one of the operands has (possibly cv-qualified) class
5087 if (!CLASS_TYPE_P (t1
) && !CLASS_TYPE_P (t2
))
5092 If E1 and E2 have class type, and the underlying class types are
5093 the same or one is a base class of the other: E1 can be converted
5094 to match E2 if the class of T2 is the same type as, or a base
5095 class of, the class of T1, and the cv-qualification of T2 is the
5096 same cv-qualification as, or a greater cv-qualification than, the
5097 cv-qualification of T1. If the conversion is applied, E1 is
5098 changed to an rvalue of type T2 that still refers to the original
5099 source class object (or the appropriate subobject thereof). */
5100 if (CLASS_TYPE_P (t1
) && CLASS_TYPE_P (t2
)
5101 && ((good_base
= DERIVED_FROM_P (t2
, t1
)) || DERIVED_FROM_P (t1
, t2
)))
5103 if (good_base
&& at_least_as_qualified_p (t2
, t1
))
5105 conv
= build_identity_conv (t1
, e1
);
5106 if (!same_type_p (TYPE_MAIN_VARIANT (t1
),
5107 TYPE_MAIN_VARIANT (t2
)))
5108 conv
= build_conv (ck_base
, t2
, conv
);
5110 conv
= build_conv (ck_rvalue
, t2
, conv
);
5119 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5120 converted to the type that expression E2 would have if E2 were
5121 converted to an rvalue (or the type it has, if E2 is an rvalue). */
5122 return implicit_conversion (t2
, t1
, e1
, /*c_cast_p=*/false,
5123 LOOKUP_IMPLICIT
, complain
);
5126 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5127 arguments to the conditional expression. */
5130 build_conditional_expr_1 (const op_location_t
&loc
,
5131 tree arg1
, tree arg2
, tree arg3
,
5132 tsubst_flags_t complain
)
5136 tree result
= NULL_TREE
;
5137 tree result_type
= NULL_TREE
;
5138 bool is_glvalue
= true;
5139 struct z_candidate
*candidates
= 0;
5140 struct z_candidate
*cand
;
5142 tree orig_arg2
, orig_arg3
;
5144 /* As a G++ extension, the second argument to the conditional can be
5145 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5146 c'.) If the second operand is omitted, make sure it is
5147 calculated only once. */
5150 if (complain
& tf_error
)
5151 pedwarn (loc
, OPT_Wpedantic
,
5152 "ISO C++ forbids omitting the middle term of "
5153 "a %<?:%> expression");
5155 if ((complain
& tf_warning
) && !truth_value_p (TREE_CODE (arg1
)))
5156 warn_for_omitted_condop (loc
, arg1
);
5158 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5159 if (glvalue_p (arg1
))
5161 arg1
= cp_stabilize_reference (arg1
);
5162 arg2
= arg1
= prevent_lifetime_extension (arg1
);
5165 arg2
= arg1
= cp_save_expr (arg1
);
5168 /* If something has already gone wrong, just pass that fact up the
5170 if (error_operand_p (arg1
)
5171 || error_operand_p (arg2
)
5172 || error_operand_p (arg3
))
5173 return error_mark_node
;
5178 if (gnu_vector_type_p (TREE_TYPE (arg1
))
5179 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1
)))
5181 tree arg1_type
= TREE_TYPE (arg1
);
5183 /* If arg1 is another cond_expr choosing between -1 and 0,
5184 then we can use its comparison. It may help to avoid
5185 additional comparison, produce more accurate diagnostics
5186 and enables folding. */
5187 if (TREE_CODE (arg1
) == VEC_COND_EXPR
5188 && integer_minus_onep (TREE_OPERAND (arg1
, 1))
5189 && integer_zerop (TREE_OPERAND (arg1
, 2)))
5190 arg1
= TREE_OPERAND (arg1
, 0);
5192 arg1
= force_rvalue (arg1
, complain
);
5193 arg2
= force_rvalue (arg2
, complain
);
5194 arg3
= force_rvalue (arg3
, complain
);
5196 /* force_rvalue can return error_mark on valid arguments. */
5197 if (error_operand_p (arg1
)
5198 || error_operand_p (arg2
)
5199 || error_operand_p (arg3
))
5200 return error_mark_node
;
5202 arg2_type
= TREE_TYPE (arg2
);
5203 arg3_type
= TREE_TYPE (arg3
);
5205 if (!VECTOR_TYPE_P (arg2_type
)
5206 && !VECTOR_TYPE_P (arg3_type
))
5208 /* Rely on the error messages of the scalar version. */
5209 tree scal
= build_conditional_expr_1 (loc
, integer_one_node
,
5210 orig_arg2
, orig_arg3
, complain
);
5211 if (scal
== error_mark_node
)
5212 return error_mark_node
;
5213 tree stype
= TREE_TYPE (scal
);
5214 tree ctype
= TREE_TYPE (arg1_type
);
5215 if (TYPE_SIZE (stype
) != TYPE_SIZE (ctype
)
5216 || (!INTEGRAL_TYPE_P (stype
) && !SCALAR_FLOAT_TYPE_P (stype
)))
5218 if (complain
& tf_error
)
5219 error_at (loc
, "inferred scalar type %qT is not an integer or "
5220 "floating-point type of the same size as %qT", stype
,
5221 COMPARISON_CLASS_P (arg1
)
5222 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
5224 return error_mark_node
;
5227 tree vtype
= build_opaque_vector_type (stype
,
5228 TYPE_VECTOR_SUBPARTS (arg1_type
));
5229 /* We could pass complain & tf_warning to unsafe_conversion_p,
5230 but the warnings (like Wsign-conversion) have already been
5231 given by the scalar build_conditional_expr_1. We still check
5232 unsafe_conversion_p to forbid truncating long long -> float. */
5233 if (unsafe_conversion_p (stype
, arg2
, NULL_TREE
, false))
5235 if (complain
& tf_error
)
5236 error_at (loc
, "conversion of scalar %qH to vector %qI "
5237 "involves truncation", arg2_type
, vtype
);
5238 return error_mark_node
;
5240 if (unsafe_conversion_p (stype
, arg3
, NULL_TREE
, false))
5242 if (complain
& tf_error
)
5243 error_at (loc
, "conversion of scalar %qH to vector %qI "
5244 "involves truncation", arg3_type
, vtype
);
5245 return error_mark_node
;
5248 arg2
= cp_convert (stype
, arg2
, complain
);
5249 arg2
= save_expr (arg2
);
5250 arg2
= build_vector_from_val (vtype
, arg2
);
5252 arg3
= cp_convert (stype
, arg3
, complain
);
5253 arg3
= save_expr (arg3
);
5254 arg3
= build_vector_from_val (vtype
, arg3
);
5258 if ((gnu_vector_type_p (arg2_type
) && !VECTOR_TYPE_P (arg3_type
))
5259 || (gnu_vector_type_p (arg3_type
) && !VECTOR_TYPE_P (arg2_type
)))
5261 enum stv_conv convert_flag
=
5262 scalar_to_vector (loc
, VEC_COND_EXPR
, arg2
, arg3
,
5263 complain
& tf_error
);
5265 switch (convert_flag
)
5268 return error_mark_node
;
5271 arg2
= save_expr (arg2
);
5272 arg2
= convert (TREE_TYPE (arg3_type
), arg2
);
5273 arg2
= build_vector_from_val (arg3_type
, arg2
);
5274 arg2_type
= TREE_TYPE (arg2
);
5279 arg3
= save_expr (arg3
);
5280 arg3
= convert (TREE_TYPE (arg2_type
), arg3
);
5281 arg3
= build_vector_from_val (arg2_type
, arg3
);
5282 arg3_type
= TREE_TYPE (arg3
);
5290 if (!gnu_vector_type_p (arg2_type
)
5291 || !gnu_vector_type_p (arg3_type
)
5292 || !same_type_p (arg2_type
, arg3_type
)
5293 || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type
),
5294 TYPE_VECTOR_SUBPARTS (arg2_type
))
5295 || TYPE_SIZE (arg1_type
) != TYPE_SIZE (arg2_type
))
5297 if (complain
& tf_error
)
5299 "incompatible vector types in conditional expression: "
5300 "%qT, %qT and %qT", TREE_TYPE (arg1
),
5301 TREE_TYPE (orig_arg2
), TREE_TYPE (orig_arg3
));
5302 return error_mark_node
;
5305 if (!COMPARISON_CLASS_P (arg1
))
5307 tree cmp_type
= truth_type_for (arg1_type
);
5308 arg1
= build2 (NE_EXPR
, cmp_type
, arg1
, build_zero_cst (arg1_type
));
5310 return build3_loc (loc
, VEC_COND_EXPR
, arg2_type
, arg1
, arg2
, arg3
);
5315 The first expression is implicitly converted to bool (clause
5317 arg1
= perform_implicit_conversion_flags (boolean_type_node
, arg1
, complain
,
5319 if (error_operand_p (arg1
))
5320 return error_mark_node
;
5324 If either the second or the third operand has type (possibly
5325 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
5326 array-to-pointer (_conv.array_), and function-to-pointer
5327 (_conv.func_) standard conversions are performed on the second
5328 and third operands. */
5329 arg2_type
= unlowered_expr_type (arg2
);
5330 arg3_type
= unlowered_expr_type (arg3
);
5331 if (VOID_TYPE_P (arg2_type
) || VOID_TYPE_P (arg3_type
))
5333 /* 'void' won't help in resolving an overloaded expression on the
5334 other side, so require it to resolve by itself. */
5335 if (arg2_type
== unknown_type_node
)
5337 arg2
= resolve_nondeduced_context_or_error (arg2
, complain
);
5338 arg2_type
= TREE_TYPE (arg2
);
5340 if (arg3_type
== unknown_type_node
)
5342 arg3
= resolve_nondeduced_context_or_error (arg3
, complain
);
5343 arg3_type
= TREE_TYPE (arg3
);
5348 One of the following shall hold:
5350 --The second or the third operand (but not both) is a
5351 throw-expression (_except.throw_); the result is of the type
5352 and value category of the other.
5354 --Both the second and the third operands have type void; the
5355 result is of type void and is a prvalue. */
5356 if (TREE_CODE (arg2
) == THROW_EXPR
5357 && TREE_CODE (arg3
) != THROW_EXPR
)
5359 result_type
= arg3_type
;
5360 is_glvalue
= glvalue_p (arg3
);
5362 else if (TREE_CODE (arg2
) != THROW_EXPR
5363 && TREE_CODE (arg3
) == THROW_EXPR
)
5365 result_type
= arg2_type
;
5366 is_glvalue
= glvalue_p (arg2
);
5368 else if (VOID_TYPE_P (arg2_type
) && VOID_TYPE_P (arg3_type
))
5370 result_type
= void_type_node
;
5375 if (complain
& tf_error
)
5377 if (VOID_TYPE_P (arg2_type
))
5378 error_at (cp_expr_loc_or_loc (arg3
, loc
),
5379 "second operand to the conditional operator "
5380 "is of type %<void%>, but the third operand is "
5381 "neither a throw-expression nor of type %<void%>");
5383 error_at (cp_expr_loc_or_loc (arg2
, loc
),
5384 "third operand to the conditional operator "
5385 "is of type %<void%>, but the second operand is "
5386 "neither a throw-expression nor of type %<void%>");
5388 return error_mark_node
;
5391 goto valid_operands
;
5395 Otherwise, if the second and third operand have different types,
5396 and either has (possibly cv-qualified) class type, or if both are
5397 glvalues of the same value category and the same type except for
5398 cv-qualification, an attempt is made to convert each of those operands
5399 to the type of the other. */
5400 else if (!same_type_p (arg2_type
, arg3_type
)
5401 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)
5402 || (same_type_ignoring_top_level_qualifiers_p (arg2_type
,
5404 && glvalue_p (arg2
) && glvalue_p (arg3
)
5405 && lvalue_p (arg2
) == lvalue_p (arg3
))))
5409 bool converted
= false;
5411 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5412 p
= conversion_obstack_alloc (0);
5414 conv2
= conditional_conversion (arg2
, arg3
, complain
);
5415 conv3
= conditional_conversion (arg3
, arg2
, complain
);
5419 If both can be converted, or one can be converted but the
5420 conversion is ambiguous, the program is ill-formed. If
5421 neither can be converted, the operands are left unchanged and
5422 further checking is performed as described below. If exactly
5423 one conversion is possible, that conversion is applied to the
5424 chosen operand and the converted operand is used in place of
5425 the original operand for the remainder of this section. */
5426 if ((conv2
&& !conv2
->bad_p
5427 && conv3
&& !conv3
->bad_p
)
5428 || (conv2
&& conv2
->kind
== ck_ambig
)
5429 || (conv3
&& conv3
->kind
== ck_ambig
))
5431 if (complain
& tf_error
)
5433 error_at (loc
, "operands to %<?:%> have different types "
5435 arg2_type
, arg3_type
);
5436 if (conv2
&& !conv2
->bad_p
&& conv3
&& !conv3
->bad_p
)
5437 inform (loc
, " and each type can be converted to the other");
5438 else if (conv2
&& conv2
->kind
== ck_ambig
)
5439 convert_like (conv2
, arg2
, complain
);
5441 convert_like (conv3
, arg3
, complain
);
5443 result
= error_mark_node
;
5445 else if (conv2
&& !conv2
->bad_p
)
5447 arg2
= convert_like (conv2
, arg2
, complain
);
5448 arg2
= convert_from_reference (arg2
);
5449 arg2_type
= TREE_TYPE (arg2
);
5450 /* Even if CONV2 is a valid conversion, the result of the
5451 conversion may be invalid. For example, if ARG3 has type
5452 "volatile X", and X does not have a copy constructor
5453 accepting a "volatile X&", then even if ARG2 can be
5454 converted to X, the conversion will fail. */
5455 if (error_operand_p (arg2
))
5456 result
= error_mark_node
;
5459 else if (conv3
&& !conv3
->bad_p
)
5461 arg3
= convert_like (conv3
, arg3
, complain
);
5462 arg3
= convert_from_reference (arg3
);
5463 arg3_type
= TREE_TYPE (arg3
);
5464 if (error_operand_p (arg3
))
5465 result
= error_mark_node
;
5469 /* Free all the conversions we allocated. */
5470 obstack_free (&conversion_obstack
, p
);
5475 /* If, after the conversion, both operands have class type,
5476 treat the cv-qualification of both operands as if it were the
5477 union of the cv-qualification of the operands.
5479 The standard is not clear about what to do in this
5480 circumstance. For example, if the first operand has type
5481 "const X" and the second operand has a user-defined
5482 conversion to "volatile X", what is the type of the second
5483 operand after this step? Making it be "const X" (matching
5484 the first operand) seems wrong, as that discards the
5485 qualification without actually performing a copy. Leaving it
5486 as "volatile X" seems wrong as that will result in the
5487 conditional expression failing altogether, even though,
5488 according to this step, the one operand could be converted to
5489 the type of the other. */
5491 && CLASS_TYPE_P (arg2_type
)
5492 && cp_type_quals (arg2_type
) != cp_type_quals (arg3_type
))
5493 arg2_type
= arg3_type
=
5494 cp_build_qualified_type (arg2_type
,
5495 cp_type_quals (arg2_type
)
5496 | cp_type_quals (arg3_type
));
5501 If the second and third operands are glvalues of the same value
5502 category and have the same type, the result is of that type and
5504 if (((lvalue_p (arg2
) && lvalue_p (arg3
))
5505 || (xvalue_p (arg2
) && xvalue_p (arg3
)))
5506 && same_type_p (arg2_type
, arg3_type
))
5508 result_type
= arg2_type
;
5509 arg2
= mark_lvalue_use (arg2
);
5510 arg3
= mark_lvalue_use (arg3
);
5511 goto valid_operands
;
5516 Otherwise, the result is an rvalue. If the second and third
5517 operand do not have the same type, and either has (possibly
5518 cv-qualified) class type, overload resolution is used to
5519 determine the conversions (if any) to be applied to the operands
5520 (_over.match.oper_, _over.built_). */
5522 if (!same_type_p (arg2_type
, arg3_type
)
5523 && (CLASS_TYPE_P (arg2_type
) || CLASS_TYPE_P (arg3_type
)))
5529 /* Rearrange the arguments so that add_builtin_candidate only has
5530 to know about two args. In build_builtin_candidate, the
5531 arguments are unscrambled. */
5532 args
->quick_push (arg2
);
5533 args
->quick_push (arg3
);
5534 args
->quick_push (arg1
);
5535 add_builtin_candidates (&candidates
,
5538 ovl_op_identifier (false, COND_EXPR
),
5540 LOOKUP_NORMAL
, complain
);
5544 If the overload resolution fails, the program is
5546 candidates
= splice_viable (candidates
, false, &any_viable_p
);
5549 if (complain
& tf_error
)
5550 error_at (loc
, "operands to %<?:%> have different types %qT and %qT",
5551 arg2_type
, arg3_type
);
5552 return error_mark_node
;
5554 cand
= tourney (candidates
, complain
);
5557 if (complain
& tf_error
)
5559 auto_diagnostic_group d
;
5560 op_error (loc
, COND_EXPR
, NOP_EXPR
, arg1
, arg2
, arg3
, FALSE
);
5561 print_z_candidates (loc
, candidates
);
5563 return error_mark_node
;
5568 Otherwise, the conversions thus determined are applied, and
5569 the converted operands are used in place of the original
5570 operands for the remainder of this section. */
5571 conv
= cand
->convs
[0];
5572 arg1
= convert_like (conv
, arg1
, complain
);
5573 conv
= cand
->convs
[1];
5574 arg2
= convert_like (conv
, arg2
, complain
);
5575 arg2_type
= TREE_TYPE (arg2
);
5576 conv
= cand
->convs
[2];
5577 arg3
= convert_like (conv
, arg3
, complain
);
5578 arg3_type
= TREE_TYPE (arg3
);
5583 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5584 and function-to-pointer (_conv.func_) standard conversions are
5585 performed on the second and third operands.
5587 We need to force the lvalue-to-rvalue conversion here for class types,
5588 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5589 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5592 arg2
= force_rvalue (arg2
, complain
);
5593 if (!CLASS_TYPE_P (arg2_type
))
5594 arg2_type
= TREE_TYPE (arg2
);
5596 arg3
= force_rvalue (arg3
, complain
);
5597 if (!CLASS_TYPE_P (arg3_type
))
5598 arg3_type
= TREE_TYPE (arg3
);
5600 if (arg2
== error_mark_node
|| arg3
== error_mark_node
)
5601 return error_mark_node
;
5605 After those conversions, one of the following shall hold:
5607 --The second and third operands have the same type; the result is of
5609 if (same_type_p (arg2_type
, arg3_type
))
5610 result_type
= arg2_type
;
5613 --The second and third operands have arithmetic or enumeration
5614 type; the usual arithmetic conversions are performed to bring
5615 them to a common type, and the result is of that type. */
5616 else if ((ARITHMETIC_TYPE_P (arg2_type
)
5617 || UNSCOPED_ENUM_P (arg2_type
))
5618 && (ARITHMETIC_TYPE_P (arg3_type
)
5619 || UNSCOPED_ENUM_P (arg3_type
)))
5621 /* In this case, there is always a common type. */
5622 result_type
= type_after_usual_arithmetic_conversions (arg2_type
,
5624 if (complain
& tf_warning
)
5625 do_warn_double_promotion (result_type
, arg2_type
, arg3_type
,
5626 "implicit conversion from %qH to %qI to "
5627 "match other result of conditional",
5630 if (TREE_CODE (arg2_type
) == ENUMERAL_TYPE
5631 && TREE_CODE (arg3_type
) == ENUMERAL_TYPE
)
5633 tree stripped_orig_arg2
= tree_strip_any_location_wrapper (orig_arg2
);
5634 tree stripped_orig_arg3
= tree_strip_any_location_wrapper (orig_arg3
);
5635 if (TREE_CODE (stripped_orig_arg2
) == CONST_DECL
5636 && TREE_CODE (stripped_orig_arg3
) == CONST_DECL
5637 && (DECL_CONTEXT (stripped_orig_arg2
)
5638 == DECL_CONTEXT (stripped_orig_arg3
)))
5639 /* Two enumerators from the same enumeration can have different
5640 types when the enumeration is still being defined. */;
5641 else if (complain
& tf_warning
)
5642 warning_at (loc
, OPT_Wenum_compare
, "enumerated mismatch "
5643 "in conditional expression: %qT vs %qT",
5644 arg2_type
, arg3_type
);
5646 else if (extra_warnings
5647 && ((TREE_CODE (arg2_type
) == ENUMERAL_TYPE
5648 && !same_type_p (arg3_type
, type_promotes_to (arg2_type
)))
5649 || (TREE_CODE (arg3_type
) == ENUMERAL_TYPE
5650 && !same_type_p (arg2_type
,
5651 type_promotes_to (arg3_type
)))))
5653 if (complain
& tf_warning
)
5654 warning_at (loc
, OPT_Wextra
, "enumerated and non-enumerated "
5655 "type in conditional expression");
5658 arg2
= perform_implicit_conversion (result_type
, arg2
, complain
);
5659 arg3
= perform_implicit_conversion (result_type
, arg3
, complain
);
5663 --The second and third operands have pointer type, or one has
5664 pointer type and the other is a null pointer constant; pointer
5665 conversions (_conv.ptr_) and qualification conversions
5666 (_conv.qual_) are performed to bring them to their composite
5667 pointer type (_expr.rel_). The result is of the composite
5670 --The second and third operands have pointer to member type, or
5671 one has pointer to member type and the other is a null pointer
5672 constant; pointer to member conversions (_conv.mem_) and
5673 qualification conversions (_conv.qual_) are performed to bring
5674 them to a common type, whose cv-qualification shall match the
5675 cv-qualification of either the second or the third operand.
5676 The result is of the common type. */
5677 else if ((null_ptr_cst_p (arg2
)
5678 && TYPE_PTR_OR_PTRMEM_P (arg3_type
))
5679 || (null_ptr_cst_p (arg3
)
5680 && TYPE_PTR_OR_PTRMEM_P (arg2_type
))
5681 || (TYPE_PTR_P (arg2_type
) && TYPE_PTR_P (arg3_type
))
5682 || (TYPE_PTRDATAMEM_P (arg2_type
) && TYPE_PTRDATAMEM_P (arg3_type
))
5683 || (TYPE_PTRMEMFUNC_P (arg2_type
) && TYPE_PTRMEMFUNC_P (arg3_type
)))
5685 result_type
= composite_pointer_type (loc
,
5686 arg2_type
, arg3_type
, arg2
,
5687 arg3
, CPO_CONDITIONAL_EXPR
,
5689 if (result_type
== error_mark_node
)
5690 return error_mark_node
;
5691 arg2
= perform_implicit_conversion (result_type
, arg2
, complain
);
5692 arg3
= perform_implicit_conversion (result_type
, arg3
, complain
);
5697 if (complain
& tf_error
)
5698 error_at (loc
, "operands to %<?:%> have different types %qT and %qT",
5699 arg2_type
, arg3_type
);
5700 return error_mark_node
;
5703 if (arg2
== error_mark_node
|| arg3
== error_mark_node
)
5704 return error_mark_node
;
5707 if (processing_template_decl
&& is_glvalue
)
5709 /* Let lvalue_kind know this was a glvalue. */
5710 tree arg
= (result_type
== arg2_type
? arg2
: arg3
);
5711 result_type
= cp_build_reference_type (result_type
, xvalue_p (arg
));
5714 result
= build3_loc (loc
, COND_EXPR
, result_type
, arg1
, arg2
, arg3
);
5716 /* If the ARG2 and ARG3 are the same and don't have side-effects,
5717 warn here, because the COND_EXPR will be turned into ARG2. */
5718 if (warn_duplicated_branches
5719 && (complain
& tf_warning
)
5720 && (arg2
== arg3
|| operand_equal_p (arg2
, arg3
, 0)))
5721 warning_at (EXPR_LOCATION (result
), OPT_Wduplicated_branches
,
5722 "this condition has identical branches");
5724 /* We can't use result_type below, as fold might have returned a
5729 /* Expand both sides into the same slot, hopefully the target of
5730 the ?: expression. We used to check for TARGET_EXPRs here,
5731 but now we sometimes wrap them in NOP_EXPRs so the test would
5733 if (CLASS_TYPE_P (TREE_TYPE (result
)))
5734 result
= get_target_expr_sfinae (result
, complain
);
5735 /* If this expression is an rvalue, but might be mistaken for an
5736 lvalue, we must add a NON_LVALUE_EXPR. */
5737 result
= rvalue (result
);
5740 result
= force_paren_expr (result
);
5745 /* Wrapper for above. */
5748 build_conditional_expr (const op_location_t
&loc
,
5749 tree arg1
, tree arg2
, tree arg3
,
5750 tsubst_flags_t complain
)
5753 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
5754 ret
= build_conditional_expr_1 (loc
, arg1
, arg2
, arg3
, complain
);
5755 timevar_cond_stop (TV_OVERLOAD
, subtime
);
5759 /* OPERAND is an operand to an expression. Perform necessary steps
5760 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
5764 prep_operand (tree operand
)
5768 if (CLASS_TYPE_P (TREE_TYPE (operand
))
5769 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand
)))
5770 /* Make sure the template type is instantiated now. */
5771 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand
)));
5777 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
5778 OVERLOAD) to the CANDIDATES, returning an updated list of
5779 CANDIDATES. The ARGS are the arguments provided to the call;
5780 if FIRST_ARG is non-null it is the implicit object argument,
5781 otherwise the first element of ARGS is used if needed. The
5782 EXPLICIT_TARGS are explicit template arguments provided.
5783 TEMPLATE_ONLY is true if only template functions should be
5784 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
5785 add_function_candidate. */
5788 add_candidates (tree fns
, tree first_arg
, const vec
<tree
, va_gc
> *args
,
5790 tree explicit_targs
, bool template_only
,
5791 tree conversion_path
, tree access_path
,
5793 struct z_candidate
**candidates
,
5794 tsubst_flags_t complain
)
5797 const vec
<tree
, va_gc
> *non_static_args
;
5798 bool check_list_ctor
= false;
5799 bool check_converting
= false;
5800 unification_kind_t strict
;
5805 /* Precalculate special handling of constructors and conversion ops. */
5806 tree fn
= OVL_FIRST (fns
);
5807 if (DECL_CONV_FN_P (fn
))
5809 check_list_ctor
= false;
5810 check_converting
= (flags
& LOOKUP_ONLYCONVERTING
) != 0;
5811 if (flags
& LOOKUP_NO_CONVERSION
)
5812 /* We're doing return_type(x). */
5813 strict
= DEDUCE_CONV
;
5815 /* We're doing x.operator return_type(). */
5816 strict
= DEDUCE_EXACT
;
5817 /* [over.match.funcs] For conversion functions, the function
5818 is considered to be a member of the class of the implicit
5819 object argument for the purpose of defining the type of
5820 the implicit object parameter. */
5821 ctype
= TYPE_MAIN_VARIANT (TREE_TYPE (first_arg
));
5825 if (DECL_CONSTRUCTOR_P (fn
))
5827 check_list_ctor
= (flags
& LOOKUP_LIST_ONLY
) != 0;
5828 /* For list-initialization we consider explicit constructors
5829 and complain if one is chosen. */
5831 = ((flags
& (LOOKUP_ONLYCONVERTING
|LOOKUP_LIST_INIT_CTOR
))
5832 == LOOKUP_ONLYCONVERTING
);
5834 strict
= DEDUCE_CALL
;
5835 ctype
= conversion_path
? BINFO_TYPE (conversion_path
) : NULL_TREE
;
5839 non_static_args
= args
;
5841 /* Delay creating the implicit this parameter until it is needed. */
5842 non_static_args
= NULL
;
5844 for (lkp_iterator
iter (fns
); iter
; ++iter
)
5848 if (check_converting
&& DECL_NONCONVERTING_P (fn
))
5850 if (check_list_ctor
&& !is_list_ctor (fn
))
5853 tree fn_first_arg
= NULL_TREE
;
5854 const vec
<tree
, va_gc
> *fn_args
= args
;
5856 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
5858 /* Figure out where the object arg comes from. If this
5859 function is a non-static member and we didn't get an
5860 implicit object argument, move it out of args. */
5861 if (first_arg
== NULL_TREE
)
5865 vec
<tree
, va_gc
> *tempvec
;
5866 vec_alloc (tempvec
, args
->length () - 1);
5867 for (ix
= 1; args
->iterate (ix
, &arg
); ++ix
)
5868 tempvec
->quick_push (arg
);
5869 non_static_args
= tempvec
;
5870 first_arg
= (*args
)[0];
5873 fn_first_arg
= first_arg
;
5874 fn_args
= non_static_args
;
5877 /* Don't bother reversing an operator with two identical parameters. */
5878 else if (vec_safe_length (args
) == 2 && (flags
& LOOKUP_REVERSED
))
5880 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5881 if (same_type_p (TREE_VALUE (parmlist
),
5882 TREE_VALUE (TREE_CHAIN (parmlist
))))
5886 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
5887 add_template_candidate (candidates
,
5899 else if (!template_only
)
5900 add_function_candidate (candidates
,
5913 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
5914 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
5917 op_is_ordered (tree_code code
)
5923 return (flag_strong_eval_order
> 1 ? -1 : 0);
5927 return (flag_strong_eval_order
> 1 ? 1 : 0);
5930 // Not overloadable (yet).
5932 // Only one argument.
5939 return (flag_strong_eval_order
? 1 : 0);
5946 /* Subroutine of build_new_op_1: Add to CANDIDATES all candidates for the
5947 operator indicated by CODE/CODE2. This function calls itself recursively to
5948 handle C++20 rewritten comparison operator candidates. */
5951 add_operator_candidates (z_candidate
**candidates
,
5952 tree_code code
, tree_code code2
,
5953 vec
<tree
, va_gc
> *arglist
,
5954 int flags
, tsubst_flags_t complain
)
5956 z_candidate
*start_candidates
= *candidates
;
5957 bool ismodop
= code2
!= ERROR_MARK
;
5958 tree fnname
= ovl_op_identifier (ismodop
, ismodop
? code2
: code
);
5960 /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
5961 rewrite from, and also when we're looking for the e.g. < operator to use
5962 on the result of <=>. In the latter case, we don't want the flag set in
5963 the candidate, we just want to suppress looking for rewrites. */
5964 bool rewritten
= (flags
& LOOKUP_REWRITTEN
);
5965 if (rewritten
&& code
!= EQ_EXPR
&& code
!= SPACESHIP_EXPR
)
5966 flags
&= ~LOOKUP_REWRITTEN
;
5968 bool memonly
= false;
5971 /* =, ->, [], () must be non-static member functions. */
5973 if (code2
!= NOP_EXPR
)
5985 /* Add namespace-scope operators to the list of functions to
5989 tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
);
5990 fns
= lookup_arg_dependent (fnname
, fns
, arglist
);
5991 add_candidates (fns
, NULL_TREE
, arglist
, NULL_TREE
,
5992 NULL_TREE
, false, NULL_TREE
, NULL_TREE
,
5993 flags
, candidates
, complain
);
5996 /* Add class-member operators to the candidate set. */
5997 tree arg1_type
= TREE_TYPE ((*arglist
)[0]);
5998 unsigned nargs
= arglist
->length () > 1 ? 2 : 1;
5999 tree arg2_type
= nargs
> 1 ? TREE_TYPE ((*arglist
)[1]) : NULL_TREE
;
6000 if (CLASS_TYPE_P (arg1_type
))
6002 tree fns
= lookup_fnfields (arg1_type
, fnname
, 1, complain
);
6003 if (fns
== error_mark_node
)
6004 return error_mark_node
;
6006 add_candidates (BASELINK_FUNCTIONS (fns
),
6007 NULL_TREE
, arglist
, NULL_TREE
,
6009 BASELINK_BINFO (fns
),
6010 BASELINK_ACCESS_BINFO (fns
),
6011 flags
, candidates
, complain
);
6013 /* Per [over.match.oper]3.2, if no operand has a class type, then
6014 only non-member functions that have type T1 or reference to
6015 cv-qualified-opt T1 for the first argument, if the first argument
6016 has an enumeration type, or T2 or reference to cv-qualified-opt
6017 T2 for the second argument, if the second argument has an
6018 enumeration type. Filter out those that don't match. */
6019 else if (! arg2_type
|| ! CLASS_TYPE_P (arg2_type
))
6021 struct z_candidate
**candp
, **next
;
6023 for (candp
= candidates
; *candp
!= start_candidates
; candp
= next
)
6026 z_candidate
*cand
= *candp
;
6029 tree parmlist
= TYPE_ARG_TYPES (TREE_TYPE (cand
->fn
));
6031 for (i
= 0; i
< nargs
; ++i
)
6033 tree parmtype
= TREE_VALUE (parmlist
);
6034 tree argtype
= unlowered_expr_type ((*arglist
)[i
]);
6036 if (TYPE_REF_P (parmtype
))
6037 parmtype
= TREE_TYPE (parmtype
);
6038 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
6039 && (same_type_ignoring_top_level_qualifiers_p
6040 (argtype
, parmtype
)))
6043 parmlist
= TREE_CHAIN (parmlist
);
6046 /* No argument has an appropriate type, so remove this
6047 candidate function from the list. */
6050 *candp
= cand
->next
;
6058 /* The standard says to rewrite built-in candidates, too,
6059 but there's no point. */
6060 add_builtin_candidates (candidates
, code
, code2
, fnname
, arglist
,
6063 /* Maybe add C++20 rewritten comparison candidates. */
6064 tree_code rewrite_code
= ERROR_MARK
;
6065 if (cxx_dialect
>= cxx20
6067 && (OVERLOAD_TYPE_P (arg1_type
) || OVERLOAD_TYPE_P (arg2_type
)))
6074 case SPACESHIP_EXPR
:
6075 rewrite_code
= SPACESHIP_EXPR
;
6080 rewrite_code
= EQ_EXPR
;
6088 flags
|= LOOKUP_REWRITTEN
;
6089 if (rewrite_code
!= code
)
6090 /* Add rewritten candidates in same order. */
6091 add_operator_candidates (candidates
, rewrite_code
, ERROR_MARK
,
6092 arglist
, flags
, complain
);
6094 z_candidate
*save_cand
= *candidates
;
6096 /* Add rewritten candidates in reverse order. */
6097 flags
|= LOOKUP_REVERSED
;
6098 vec
<tree
,va_gc
> *revlist
= make_tree_vector ();
6099 revlist
->quick_push ((*arglist
)[1]);
6100 revlist
->quick_push ((*arglist
)[0]);
6101 add_operator_candidates (candidates
, rewrite_code
, ERROR_MARK
,
6102 revlist
, flags
, complain
);
6104 /* Release the vec if we didn't add a candidate that uses it. */
6105 for (z_candidate
*c
= *candidates
; c
!= save_cand
; c
= c
->next
)
6106 if (c
->args
== revlist
)
6111 release_tree_vector (revlist
);
6119 build_new_op_1 (const op_location_t
&loc
, enum tree_code code
, int flags
,
6120 tree arg1
, tree arg2
, tree arg3
, tree
*overload
,
6121 tsubst_flags_t complain
)
6123 struct z_candidate
*candidates
= 0, *cand
;
6124 vec
<tree
, va_gc
> *arglist
;
6125 tree result
= NULL_TREE
;
6126 bool result_valid_p
= false;
6127 enum tree_code code2
= ERROR_MARK
;
6128 enum tree_code code_orig_arg1
= ERROR_MARK
;
6129 enum tree_code code_orig_arg2
= ERROR_MARK
;
6135 if (error_operand_p (arg1
)
6136 || error_operand_p (arg2
)
6137 || error_operand_p (arg3
))
6138 return error_mark_node
;
6140 bool ismodop
= code
== MODIFY_EXPR
;
6143 code2
= TREE_CODE (arg3
);
6147 tree arg1_type
= unlowered_expr_type (arg1
);
6148 tree arg2_type
= arg2
? unlowered_expr_type (arg2
) : NULL_TREE
;
6150 arg1
= prep_operand (arg1
);
6156 case VEC_DELETE_EXPR
:
6158 /* Use build_operator_new_call and build_op_delete_call instead. */
6162 /* Use build_op_call instead. */
6165 case TRUTH_ORIF_EXPR
:
6166 case TRUTH_ANDIF_EXPR
:
6167 case TRUTH_AND_EXPR
:
6169 /* These are saved for the sake of warn_logical_operator. */
6170 code_orig_arg1
= TREE_CODE (arg1
);
6171 code_orig_arg2
= TREE_CODE (arg2
);
6179 /* These are saved for the sake of maybe_warn_bool_compare. */
6180 code_orig_arg1
= TREE_CODE (arg1_type
);
6181 code_orig_arg2
= TREE_CODE (arg2_type
);
6188 arg2
= prep_operand (arg2
);
6189 arg3
= prep_operand (arg3
);
6191 if (code
== COND_EXPR
)
6192 /* Use build_conditional_expr instead. */
6194 else if (! OVERLOAD_TYPE_P (arg1_type
)
6195 && (! arg2
|| ! OVERLOAD_TYPE_P (arg2_type
)))
6198 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6200 arg2
= integer_zero_node
;
6201 arg2_type
= integer_type_node
;
6204 vec_alloc (arglist
, 3);
6205 arglist
->quick_push (arg1
);
6206 if (arg2
!= NULL_TREE
)
6207 arglist
->quick_push (arg2
);
6208 if (arg3
!= NULL_TREE
)
6209 arglist
->quick_push (arg3
);
6211 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6212 p
= conversion_obstack_alloc (0);
6214 result
= add_operator_candidates (&candidates
, code
, code2
, arglist
,
6216 if (result
== error_mark_node
)
6217 goto user_defined_result_ready
;
6223 /* For these, the built-in candidates set is empty
6224 [over.match.oper]/3. We don't want non-strict matches
6225 because exact matches are always possible with built-in
6226 operators. The built-in candidate set for COMPONENT_REF
6227 would be empty too, but since there are no such built-in
6228 operators, we accept non-strict matches for them. */
6237 candidates
= splice_viable (candidates
, strict_p
, &any_viable_p
);
6242 case POSTINCREMENT_EXPR
:
6243 case POSTDECREMENT_EXPR
:
6244 /* Don't try anything fancy if we're not allowed to produce
6246 if (!(complain
& tf_error
))
6247 return error_mark_node
;
6249 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
6250 distinguish between prefix and postfix ++ and
6251 operator++() was used for both, so we allow this with
6255 tree fnname
= ovl_op_identifier (ismodop
, ismodop
? code2
: code
);
6256 const char *msg
= (flag_permissive
)
6257 ? G_("no %<%D(int)%> declared for postfix %qs,"
6258 " trying prefix operator instead")
6259 : G_("no %<%D(int)%> declared for postfix %qs");
6260 permerror (loc
, msg
, fnname
, OVL_OP_INFO (false, code
)->name
);
6263 if (!flag_permissive
)
6264 return error_mark_node
;
6266 if (code
== POSTINCREMENT_EXPR
)
6267 code
= PREINCREMENT_EXPR
;
6269 code
= PREDECREMENT_EXPR
;
6270 result
= build_new_op_1 (loc
, code
, flags
, arg1
, NULL_TREE
,
6271 NULL_TREE
, overload
, complain
);
6274 /* The caller will deal with these. */
6280 result_valid_p
= true;
6284 if (complain
& tf_error
)
6286 /* If one of the arguments of the operator represents
6287 an invalid use of member function pointer, try to report
6288 a meaningful error ... */
6289 if (invalid_nonstatic_memfn_p (loc
, arg1
, tf_error
)
6290 || invalid_nonstatic_memfn_p (loc
, arg2
, tf_error
)
6291 || invalid_nonstatic_memfn_p (loc
, arg3
, tf_error
))
6292 /* We displayed the error message. */;
6295 /* ... Otherwise, report the more generic
6296 "no matching operator found" error */
6297 auto_diagnostic_group d
;
6298 op_error (loc
, code
, code2
, arg1
, arg2
, arg3
, FALSE
);
6299 print_z_candidates (loc
, candidates
);
6302 result
= error_mark_node
;
6308 cand
= tourney (candidates
, complain
);
6311 if (complain
& tf_error
)
6313 auto_diagnostic_group d
;
6314 op_error (loc
, code
, code2
, arg1
, arg2
, arg3
, TRUE
);
6315 print_z_candidates (loc
, candidates
);
6317 result
= error_mark_node
;
6319 else if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
6322 *overload
= cand
->fn
;
6324 if (resolve_args (arglist
, complain
) == NULL
)
6325 result
= error_mark_node
;
6328 tsubst_flags_t ocomplain
= complain
;
6329 if (cand
->rewritten ())
6330 /* We'll wrap this call in another one. */
6331 ocomplain
&= ~tf_decltype
;
6332 if (cand
->reversed ())
6334 /* We swapped these in add_candidate, swap them back now. */
6335 std::swap (cand
->convs
[0], cand
->convs
[1]);
6336 if (cand
->fn
== current_function_decl
)
6337 warning_at (loc
, 0, "in C++20 this comparison calls the "
6338 "current function recursively with reversed "
6341 result
= build_over_call (cand
, LOOKUP_NORMAL
, ocomplain
);
6344 if (trivial_fn_p (cand
->fn
) || DECL_IMMEDIATE_FUNCTION_P (cand
->fn
))
6345 /* There won't be a CALL_EXPR. */;
6346 else if (result
&& result
!= error_mark_node
)
6348 tree call
= extract_call_expr (result
);
6349 CALL_EXPR_OPERATOR_SYNTAX (call
) = true;
6351 /* Specify evaluation order as per P0145R2. */
6352 CALL_EXPR_ORDERED_ARGS (call
) = false;
6353 switch (op_is_ordered (code
))
6356 CALL_EXPR_REVERSE_ARGS (call
) = true;
6360 CALL_EXPR_ORDERED_ARGS (call
) = true;
6368 /* If this was a C++20 rewritten comparison, adjust the result. */
6369 if (cand
->rewritten ())
6371 /* FIXME build_min_non_dep_op_overload can't handle rewrites. */
6373 *overload
= NULL_TREE
;
6377 gcc_checking_assert (cand
->reversed ());
6380 /* If a rewritten operator== candidate is selected by
6381 overload resolution for an operator @, its return type
6382 shall be cv bool.... */
6383 if (TREE_CODE (TREE_TYPE (result
)) != BOOLEAN_TYPE
)
6385 if (complain
& tf_error
)
6387 auto_diagnostic_group d
;
6388 error_at (loc
, "return type of %qD is not %qs",
6390 inform (loc
, "used as rewritten candidate for "
6391 "comparison of %qT and %qT",
6392 arg1_type
, arg2_type
);
6394 result
= error_mark_node
;
6396 else if (code
== NE_EXPR
)
6397 /* !(y == x) or !(x == y) */
6398 result
= build1_loc (loc
, TRUTH_NOT_EXPR
,
6399 boolean_type_node
, result
);
6402 /* If a rewritten operator<=> candidate is selected by
6403 overload resolution for an operator @, x @ y is
6404 interpreted as 0 @ (y <=> x) if the selected candidate is
6405 a synthesized candidate with reversed order of parameters,
6406 or (x <=> y) @ 0 otherwise, using the selected rewritten
6407 operator<=> candidate. */
6408 case SPACESHIP_EXPR
:
6409 if (!cand
->reversed ())
6410 /* We're in the build_new_op call below for an outer
6411 reversed call; we don't need to do anything more. */
6420 tree rhs
= integer_zero_node
;
6421 if (cand
->reversed ())
6422 std::swap (lhs
, rhs
);
6423 warning_sentinel
ws (warn_zero_as_null_pointer_constant
);
6424 result
= build_new_op (loc
, code
,
6425 LOOKUP_NORMAL
|LOOKUP_REWRITTEN
,
6426 lhs
, rhs
, NULL_TREE
,
6438 /* Give any warnings we noticed during overload resolution. */
6439 if (cand
->warnings
&& (complain
& tf_warning
))
6441 struct candidate_warning
*w
;
6442 for (w
= cand
->warnings
; w
; w
= w
->next
)
6443 joust (cand
, w
->loser
, 1, complain
);
6446 /* Check for comparison of different enum types. */
6455 if (TREE_CODE (arg1_type
) == ENUMERAL_TYPE
6456 && TREE_CODE (arg2_type
) == ENUMERAL_TYPE
6457 && (TYPE_MAIN_VARIANT (arg1_type
)
6458 != TYPE_MAIN_VARIANT (arg2_type
))
6459 && (complain
& tf_warning
))
6460 warning_at (loc
, OPT_Wenum_compare
,
6461 "comparison between %q#T and %q#T",
6462 arg1_type
, arg2_type
);
6468 /* "If a built-in candidate is selected by overload resolution, the
6469 operands of class type are converted to the types of the
6470 corresponding parameters of the selected operation function,
6471 except that the second standard conversion sequence of a
6472 user-defined conversion sequence (12.3.3.1.2) is not applied." */
6473 conv
= cand
->convs
[0];
6474 if (conv
->user_conv_p
)
6476 conv
= strip_standard_conversion (conv
);
6477 arg1
= convert_like (conv
, arg1
, complain
);
6482 conv
= cand
->convs
[1];
6483 if (conv
->user_conv_p
)
6485 conv
= strip_standard_conversion (conv
);
6486 arg2
= convert_like (conv
, arg2
, complain
);
6492 conv
= cand
->convs
[2];
6493 if (conv
->user_conv_p
)
6495 conv
= strip_standard_conversion (conv
);
6496 arg3
= convert_like (conv
, arg3
, complain
);
6502 user_defined_result_ready
:
6504 /* Free all the conversions we allocated. */
6505 obstack_free (&conversion_obstack
, p
);
6507 if (result
|| result_valid_p
)
6514 return cp_build_modify_expr (loc
, arg1
, code2
, arg2
, complain
);
6517 return cp_build_indirect_ref (loc
, arg1
, RO_UNARY_STAR
, complain
);
6519 case TRUTH_ANDIF_EXPR
:
6520 case TRUTH_ORIF_EXPR
:
6521 case TRUTH_AND_EXPR
:
6523 if (complain
& tf_warning
)
6524 warn_logical_operator (loc
, code
, boolean_type_node
,
6525 code_orig_arg1
, arg1
,
6526 code_orig_arg2
, arg2
);
6534 if ((complain
& tf_warning
)
6535 && ((code_orig_arg1
== BOOLEAN_TYPE
)
6536 ^ (code_orig_arg2
== BOOLEAN_TYPE
)))
6537 maybe_warn_bool_compare (loc
, code
, arg1
, arg2
);
6538 if (complain
& tf_warning
&& warn_tautological_compare
)
6539 warn_tautological_cmp (loc
, code
, arg1
, arg2
);
6541 case SPACESHIP_EXPR
:
6545 case TRUNC_DIV_EXPR
:
6550 case TRUNC_MOD_EXPR
:
6554 return cp_build_binary_op (loc
, code
, arg1
, arg2
, complain
);
6556 case UNARY_PLUS_EXPR
:
6559 case TRUTH_NOT_EXPR
:
6560 case PREINCREMENT_EXPR
:
6561 case POSTINCREMENT_EXPR
:
6562 case PREDECREMENT_EXPR
:
6563 case POSTDECREMENT_EXPR
:
6568 return cp_build_unary_op (code
, arg1
, false, complain
);
6571 return cp_build_array_ref (input_location
, arg1
, arg2
, complain
);
6574 return build_m_component_ref (cp_build_indirect_ref (loc
, arg1
,
6579 /* The caller will deal with these. */
6591 /* Wrapper for above. */
6594 build_new_op (const op_location_t
&loc
, enum tree_code code
, int flags
,
6595 tree arg1
, tree arg2
, tree arg3
,
6596 tree
*overload
, tsubst_flags_t complain
)
6599 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
6600 ret
= build_new_op_1 (loc
, code
, flags
, arg1
, arg2
, arg3
,
6601 overload
, complain
);
6602 timevar_cond_stop (TV_OVERLOAD
, subtime
);
6606 /* CALL was returned by some call-building function; extract the actual
6607 CALL_EXPR from any bits that have been tacked on, e.g. by
6608 convert_from_reference. */
6611 extract_call_expr (tree call
)
6613 while (TREE_CODE (call
) == COMPOUND_EXPR
)
6614 call
= TREE_OPERAND (call
, 1);
6615 if (REFERENCE_REF_P (call
))
6616 call
= TREE_OPERAND (call
, 0);
6617 if (TREE_CODE (call
) == TARGET_EXPR
)
6618 call
= TARGET_EXPR_INITIAL (call
);
6619 if (cxx_dialect
>= cxx20
)
6620 switch (TREE_CODE (call
))
6622 /* C++20 rewritten comparison operators. */
6623 case TRUTH_NOT_EXPR
:
6624 call
= TREE_OPERAND (call
, 0);
6630 case SPACESHIP_EXPR
:
6632 tree op0
= TREE_OPERAND (call
, 0);
6633 if (integer_zerop (op0
))
6634 call
= TREE_OPERAND (call
, 1);
6642 gcc_assert (TREE_CODE (call
) == CALL_EXPR
6643 || TREE_CODE (call
) == AGGR_INIT_EXPR
6644 || call
== error_mark_node
);
6648 /* Returns true if FN has two parameters, of which the second has type
6652 second_parm_is_size_t (tree fn
)
6654 tree t
= FUNCTION_ARG_CHAIN (fn
);
6655 if (!t
|| !same_type_p (TREE_VALUE (t
), size_type_node
))
6658 if (t
== void_list_node
)
6663 /* True if T, an allocation function, has std::align_val_t as its second
6667 aligned_allocation_fn_p (tree t
)
6669 if (!aligned_new_threshold
)
6672 tree a
= FUNCTION_ARG_CHAIN (t
);
6673 return (a
&& same_type_p (TREE_VALUE (a
), align_type_node
));
6676 /* True if T is std::destroying_delete_t. */
6679 std_destroying_delete_t_p (tree t
)
6681 return (TYPE_CONTEXT (t
) == std_node
6682 && id_equal (TYPE_IDENTIFIER (t
), "destroying_delete_t"));
6685 /* A deallocation function with at least two parameters whose second parameter
6686 type is of type std::destroying_delete_t is a destroying operator delete. A
6687 destroying operator delete shall be a class member function named operator
6688 delete. [ Note: Array deletion cannot use a destroying operator
6689 delete. --end note ] */
6692 destroying_delete_p (tree t
)
6694 tree a
= TYPE_ARG_TYPES (TREE_TYPE (t
));
6695 if (!a
|| !TREE_CHAIN (a
))
6697 tree type
= TREE_VALUE (TREE_CHAIN (a
));
6698 return std_destroying_delete_t_p (type
) ? type
: NULL_TREE
;
6708 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
6709 function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
6710 non-null, also set *DI. */
6713 usual_deallocation_fn_p (tree t
, dealloc_info
*di
)
6715 if (di
) *di
= dealloc_info();
6717 /* A template instance is never a usual deallocation function,
6718 regardless of its signature. */
6719 if (TREE_CODE (t
) == TEMPLATE_DECL
6720 || primary_template_specialization_p (t
))
6723 /* A usual deallocation function is a deallocation function whose parameters
6725 - optionally, a parameter of type std::destroying_delete_t, then
6726 - optionally, a parameter of type std::size_t, then
6727 - optionally, a parameter of type std::align_val_t. */
6728 bool global
= DECL_NAMESPACE_SCOPE_P (t
);
6729 tree chain
= FUNCTION_ARG_CHAIN (t
);
6730 if (chain
&& destroying_delete_p (t
))
6732 if (di
) di
->destroying
= TREE_VALUE (chain
);
6733 chain
= TREE_CHAIN (chain
);
6736 && (!global
|| flag_sized_deallocation
)
6737 && same_type_p (TREE_VALUE (chain
), size_type_node
))
6739 if (di
) di
->sized
= true;
6740 chain
= TREE_CHAIN (chain
);
6742 if (chain
&& aligned_new_threshold
6743 && same_type_p (TREE_VALUE (chain
), align_type_node
))
6745 if (di
) di
->aligned
= true;
6746 chain
= TREE_CHAIN (chain
);
6748 return (chain
== void_list_node
);
6751 /* Just return whether FN is a usual deallocation function. */
6754 usual_deallocation_fn_p (tree fn
)
6756 return usual_deallocation_fn_p (fn
, NULL
);
6759 /* Build a call to operator delete. This has to be handled very specially,
6760 because the restrictions on what signatures match are different from all
6761 other call instances. For a normal delete, only a delete taking (void *)
6762 or (void *, size_t) is accepted. For a placement delete, only an exact
6763 match with the placement new is accepted.
6765 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
6766 ADDR is the pointer to be deleted.
6767 SIZE is the size of the memory block to be deleted.
6768 GLOBAL_P is true if the delete-expression should not consider
6769 class-specific delete operators.
6770 PLACEMENT is the corresponding placement new call, or NULL_TREE.
6772 If this call to "operator delete" is being generated as part to
6773 deallocate memory allocated via a new-expression (as per [expr.new]
6774 which requires that if the initialization throws an exception then
6775 we call a deallocation function), then ALLOC_FN is the allocation
6779 build_op_delete_call (enum tree_code code
, tree addr
, tree size
,
6780 bool global_p
, tree placement
,
6781 tree alloc_fn
, tsubst_flags_t complain
)
6783 tree fn
= NULL_TREE
;
6784 tree fns
, fnname
, type
, t
;
6785 dealloc_info di_fn
= { };
6787 if (addr
== error_mark_node
)
6788 return error_mark_node
;
6790 type
= strip_array_types (TREE_TYPE (TREE_TYPE (addr
)));
6792 fnname
= ovl_op_identifier (false, code
);
6794 if (CLASS_TYPE_P (type
)
6795 && COMPLETE_TYPE_P (complete_type (type
))
6799 If the result of the lookup is ambiguous or inaccessible, or if
6800 the lookup selects a placement deallocation function, the
6801 program is ill-formed.
6803 Therefore, we ask lookup_fnfields to complain about ambiguity. */
6805 fns
= lookup_fnfields (TYPE_BINFO (type
), fnname
, 1, complain
);
6806 if (fns
== error_mark_node
)
6807 return error_mark_node
;
6812 if (fns
== NULL_TREE
)
6813 fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
);
6815 /* Strip const and volatile from addr. */
6817 addr
= cp_convert (ptr_type_node
, addr
, complain
);
6821 /* "A declaration of a placement deallocation function matches the
6822 declaration of a placement allocation function if it has the same
6823 number of parameters and, after parameter transformations (8.3.5),
6824 all parameter types except the first are identical."
6826 So we build up the function type we want and ask instantiate_type
6827 to get it for us. */
6828 t
= FUNCTION_ARG_CHAIN (alloc_fn
);
6829 t
= tree_cons (NULL_TREE
, ptr_type_node
, t
);
6830 t
= build_function_type (void_type_node
, t
);
6832 fn
= instantiate_type (t
, fns
, tf_none
);
6833 if (fn
== error_mark_node
)
6836 fn
= MAYBE_BASELINK_FUNCTIONS (fn
);
6838 /* "If the lookup finds the two-parameter form of a usual deallocation
6839 function (3.7.4.2) and that function, considered as a placement
6840 deallocation function, would have been selected as a match for the
6841 allocation function, the program is ill-formed." */
6842 if (second_parm_is_size_t (fn
))
6844 const char *const msg1
6845 = G_("exception cleanup for this placement new selects "
6846 "non-placement %<operator delete%>");
6847 const char *const msg2
6848 = G_("%qD is a usual (non-placement) deallocation "
6849 "function in C++14 (or with %<-fsized-deallocation%>)");
6851 /* But if the class has an operator delete (void *), then that is
6852 the usual deallocation function, so we shouldn't complain
6853 about using the operator delete (void *, size_t). */
6854 if (DECL_CLASS_SCOPE_P (fn
))
6855 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (fns
));
6859 if (usual_deallocation_fn_p (elt
)
6860 && FUNCTION_ARG_CHAIN (elt
) == void_list_node
)
6863 /* Before C++14 a two-parameter global deallocation function is
6864 always a placement deallocation function, but warn if
6866 else if (!flag_sized_deallocation
)
6868 if (complain
& tf_warning
)
6870 auto_diagnostic_group d
;
6871 if (warning (OPT_Wc__14_compat
, msg1
))
6872 inform (DECL_SOURCE_LOCATION (fn
), msg2
, fn
);
6877 if (complain
& tf_warning_or_error
)
6879 auto_diagnostic_group d
;
6880 if (permerror (input_location
, msg1
))
6882 /* Only mention C++14 for namespace-scope delete. */
6883 if (DECL_NAMESPACE_SCOPE_P (fn
))
6884 inform (DECL_SOURCE_LOCATION (fn
), msg2
, fn
);
6886 inform (DECL_SOURCE_LOCATION (fn
),
6887 "%qD is a usual (non-placement) deallocation "
6892 return error_mark_node
;
6897 /* "Any non-placement deallocation function matches a non-placement
6898 allocation function. If the lookup finds a single matching
6899 deallocation function, that function will be called; otherwise, no
6900 deallocation function will be called." */
6901 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (fns
)); iter
; ++iter
)
6904 dealloc_info di_elt
;
6905 if (usual_deallocation_fn_p (elt
, &di_elt
))
6914 /* -- If any of the deallocation functions is a destroying
6915 operator delete, all deallocation functions that are not
6916 destroying operator deletes are eliminated from further
6918 if (di_elt
.destroying
!= di_fn
.destroying
)
6920 if (di_elt
.destroying
)
6928 /* -- If the type has new-extended alignment, a function with a
6929 parameter of type std::align_val_t is preferred; otherwise a
6930 function without such a parameter is preferred. If exactly one
6931 preferred function is found, that function is selected and the
6932 selection process terminates. If more than one preferred
6933 function is found, all non-preferred functions are eliminated
6934 from further consideration. */
6935 if (aligned_new_threshold
)
6937 bool want_align
= type_has_new_extended_alignment (type
);
6938 if (di_elt
.aligned
!= di_fn
.aligned
)
6940 if (want_align
== di_elt
.aligned
)
6949 /* -- If the deallocation functions have class scope, the one
6950 without a parameter of type std::size_t is selected. */
6952 if (DECL_CLASS_SCOPE_P (fn
))
6955 /* -- If the type is complete and if, for the second alternative
6956 (delete array) only, the operand is a pointer to a class type
6957 with a non-trivial destructor or a (possibly multi-dimensional)
6958 array thereof, the function with a parameter of type std::size_t
6961 -- Otherwise, it is unspecified whether a deallocation function
6962 with a parameter of type std::size_t is selected. */
6965 want_size
= COMPLETE_TYPE_P (type
);
6966 if (code
== VEC_DELETE_EXPR
6967 && !TYPE_VEC_NEW_USES_COOKIE (type
))
6968 /* We need a cookie to determine the array size. */
6971 gcc_assert (di_fn
.sized
!= di_elt
.sized
);
6972 if (want_size
== di_elt
.sized
)
6980 /* If we have a matching function, call it. */
6983 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
6985 /* If the FN is a member function, make sure that it is
6987 if (BASELINK_P (fns
))
6988 perform_or_defer_access_check (BASELINK_BINFO (fns
), fn
, fn
,
6991 /* Core issue 901: It's ok to new a type with deleted delete. */
6992 if (DECL_DELETED_FN (fn
) && alloc_fn
)
6998 /* The placement args might not be suitable for overload
6999 resolution at this point, so build the call directly. */
7000 int nargs
= call_expr_nargs (placement
);
7001 tree
*argarray
= XALLOCAVEC (tree
, nargs
);
7004 for (i
= 1; i
< nargs
; i
++)
7005 argarray
[i
] = CALL_EXPR_ARG (placement
, i
);
7006 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
7007 return error_mark_node
;
7008 ret
= build_cxx_call (fn
, nargs
, argarray
, complain
);
7012 tree destroying
= di_fn
.destroying
;
7015 /* Strip const and volatile from addr but retain the type of the
7017 tree rtype
= TREE_TYPE (TREE_TYPE (oaddr
));
7018 rtype
= cv_unqualified (rtype
);
7019 rtype
= TYPE_POINTER_TO (rtype
);
7020 addr
= cp_convert (rtype
, oaddr
, complain
);
7021 destroying
= build_functional_cast (input_location
,
7022 destroying
, NULL_TREE
,
7027 args
->quick_push (addr
);
7029 args
->quick_push (destroying
);
7031 args
->quick_push (size
);
7034 tree al
= build_int_cst (align_type_node
, TYPE_ALIGN_UNIT (type
));
7035 args
->quick_push (al
);
7037 ret
= cp_build_function_call_vec (fn
, &args
, complain
);
7040 /* Set this flag for all callers of this function. In addition to
7041 delete-expressions, this is called for deallocating coroutine state;
7042 treat that as an implicit delete-expression. This is also called for
7043 the delete if the constructor throws in a new-expression, and for a
7044 deleting destructor (which implements a delete-expression). */
7045 tree call
= extract_call_expr (ret
);
7046 if (TREE_CODE (call
) == CALL_EXPR
)
7047 CALL_FROM_NEW_OR_DELETE_P (call
) = 1;
7054 If no unambiguous matching deallocation function can be found,
7055 propagating the exception does not cause the object's memory to
7059 if ((complain
& tf_warning
)
7061 warning (0, "no corresponding deallocation function for %qD",
7066 if (complain
& tf_error
)
7067 error ("no suitable %<operator %s%> for %qT",
7068 OVL_OP_INFO (false, code
)->name
, type
);
7069 return error_mark_node
;
7072 /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
7075 If ISSUE_ERROR is true, then issue an error about the
7076 access, followed by a note showing the declaration.
7077 Otherwise, just show the note. */
7080 complain_about_access (tree decl
, tree diag_decl
, bool issue_error
)
7082 if (TREE_PRIVATE (decl
))
7085 error ("%q#D is private within this context", diag_decl
);
7086 inform (DECL_SOURCE_LOCATION (diag_decl
),
7087 "declared private here");
7089 else if (TREE_PROTECTED (decl
))
7092 error ("%q#D is protected within this context", diag_decl
);
7093 inform (DECL_SOURCE_LOCATION (diag_decl
),
7094 "declared protected here");
7099 error ("%q#D is inaccessible within this context", diag_decl
);
7100 inform (DECL_SOURCE_LOCATION (diag_decl
), "declared here");
7104 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
7105 bitwise or of LOOKUP_* values. If any errors are warnings are
7106 generated, set *DIAGNOSTIC_FN to "error" or "warning",
7107 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
7111 build_temp (tree expr
, tree type
, int flags
,
7112 diagnostic_t
*diagnostic_kind
, tsubst_flags_t complain
)
7116 *diagnostic_kind
= DK_UNSPECIFIED
;
7118 /* If the source is a packed field, calling the copy constructor will require
7119 binding the field to the reference parameter to the copy constructor, and
7120 we'll end up with an infinite loop. If we can use a bitwise copy, then
7122 if ((lvalue_kind (expr
) & clk_packed
)
7123 && CLASS_TYPE_P (TREE_TYPE (expr
))
7124 && !type_has_nontrivial_copy_init (TREE_TYPE (expr
)))
7125 return get_target_expr_sfinae (expr
, complain
);
7127 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
7128 releasing_vec
args (make_tree_vector_single (expr
));
7129 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
7130 &args
, type
, flags
, complain
);
7131 if (warningcount
+ werrorcount
> savew
)
7132 *diagnostic_kind
= DK_WARNING
;
7133 else if (errorcount
> savee
)
7134 *diagnostic_kind
= DK_ERROR
;
7138 /* Get any location for EXPR, falling back to input_location.
7140 If the result is in a system header and is the virtual location for
7141 a token coming from the expansion of a macro, unwind it to the
7142 location of the expansion point of the macro (e.g. to avoid the
7143 diagnostic being suppressed for expansions of NULL where "NULL" is
7144 in a system header). */
7147 get_location_for_expr_unwinding_for_system_header (tree expr
)
7149 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
7150 loc
= expansion_point_location_if_in_system_header (loc
);
7154 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
7155 Also handle a subset of zero as null warnings.
7156 EXPR is implicitly converted to type TOTYPE.
7157 FN and ARGNUM are used for diagnostics. */
7160 conversion_null_warnings (tree totype
, tree expr
, tree fn
, int argnum
)
7162 /* Issue warnings about peculiar, but valid, uses of NULL. */
7163 if (TREE_CODE (totype
) != BOOLEAN_TYPE
7164 && ARITHMETIC_TYPE_P (totype
)
7165 && null_node_p (expr
))
7167 location_t loc
= get_location_for_expr_unwinding_for_system_header (expr
);
7170 auto_diagnostic_group d
;
7171 if (warning_at (loc
, OPT_Wconversion_null
,
7172 "passing NULL to non-pointer argument %P of %qD",
7174 inform (get_fndecl_argument_location (fn
, argnum
),
7178 warning_at (loc
, OPT_Wconversion_null
,
7179 "converting to non-pointer type %qT from NULL", totype
);
7182 /* Issue warnings if "false" is converted to a NULL pointer */
7183 else if (TREE_CODE (TREE_TYPE (expr
)) == BOOLEAN_TYPE
7184 && TYPE_PTR_P (totype
))
7186 location_t loc
= get_location_for_expr_unwinding_for_system_header (expr
);
7189 auto_diagnostic_group d
;
7190 if (warning_at (loc
, OPT_Wconversion_null
,
7191 "converting %<false%> to pointer type for argument "
7192 "%P of %qD", argnum
, fn
))
7193 inform (get_fndecl_argument_location (fn
, argnum
),
7197 warning_at (loc
, OPT_Wconversion_null
,
7198 "converting %<false%> to pointer type %qT", totype
);
7200 /* Handle zero as null pointer warnings for cases other
7201 than EQ_EXPR and NE_EXPR */
7202 else if ((TYPE_PTR_OR_PTRMEM_P (totype
) || NULLPTR_TYPE_P (totype
))
7203 && null_ptr_cst_p (expr
))
7205 location_t loc
= get_location_for_expr_unwinding_for_system_header (expr
);
7206 maybe_warn_zero_as_null_pointer_constant (expr
, loc
);
7210 /* We gave a diagnostic during a conversion. If this was in the second
7211 standard conversion sequence of a user-defined conversion sequence, say
7212 which user-defined conversion. */
7215 maybe_print_user_conv_context (conversion
*convs
)
7217 if (convs
->user_conv_p
)
7218 for (conversion
*t
= convs
; t
; t
= next_conversion (t
))
7219 if (t
->kind
== ck_user
)
7221 print_z_candidate (0, N_(" after user-defined conversion:"),
7227 /* Locate the parameter with the given index within FNDECL.
7228 ARGNUM is zero based, -1 indicates the `this' argument of a method.
7229 Return the location of the FNDECL itself if there are problems. */
7232 get_fndecl_argument_location (tree fndecl
, int argnum
)
7234 /* The locations of implicitly-declared functions are likely to be
7235 more meaningful than those of their parameters. */
7236 if (DECL_ARTIFICIAL (fndecl
))
7237 return DECL_SOURCE_LOCATION (fndecl
);
7242 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
7243 for (i
= 0, param
= FUNCTION_FIRST_USER_PARM (fndecl
);
7244 i
< argnum
&& param
;
7245 i
++, param
= TREE_CHAIN (param
))
7248 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
7249 return the location of FNDECL. */
7251 return DECL_SOURCE_LOCATION (fndecl
);
7253 return DECL_SOURCE_LOCATION (param
);
7256 /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
7257 within its declaration (or the fndecl itself if something went
7261 maybe_inform_about_fndecl_for_bogus_argument_init (tree fn
, int argnum
)
7264 inform (get_fndecl_argument_location (fn
, argnum
),
7265 " initializing argument %P of %qD", argnum
, fn
);
7268 /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
7269 the conversion, EXPR is the expression we're converting. */
7272 maybe_warn_array_conv (location_t loc
, conversion
*c
, tree expr
)
7274 if (cxx_dialect
>= cxx20
)
7277 tree type
= TREE_TYPE (expr
);
7278 type
= strip_pointer_operator (type
);
7280 if (TREE_CODE (type
) != ARRAY_TYPE
7281 || TYPE_DOMAIN (type
) == NULL_TREE
)
7284 if (conv_binds_to_array_of_unknown_bound (c
))
7285 pedwarn (loc
, OPT_Wpedantic
, "conversions to arrays of unknown bound "
7286 "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
7289 /* We call this recursively in convert_like_internal. */
7290 static tree
convert_like (conversion
*, tree
, tree
, int, bool, bool,
7293 /* Perform the conversions in CONVS on the expression EXPR. FN and
7294 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
7295 indicates the `this' argument of a method. INNER is nonzero when
7296 being called to continue a conversion chain. It is negative when a
7297 reference binding will be applied, positive otherwise. If
7298 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
7299 conversions will be emitted if appropriate. If C_CAST_P is true,
7300 this conversion is coming from a C-style cast; in that case,
7301 conversions to inaccessible bases are permitted. */
7304 convert_like_internal (conversion
*convs
, tree expr
, tree fn
, int argnum
,
7305 bool issue_conversion_warnings
, bool c_cast_p
,
7306 tsubst_flags_t complain
)
7308 tree totype
= convs
->type
;
7309 diagnostic_t diag_kind
;
7311 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7313 if (convs
->bad_p
&& !(complain
& tf_error
))
7314 return error_mark_node
;
7317 && convs
->kind
!= ck_user
7318 && convs
->kind
!= ck_list
7319 && convs
->kind
!= ck_ambig
7320 && (convs
->kind
!= ck_ref_bind
7321 || (convs
->user_conv_p
&& next_conversion (convs
)->bad_p
))
7322 && (convs
->kind
!= ck_rvalue
7323 || SCALAR_TYPE_P (totype
))
7324 && convs
->kind
!= ck_base
)
7326 bool complained
= false;
7327 conversion
*t
= convs
;
7329 /* Give a helpful error if this is bad because of excess braces. */
7330 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
7331 && SCALAR_TYPE_P (totype
)
7332 && CONSTRUCTOR_NELTS (expr
) > 0
7333 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr
, 0)->value
))
7335 complained
= permerror (loc
, "too many braces around initializer "
7337 while (BRACE_ENCLOSED_INITIALIZER_P (expr
)
7338 && CONSTRUCTOR_NELTS (expr
) == 1)
7339 expr
= CONSTRUCTOR_ELT (expr
, 0)->value
;
7342 /* Give a helpful error if this is bad because a conversion to bool
7343 from std::nullptr_t requires direct-initialization. */
7344 if (NULLPTR_TYPE_P (TREE_TYPE (expr
))
7345 && TREE_CODE (totype
) == BOOLEAN_TYPE
)
7346 complained
= permerror (loc
, "converting to %qH from %qI requires "
7347 "direct-initialization",
7348 totype
, TREE_TYPE (expr
));
7350 for (; t
; t
= next_conversion (t
))
7352 if (t
->kind
== ck_user
&& t
->cand
->reason
)
7354 auto_diagnostic_group d
;
7355 complained
= permerror (loc
, "invalid user-defined conversion "
7356 "from %qH to %qI", TREE_TYPE (expr
),
7359 print_z_candidate (loc
, N_("candidate is:"), t
->cand
);
7360 expr
= convert_like (t
, expr
, fn
, argnum
,
7361 /*issue_conversion_warnings=*/false,
7362 /*c_cast_p=*/false, complain
);
7363 if (convs
->kind
== ck_ref_bind
)
7364 expr
= convert_to_reference (totype
, expr
, CONV_IMPLICIT
,
7365 LOOKUP_NORMAL
, NULL_TREE
,
7368 expr
= cp_convert (totype
, expr
, complain
);
7370 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7373 else if (t
->kind
== ck_user
|| !t
->bad_p
)
7375 expr
= convert_like (t
, expr
, fn
, argnum
,
7376 /*issue_conversion_warnings=*/false,
7377 /*c_cast_p=*/false, complain
);
7380 else if (t
->kind
== ck_ambig
)
7381 return convert_like (t
, expr
, fn
, argnum
,
7382 /*issue_conversion_warnings=*/false,
7383 /*c_cast_p=*/false, complain
);
7384 else if (t
->kind
== ck_identity
)
7389 range_label_for_type_mismatch
label (TREE_TYPE (expr
), totype
);
7390 gcc_rich_location
richloc (loc
, &label
);
7391 complained
= permerror (&richloc
,
7392 "invalid conversion from %qH to %qI",
7393 TREE_TYPE (expr
), totype
);
7396 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7398 return cp_convert (totype
, expr
, complain
);
7401 if (issue_conversion_warnings
&& (complain
& tf_warning
))
7402 conversion_null_warnings (totype
, expr
, fn
, argnum
);
7404 switch (convs
->kind
)
7408 struct z_candidate
*cand
= convs
->cand
;
7411 /* We chose the surrogate function from add_conv_candidate, now we
7412 actually need to build the conversion. */
7413 cand
= build_user_type_conversion_1 (totype
, expr
,
7414 LOOKUP_NO_CONVERSION
, complain
);
7416 tree convfn
= cand
->fn
;
7418 /* When converting from an init list we consider explicit
7419 constructors, but actually trying to call one is an error. */
7420 if (DECL_NONCONVERTING_P (convfn
) && DECL_CONSTRUCTOR_P (convfn
)
7421 && BRACE_ENCLOSED_INITIALIZER_P (expr
)
7422 /* Unless this is for direct-list-initialization. */
7423 && (!CONSTRUCTOR_IS_DIRECT_INIT (expr
) || convs
->need_temporary_p
)
7424 /* And in C++98 a default constructor can't be explicit. */
7425 && cxx_dialect
>= cxx11
)
7427 if (!(complain
& tf_error
))
7428 return error_mark_node
;
7429 location_t loc
= location_of (expr
);
7430 if (CONSTRUCTOR_NELTS (expr
) == 0
7431 && FUNCTION_FIRST_USER_PARMTYPE (convfn
) != void_list_node
)
7433 auto_diagnostic_group d
;
7434 if (pedwarn (loc
, 0, "converting to %qT from initializer list "
7435 "would use explicit constructor %qD",
7437 inform (loc
, "in C++11 and above a default constructor "
7441 error ("converting to %qT from initializer list would use "
7442 "explicit constructor %qD", totype
, convfn
);
7445 /* If we're initializing from {}, it's value-initialization. */
7446 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
7447 && CONSTRUCTOR_NELTS (expr
) == 0
7448 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype
)
7449 && !processing_template_decl
)
7451 bool direct
= CONSTRUCTOR_IS_DIRECT_INIT (expr
);
7452 if (abstract_virtuals_error_sfinae (NULL_TREE
, totype
, complain
))
7453 return error_mark_node
;
7454 expr
= build_value_init (totype
, complain
);
7455 expr
= get_target_expr_sfinae (expr
, complain
);
7456 if (expr
!= error_mark_node
)
7458 TARGET_EXPR_LIST_INIT_P (expr
) = true;
7459 TARGET_EXPR_DIRECT_INIT_P (expr
) = direct
;
7464 /* We don't know here whether EXPR is being used as an lvalue or
7465 rvalue, but we know it's read. */
7466 mark_exp_read (expr
);
7468 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
7470 expr
= build_over_call (cand
, LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
,
7473 /* If this is a constructor or a function returning an aggr type,
7474 we need to build up a TARGET_EXPR. */
7475 if (DECL_CONSTRUCTOR_P (convfn
))
7477 expr
= build_cplus_new (totype
, expr
, complain
);
7479 /* Remember that this was list-initialization. */
7480 if (convs
->check_narrowing
&& expr
!= error_mark_node
)
7481 TARGET_EXPR_LIST_INIT_P (expr
) = true;
7487 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
7489 int nelts
= CONSTRUCTOR_NELTS (expr
);
7491 expr
= build_value_init (totype
, complain
);
7492 else if (nelts
== 1)
7493 expr
= CONSTRUCTOR_ELT (expr
, 0)->value
;
7497 expr
= mark_use (expr
, /*rvalue_p=*/!convs
->rvaluedness_matches_p
,
7498 /*read_p=*/true, UNKNOWN_LOCATION
,
7499 /*reject_builtin=*/true);
7501 if (type_unknown_p (expr
))
7502 expr
= instantiate_type (totype
, expr
, complain
);
7503 if (expr
== null_node
7504 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype
))
7505 /* If __null has been converted to an integer type, we do not want to
7506 continue to warn about uses of EXPR as an integer, rather than as a
7508 expr
= build_int_cst (totype
, 0);
7511 /* We leave bad_p off ck_ambig because overload resolution considers
7512 it valid, it just fails when we try to perform it. So we need to
7513 check complain here, too. */
7514 if (complain
& tf_error
)
7516 /* Call build_user_type_conversion again for the error. */
7517 int flags
= (convs
->need_temporary_p
7518 ? LOOKUP_IMPLICIT
: LOOKUP_NORMAL
);
7519 build_user_type_conversion (totype
, convs
->u
.expr
, flags
, complain
);
7520 gcc_assert (seen_error ());
7521 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7523 return error_mark_node
;
7527 /* Conversion to std::initializer_list<T>. */
7528 tree elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype
), 0);
7529 unsigned len
= CONSTRUCTOR_NELTS (expr
);
7534 tree val
; unsigned ix
;
7536 tree new_ctor
= build_constructor (init_list_type_node
, NULL
);
7538 /* Convert all the elements. */
7539 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), ix
, val
)
7541 tree sub
= convert_like (convs
->u
.list
[ix
], val
, fn
,
7542 argnum
, false, false, complain
);
7543 if (sub
== error_mark_node
)
7545 if (!BRACE_ENCLOSED_INITIALIZER_P (val
)
7546 && !check_narrowing (TREE_TYPE (sub
), val
, complain
))
7547 return error_mark_node
;
7548 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor
),
7550 if (!TREE_CONSTANT (sub
))
7551 TREE_CONSTANT (new_ctor
) = false;
7553 /* Build up the array. */
7554 elttype
= cp_build_qualified_type
7555 (elttype
, cp_type_quals (elttype
) | TYPE_QUAL_CONST
);
7556 array
= build_array_of_n_type (elttype
, len
);
7557 array
= finish_compound_literal (array
, new_ctor
, complain
);
7558 /* Take the address explicitly rather than via decay_conversion
7559 to avoid the error about taking the address of a temporary. */
7560 array
= cp_build_addr_expr (array
, complain
);
7563 array
= nullptr_node
;
7565 array
= cp_convert (build_pointer_type (elttype
), array
, complain
);
7566 if (array
== error_mark_node
)
7567 return error_mark_node
;
7569 /* Build up the initializer_list object. Note: fail gracefully
7570 if the object cannot be completed because, for example, no
7571 definition is provided (c++/80956). */
7572 totype
= complete_type_or_maybe_complain (totype
, NULL_TREE
, complain
);
7574 return error_mark_node
;
7575 tree field
= next_initializable_field (TYPE_FIELDS (totype
));
7576 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
7577 CONSTRUCTOR_APPEND_ELT (vec
, field
, array
);
7578 field
= next_initializable_field (DECL_CHAIN (field
));
7579 CONSTRUCTOR_APPEND_ELT (vec
, field
, size_int (len
));
7580 tree new_ctor
= build_constructor (totype
, vec
);
7581 return get_target_expr_sfinae (new_ctor
, complain
);
7585 if (TREE_CODE (totype
) == COMPLEX_TYPE
)
7587 tree real
= CONSTRUCTOR_ELT (expr
, 0)->value
;
7588 tree imag
= CONSTRUCTOR_ELT (expr
, 1)->value
;
7589 real
= perform_implicit_conversion (TREE_TYPE (totype
),
7591 imag
= perform_implicit_conversion (TREE_TYPE (totype
),
7593 expr
= build2 (COMPLEX_EXPR
, totype
, real
, imag
);
7596 expr
= reshape_init (totype
, expr
, complain
);
7597 expr
= get_target_expr_sfinae (digest_init (totype
, expr
, complain
),
7599 if (expr
!= error_mark_node
)
7600 TARGET_EXPR_LIST_INIT_P (expr
) = true;
7607 expr
= convert_like (next_conversion (convs
), expr
, fn
, argnum
,
7608 convs
->kind
== ck_ref_bind
7609 ? issue_conversion_warnings
: false,
7610 c_cast_p
, complain
);
7611 if (expr
== error_mark_node
)
7612 return error_mark_node
;
7614 switch (convs
->kind
)
7617 expr
= decay_conversion (expr
, complain
);
7618 if (expr
== error_mark_node
)
7620 if (complain
& tf_error
)
7622 auto_diagnostic_group d
;
7623 maybe_print_user_conv_context (convs
);
7624 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7626 return error_mark_node
;
7629 if (! MAYBE_CLASS_TYPE_P (totype
))
7632 /* Don't introduce copies when passing arguments along to the inherited
7634 if (current_function_decl
7635 && flag_new_inheriting_ctors
7636 && DECL_INHERITED_CTOR (current_function_decl
))
7639 if (TREE_CODE (expr
) == TARGET_EXPR
7640 && TARGET_EXPR_LIST_INIT_P (expr
))
7641 /* Copy-list-initialization doesn't actually involve a copy. */
7646 if (convs
->kind
== ck_base
&& !convs
->need_temporary_p
)
7648 /* We are going to bind a reference directly to a base-class
7649 subobject of EXPR. */
7650 /* Build an expression for `*((base*) &expr)'. */
7651 expr
= convert_to_base (expr
, totype
,
7652 !c_cast_p
, /*nonnull=*/true, complain
);
7656 /* Copy-initialization where the cv-unqualified version of the source
7657 type is the same class as, or a derived class of, the class of the
7658 destination [is treated as direct-initialization]. [dcl.init] */
7659 flags
= LOOKUP_NORMAL
;
7660 /* This conversion is being done in the context of a user-defined
7661 conversion (i.e. the second step of copy-initialization), so
7662 don't allow any more. */
7663 if (convs
->user_conv_p
)
7664 flags
|= LOOKUP_NO_CONVERSION
;
7665 /* We might be performing a conversion of the argument
7666 to the user-defined conversion, i.e., not a conversion of the
7667 result of the user-defined conversion. In which case we skip
7668 explicit constructors. */
7669 if (convs
->copy_init_p
)
7670 flags
|= LOOKUP_ONLYCONVERTING
;
7671 if (convs
->rvaluedness_matches_p
)
7672 /* standard_conversion got LOOKUP_PREFER_RVALUE. */
7673 flags
|= LOOKUP_PREFER_RVALUE
;
7674 expr
= build_temp (expr
, totype
, flags
, &diag_kind
, complain
);
7675 if (diag_kind
&& complain
)
7677 auto_diagnostic_group d
;
7678 maybe_print_user_conv_context (convs
);
7679 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7682 return build_cplus_new (totype
, expr
, complain
);
7686 tree ref_type
= totype
;
7688 /* direct_reference_binding might have inserted a ck_qual under
7689 this ck_ref_bind for the benefit of conversion sequence ranking.
7690 Ignore the conversion; we'll create our own below. */
7691 if (next_conversion (convs
)->kind
== ck_qual
7692 && !convs
->need_temporary_p
)
7694 gcc_assert (same_type_p (TREE_TYPE (expr
),
7695 next_conversion (convs
)->type
));
7696 /* Strip the cast created by the ck_qual; cp_build_addr_expr
7697 below expects an lvalue. */
7701 if (convs
->bad_p
&& !next_conversion (convs
)->bad_p
)
7703 tree extype
= TREE_TYPE (expr
);
7704 auto_diagnostic_group d
;
7705 if (TYPE_REF_IS_RVALUE (ref_type
)
7707 error_at (loc
, "cannot bind rvalue reference of type %qH to "
7708 "lvalue of type %qI", totype
, extype
);
7709 else if (!TYPE_REF_IS_RVALUE (ref_type
) && !lvalue_p (expr
)
7710 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type
)))
7711 error_at (loc
, "cannot bind non-const lvalue reference of "
7712 "type %qH to an rvalue of type %qI", totype
, extype
);
7713 else if (!reference_compatible_p (TREE_TYPE (totype
), extype
))
7715 /* If we're converting from T[] to T[N], don't talk
7716 about discarding qualifiers. (Converting from T[N] to
7717 T[] is allowed by P0388R4.) */
7718 if (TREE_CODE (extype
) == ARRAY_TYPE
7719 && TYPE_DOMAIN (extype
) == NULL_TREE
7720 && TREE_CODE (TREE_TYPE (totype
)) == ARRAY_TYPE
7721 && TYPE_DOMAIN (TREE_TYPE (totype
)) != NULL_TREE
)
7722 error_at (loc
, "cannot bind reference of type %qH to %qI "
7723 "due to different array bounds", totype
, extype
);
7725 error_at (loc
, "binding reference of type %qH to %qI "
7726 "discards qualifiers", totype
, extype
);
7730 maybe_print_user_conv_context (convs
);
7731 maybe_inform_about_fndecl_for_bogus_argument_init (fn
, argnum
);
7733 return error_mark_node
;
7735 else if (complain
& tf_warning
)
7736 maybe_warn_array_conv (loc
, convs
, expr
);
7738 /* If necessary, create a temporary.
7740 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
7741 that need temporaries, even when their types are reference
7742 compatible with the type of reference being bound, so the
7743 upcoming call to cp_build_addr_expr doesn't fail. */
7744 if (convs
->need_temporary_p
7745 || TREE_CODE (expr
) == CONSTRUCTOR
7746 || TREE_CODE (expr
) == VA_ARG_EXPR
)
7748 /* Otherwise, a temporary of type "cv1 T1" is created and
7749 initialized from the initializer expression using the rules
7750 for a non-reference copy-initialization (8.5). */
7752 tree type
= TREE_TYPE (ref_type
);
7753 cp_lvalue_kind lvalue
= lvalue_kind (expr
);
7755 gcc_assert (similar_type_p (type
, next_conversion (convs
)->type
));
7756 if (!CP_TYPE_CONST_NON_VOLATILE_P (type
)
7757 && !TYPE_REF_IS_RVALUE (ref_type
))
7759 /* If the reference is volatile or non-const, we
7760 cannot create a temporary. */
7761 if (complain
& tf_error
)
7763 if (lvalue
& clk_bitfield
)
7764 error_at (loc
, "cannot bind bit-field %qE to %qT",
7766 else if (lvalue
& clk_packed
)
7767 error_at (loc
, "cannot bind packed field %qE to %qT",
7770 error_at (loc
, "cannot bind rvalue %qE to %qT",
7773 return error_mark_node
;
7775 /* If the source is a packed field, and we must use a copy
7776 constructor, then building the target expr will require
7777 binding the field to the reference parameter to the
7778 copy constructor, and we'll end up with an infinite
7779 loop. If we can use a bitwise copy, then we'll be
7781 if ((lvalue
& clk_packed
)
7782 && CLASS_TYPE_P (type
)
7783 && type_has_nontrivial_copy_init (type
))
7785 error_at (loc
, "cannot bind packed field %qE to %qT",
7787 return error_mark_node
;
7789 if (lvalue
& clk_bitfield
)
7791 expr
= convert_bitfield_to_declared_type (expr
);
7792 expr
= fold_convert (type
, expr
);
7795 /* Creating &TARGET_EXPR<> in a template would break when
7796 tsubsting the expression, so use an IMPLICIT_CONV_EXPR
7797 instead. This can happen even when there's no class
7798 involved, e.g., when converting an integer to a reference
7800 if (processing_template_decl
)
7801 return build1 (IMPLICIT_CONV_EXPR
, totype
, expr
);
7802 expr
= build_target_expr_with_type (expr
, type
, complain
);
7805 /* Take the address of the thing to which we will bind the
7807 expr
= cp_build_addr_expr (expr
, complain
);
7808 if (expr
== error_mark_node
)
7809 return error_mark_node
;
7811 /* Convert it to a pointer to the type referred to by the
7812 reference. This will adjust the pointer if a derived to
7813 base conversion is being performed. */
7814 expr
= cp_convert (build_pointer_type (TREE_TYPE (ref_type
)),
7816 /* Convert the pointer to the desired reference type. */
7817 return build_nop (ref_type
, expr
);
7821 return decay_conversion (expr
, complain
);
7824 /* ??? Should the address of a transaction-safe pointer point to the TM
7825 clone, and this conversion look up the primary function? */
7826 return build_nop (totype
, expr
);
7829 /* Warn about deprecated conversion if appropriate. */
7830 if (complain
& tf_warning
)
7832 string_conv_p (totype
, expr
, 1);
7833 maybe_warn_array_conv (loc
, convs
, expr
);
7839 expr
= convert_to_base (expr
, totype
, !c_cast_p
,
7840 /*nonnull=*/false, complain
);
7841 return build_nop (totype
, expr
);
7844 return convert_ptrmem (totype
, expr
, /*allow_inverse_p=*/false,
7845 c_cast_p
, complain
);
7851 if (convs
->check_narrowing
7852 && !check_narrowing (totype
, expr
, complain
,
7853 convs
->check_narrowing_const_only
))
7854 return error_mark_node
;
7856 warning_sentinel
w (warn_zero_as_null_pointer_constant
);
7857 if (issue_conversion_warnings
)
7858 expr
= cp_convert_and_check (totype
, expr
, complain
);
7860 expr
= cp_convert (totype
, expr
, complain
);
7865 /* Wrapper for convert_like_internal that handles creating
7866 IMPLICIT_CONV_EXPR. */
7869 convert_like (conversion
*convs
, tree expr
, tree fn
, int argnum
,
7870 bool issue_conversion_warnings
, bool c_cast_p
,
7871 tsubst_flags_t complain
)
7873 /* Creating &TARGET_EXPR<> in a template breaks when substituting,
7874 and creating a CALL_EXPR in a template breaks in finish_call_expr
7875 so use an IMPLICIT_CONV_EXPR for this conversion. We would have
7876 created such codes e.g. when calling a user-defined conversion
7878 tree conv_expr
= NULL_TREE
;
7879 if (processing_template_decl
7880 && convs
->kind
!= ck_identity
7881 && (CLASS_TYPE_P (convs
->type
) || CLASS_TYPE_P (TREE_TYPE (expr
))))
7883 conv_expr
= build1 (IMPLICIT_CONV_EXPR
, convs
->type
, expr
);
7884 if (convs
->kind
!= ck_ref_bind
)
7885 conv_expr
= convert_from_reference (conv_expr
);
7888 /* Do the normal processing to give the bad_p errors. But we still
7889 need to return the IMPLICIT_CONV_EXPR, unless we're returning
7892 expr
= convert_like_internal (convs
, expr
, fn
, argnum
,
7893 issue_conversion_warnings
, c_cast_p
, complain
);
7894 if (expr
== error_mark_node
)
7895 return error_mark_node
;
7896 return conv_expr
? conv_expr
: expr
;
7899 /* Convenience wrapper for convert_like. */
7902 convert_like (conversion
*convs
, tree expr
, tsubst_flags_t complain
)
7904 return convert_like (convs
, expr
, NULL_TREE
, 0,
7905 /*issue_conversion_warnings=*/true,
7906 /*c_cast_p=*/false, complain
);
7909 /* Convenience wrapper for convert_like. */
7912 convert_like_with_context (conversion
*convs
, tree expr
, tree fn
, int argnum
,
7913 tsubst_flags_t complain
)
7915 return convert_like (convs
, expr
, fn
, argnum
,
7916 /*issue_conversion_warnings=*/true,
7917 /*c_cast_p=*/false, complain
);
7920 /* ARG is being passed to a varargs function. Perform any conversions
7921 required. Return the converted value. */
7924 convert_arg_to_ellipsis (tree arg
, tsubst_flags_t complain
)
7926 tree arg_type
= TREE_TYPE (arg
);
7927 location_t loc
= cp_expr_loc_or_input_loc (arg
);
7931 If the argument has integral or enumeration type that is subject
7932 to the integral promotions (_conv.prom_), or a floating-point
7933 type that is subject to the floating-point promotion
7934 (_conv.fpprom_), the value of the argument is converted to the
7935 promoted type before the call. */
7936 if (TREE_CODE (arg_type
) == REAL_TYPE
7937 && (TYPE_PRECISION (arg_type
)
7938 < TYPE_PRECISION (double_type_node
))
7939 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type
)))
7941 if ((complain
& tf_warning
)
7942 && warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
7943 warning_at (loc
, OPT_Wdouble_promotion
,
7944 "implicit conversion from %qH to %qI when passing "
7945 "argument to function",
7946 arg_type
, double_type_node
);
7947 arg
= mark_rvalue_use (arg
);
7948 arg
= convert_to_real_nofold (double_type_node
, arg
);
7950 else if (NULLPTR_TYPE_P (arg_type
))
7952 arg
= mark_rvalue_use (arg
);
7953 if (TREE_SIDE_EFFECTS (arg
))
7954 arg
= cp_build_compound_expr (arg
, null_pointer_node
, complain
);
7956 arg
= null_pointer_node
;
7958 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type
))
7960 if (SCOPED_ENUM_P (arg_type
))
7962 tree prom
= cp_convert (ENUM_UNDERLYING_TYPE (arg_type
), arg
,
7964 prom
= cp_perform_integral_promotions (prom
, complain
);
7965 if (abi_version_crosses (6)
7966 && TYPE_MODE (TREE_TYPE (prom
)) != TYPE_MODE (arg_type
)
7967 && (complain
& tf_warning
))
7968 warning_at (loc
, OPT_Wabi
, "scoped enum %qT passed through %<...%>"
7969 " as %qT before %<-fabi-version=6%>, %qT after",
7971 TREE_TYPE (prom
), ENUM_UNDERLYING_TYPE (arg_type
));
7972 if (!abi_version_at_least (6))
7976 arg
= cp_perform_integral_promotions (arg
, complain
);
7981 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7982 standard conversions are performed. */
7983 arg
= decay_conversion (arg
, complain
);
7985 arg
= require_complete_type_sfinae (arg
, complain
);
7986 arg_type
= TREE_TYPE (arg
);
7988 if (arg
!= error_mark_node
7989 /* In a template (or ill-formed code), we can have an incomplete type
7990 even after require_complete_type_sfinae, in which case we don't know
7991 whether it has trivial copy or not. */
7992 && COMPLETE_TYPE_P (arg_type
)
7993 && !cp_unevaluated_operand
)
7995 /* [expr.call] 5.2.2/7:
7996 Passing a potentially-evaluated argument of class type (Clause 9)
7997 with a non-trivial copy constructor or a non-trivial destructor
7998 with no corresponding parameter is conditionally-supported, with
7999 implementation-defined semantics.
8001 We support it as pass-by-invisible-reference, just like a normal
8004 If the call appears in the context of a sizeof expression,
8005 it is not potentially-evaluated. */
8006 if (type_has_nontrivial_copy_init (arg_type
)
8007 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type
))
8009 arg
= force_rvalue (arg
, complain
);
8010 if (complain
& tf_warning
)
8011 warning (OPT_Wconditionally_supported
,
8012 "passing objects of non-trivially-copyable "
8013 "type %q#T through %<...%> is conditionally supported",
8015 return build1 (ADDR_EXPR
, build_reference_type (arg_type
), arg
);
8017 /* Build up a real lvalue-to-rvalue conversion in case the
8018 copy constructor is trivial but not callable. */
8019 else if (CLASS_TYPE_P (arg_type
))
8020 force_rvalue (arg
, complain
);
8027 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
8030 build_x_va_arg (location_t loc
, tree expr
, tree type
)
8032 if (processing_template_decl
)
8034 tree r
= build_min (VA_ARG_EXPR
, type
, expr
);
8035 SET_EXPR_LOCATION (r
, loc
);
8039 type
= complete_type_or_else (type
, NULL_TREE
);
8041 if (expr
== error_mark_node
|| !type
)
8042 return error_mark_node
;
8044 expr
= mark_lvalue_use (expr
);
8046 if (TYPE_REF_P (type
))
8048 error ("cannot receive reference type %qT through %<...%>", type
);
8049 return error_mark_node
;
8052 if (type_has_nontrivial_copy_init (type
)
8053 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
8055 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
8056 it as pass by invisible reference. */
8057 warning_at (loc
, OPT_Wconditionally_supported
,
8058 "receiving objects of non-trivially-copyable type %q#T "
8059 "through %<...%> is conditionally-supported", type
);
8061 tree ref
= cp_build_reference_type (type
, false);
8062 expr
= build_va_arg (loc
, expr
, ref
);
8063 return convert_from_reference (expr
);
8066 tree ret
= build_va_arg (loc
, expr
, type
);
8067 if (CLASS_TYPE_P (type
))
8068 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
8069 know how to handle it. */
8070 ret
= get_target_expr (ret
);
8074 /* TYPE has been given to va_arg. Apply the default conversions which
8075 would have happened when passed via ellipsis. Return the promoted
8076 type, or the passed type if there is no change. */
8079 cxx_type_promotes_to (tree type
)
8083 /* Perform the array-to-pointer and function-to-pointer
8085 type
= type_decays_to (type
);
8087 promote
= type_promotes_to (type
);
8088 if (same_type_p (type
, promote
))
8094 /* ARG is a default argument expression being passed to a parameter of
8095 the indicated TYPE, which is a parameter to FN. PARMNUM is the
8096 zero-based argument number. Do any required conversions. Return
8097 the converted value. */
8099 static GTY(()) vec
<tree
, va_gc
> *default_arg_context
;
8101 push_defarg_context (tree fn
)
8102 { vec_safe_push (default_arg_context
, fn
); }
8105 pop_defarg_context (void)
8106 { default_arg_context
->pop (); }
8109 convert_default_arg (tree type
, tree arg
, tree fn
, int parmnum
,
8110 tsubst_flags_t complain
)
8115 /* See through clones. */
8116 fn
= DECL_ORIGIN (fn
);
8117 /* And inheriting ctors. */
8118 if (flag_new_inheriting_ctors
)
8119 fn
= strip_inheriting_ctors (fn
);
8121 /* Detect recursion. */
8122 FOR_EACH_VEC_SAFE_ELT (default_arg_context
, i
, t
)
8125 if (complain
& tf_error
)
8126 error ("recursive evaluation of default argument for %q#D", fn
);
8127 return error_mark_node
;
8130 /* If the ARG is an unparsed default argument expression, the
8131 conversion cannot be performed. */
8132 if (TREE_CODE (arg
) == DEFERRED_PARSE
)
8134 if (complain
& tf_error
)
8135 error ("call to %qD uses the default argument for parameter %P, which "
8136 "is not yet defined", fn
, parmnum
);
8137 return error_mark_node
;
8140 push_defarg_context (fn
);
8142 if (fn
&& DECL_TEMPLATE_INFO (fn
))
8143 arg
= tsubst_default_argument (fn
, parmnum
, type
, arg
, complain
);
8149 The names in the expression are bound, and the semantic
8150 constraints are checked, at the point where the default
8151 expressions appears.
8153 we must not perform access checks here. */
8154 push_deferring_access_checks (dk_no_check
);
8155 /* We must make a copy of ARG, in case subsequent processing
8156 alters any part of it. */
8157 arg
= break_out_target_exprs (arg
, /*clear location*/true);
8159 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_IMPLICIT
,
8160 ICR_DEFAULT_ARGUMENT
, fn
, parmnum
,
8162 arg
= convert_for_arg_passing (type
, arg
, complain
);
8163 pop_deferring_access_checks();
8165 pop_defarg_context ();
8170 /* Returns the type which will really be used for passing an argument of
8174 type_passed_as (tree type
)
8176 /* Pass classes with copy ctors by invisible reference. */
8177 if (TREE_ADDRESSABLE (type
))
8179 type
= build_reference_type (type
);
8180 /* There are no other pointers to this temporary. */
8181 type
= cp_build_qualified_type (type
, TYPE_QUAL_RESTRICT
);
8183 else if (targetm
.calls
.promote_prototypes (NULL_TREE
)
8184 && INTEGRAL_TYPE_P (type
)
8185 && COMPLETE_TYPE_P (type
)
8186 && tree_int_cst_lt (TYPE_SIZE (type
), TYPE_SIZE (integer_type_node
)))
8187 type
= integer_type_node
;
8192 /* Actually perform the appropriate conversion. */
8195 convert_for_arg_passing (tree type
, tree val
, tsubst_flags_t complain
)
8199 /* If VAL is a bitfield, then -- since it has already been converted
8200 to TYPE -- it cannot have a precision greater than TYPE.
8202 If it has a smaller precision, we must widen it here. For
8203 example, passing "int f:3;" to a function expecting an "int" will
8204 not result in any conversion before this point.
8206 If the precision is the same we must not risk widening. For
8207 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
8208 often have type "int", even though the C++ type for the field is
8209 "long long". If the value is being passed to a function
8210 expecting an "int", then no conversions will be required. But,
8211 if we call convert_bitfield_to_declared_type, the bitfield will
8212 be converted to "long long". */
8213 bitfield_type
= is_bitfield_expr_with_lowered_type (val
);
8215 && TYPE_PRECISION (TREE_TYPE (val
)) < TYPE_PRECISION (type
))
8216 val
= convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type
), val
);
8218 if (val
== error_mark_node
)
8220 /* Pass classes with copy ctors by invisible reference. */
8221 else if (TREE_ADDRESSABLE (type
))
8222 val
= build1 (ADDR_EXPR
, build_reference_type (type
), val
);
8223 else if (targetm
.calls
.promote_prototypes (NULL_TREE
)
8224 && INTEGRAL_TYPE_P (type
)
8225 && COMPLETE_TYPE_P (type
)
8226 && tree_int_cst_lt (TYPE_SIZE (type
), TYPE_SIZE (integer_type_node
)))
8227 val
= cp_perform_integral_promotions (val
, complain
);
8228 if (complain
& tf_warning
)
8230 if (warn_suggest_attribute_format
)
8232 tree rhstype
= TREE_TYPE (val
);
8233 const enum tree_code coder
= TREE_CODE (rhstype
);
8234 const enum tree_code codel
= TREE_CODE (type
);
8235 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
8237 && check_missing_format_attribute (type
, rhstype
))
8238 warning (OPT_Wsuggest_attribute_format
,
8239 "argument of function call might be a candidate "
8240 "for a format attribute");
8242 maybe_warn_parm_abi (type
, cp_expr_loc_or_input_loc (val
));
8245 if (complain
& tf_warning
)
8246 warn_for_address_or_pointer_of_packed_member (type
, val
);
8251 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
8252 which just decay_conversion or no conversions at all should be done.
8253 This is true for some builtins which don't act like normal functions.
8254 Return 2 if no conversions at all should be done, 1 if just
8255 decay_conversion. Return 3 for special treatment of the 3rd argument
8256 for __builtin_*_overflow_p. */
8259 magic_varargs_p (tree fn
)
8261 if (DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
8262 switch (DECL_FUNCTION_CODE (fn
))
8264 case BUILT_IN_CLASSIFY_TYPE
:
8265 case BUILT_IN_CONSTANT_P
:
8266 case BUILT_IN_NEXT_ARG
:
8267 case BUILT_IN_VA_START
:
8270 case BUILT_IN_ADD_OVERFLOW_P
:
8271 case BUILT_IN_SUB_OVERFLOW_P
:
8272 case BUILT_IN_MUL_OVERFLOW_P
:
8276 return lookup_attribute ("type generic",
8277 TYPE_ATTRIBUTES (TREE_TYPE (fn
))) != 0;
8283 /* Returns the decl of the dispatcher function if FN is a function version. */
8286 get_function_version_dispatcher (tree fn
)
8288 tree dispatcher_decl
= NULL
;
8290 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
8291 && DECL_FUNCTION_VERSIONED (fn
));
8293 gcc_assert (targetm
.get_function_versions_dispatcher
);
8294 dispatcher_decl
= targetm
.get_function_versions_dispatcher (fn
);
8296 if (dispatcher_decl
== NULL
)
8298 error_at (input_location
, "use of multiversioned function "
8299 "without a default");
8303 retrofit_lang_decl (dispatcher_decl
);
8304 gcc_assert (dispatcher_decl
!= NULL
);
8305 return dispatcher_decl
;
8308 /* fn is a function version dispatcher that is marked used. Mark all the
8309 semantically identical function versions it will dispatch as used. */
8312 mark_versions_used (tree fn
)
8314 struct cgraph_node
*node
;
8315 struct cgraph_function_version_info
*node_v
;
8316 struct cgraph_function_version_info
*it_v
;
8318 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8320 node
= cgraph_node::get (fn
);
8324 gcc_assert (node
->dispatcher_function
);
8326 node_v
= node
->function_version ();
8330 /* All semantically identical versions are chained. Traverse and mark each
8331 one of them as used. */
8332 it_v
= node_v
->next
;
8333 while (it_v
!= NULL
)
8335 mark_used (it_v
->this_node
->decl
);
8340 /* Build a call to "the copy constructor" for the type of A, even if it
8341 wouldn't be selected by normal overload resolution. Used for
8345 call_copy_ctor (tree a
, tsubst_flags_t complain
)
8347 tree ctype
= TYPE_MAIN_VARIANT (TREE_TYPE (a
));
8348 tree binfo
= TYPE_BINFO (ctype
);
8349 tree copy
= get_copy_ctor (ctype
, complain
);
8350 copy
= build_baselink (binfo
, binfo
, copy
, NULL_TREE
);
8351 tree ob
= build_dummy_object (ctype
);
8352 releasing_vec
args (make_tree_vector_single (a
));
8353 tree r
= build_new_method_call (ob
, copy
, &args
, NULL_TREE
,
8354 LOOKUP_NORMAL
, NULL
, complain
);
8358 /* Return true iff T refers to a base or potentially-overlapping field, which
8359 cannot be used for return by invisible reference. We avoid doing C++17
8360 mandatory copy elision when this is true.
8362 This returns true even if the type of T has no tail padding that other data
8363 could be allocated into, because that depends on the particular ABI.
8364 unsafe_copy_elision_p, below, does consider whether there is padding. */
8367 unsafe_return_slot_p (tree t
)
8370 if (TREE_CODE (t
) == ADDR_EXPR
)
8371 t
= TREE_OPERAND (t
, 0);
8372 if (TREE_CODE (t
) == COMPONENT_REF
)
8373 t
= TREE_OPERAND (t
, 1);
8374 if (TREE_CODE (t
) != FIELD_DECL
)
8376 if (!CLASS_TYPE_P (TREE_TYPE (t
)))
8377 /* The middle-end will do the right thing for scalar types. */
8379 return (DECL_FIELD_IS_BASE (t
)
8380 || lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t
)));
8383 /* We can't elide a copy from a function returning by value to a
8384 potentially-overlapping subobject, as the callee might clobber tail padding.
8385 Return true iff this could be that case. */
8388 unsafe_copy_elision_p (tree target
, tree exp
)
8390 /* Copy elision only happens with a TARGET_EXPR. */
8391 if (TREE_CODE (exp
) != TARGET_EXPR
)
8393 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (exp
));
8394 /* It's safe to elide the copy for a class with no tail padding. */
8395 if (!is_empty_class (type
)
8396 && tree_int_cst_equal (TYPE_SIZE (type
), CLASSTYPE_SIZE (type
)))
8398 /* It's safe to elide the copy if we aren't initializing a base object. */
8399 if (!unsafe_return_slot_p (target
))
8401 tree init
= TARGET_EXPR_INITIAL (exp
);
8402 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
8403 while (TREE_CODE (init
) == COMPOUND_EXPR
)
8404 init
= TREE_OPERAND (init
, 1);
8405 if (TREE_CODE (init
) == COND_EXPR
)
8407 /* We'll end up copying from each of the arms of the COND_EXPR directly
8408 into the target, so look at them. */
8409 if (tree op
= TREE_OPERAND (init
, 1))
8410 if (unsafe_copy_elision_p (target
, op
))
8412 return unsafe_copy_elision_p (target
, TREE_OPERAND (init
, 2));
8414 return (TREE_CODE (init
) == AGGR_INIT_EXPR
8415 && !AGGR_INIT_VIA_CTOR_P (init
));
8418 /* True iff C is a conversion that binds a reference to a prvalue. */
8421 conv_binds_ref_to_prvalue (conversion
*c
)
8423 if (c
->kind
!= ck_ref_bind
)
8425 if (c
->need_temporary_p
)
8428 c
= next_conversion (c
);
8430 if (c
->kind
== ck_rvalue
)
8432 if (c
->kind
== ck_user
&& !TYPE_REF_P (c
->type
))
8434 if (c
->kind
== ck_identity
&& c
->u
.expr
8435 && TREE_CODE (c
->u
.expr
) == TARGET_EXPR
)
8441 /* True iff converting EXPR to a reference type TYPE does not involve
8442 creating a temporary. */
8445 ref_conv_binds_directly_p (tree type
, tree expr
)
8447 gcc_assert (TYPE_REF_P (type
));
8449 /* Get the high-water mark for the CONVERSION_OBSTACK. */
8450 void *p
= conversion_obstack_alloc (0);
8452 conversion
*conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
8454 LOOKUP_IMPLICIT
, tf_none
);
8455 bool ret
= conv
&& !conv
->bad_p
&& !conv_binds_ref_to_prvalue (conv
);
8457 /* Free all the conversions we allocated. */
8458 obstack_free (&conversion_obstack
, p
);
8463 /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
8464 class type or a pointer to class type. If NO_PTR_DEREF is true and
8465 INSTANCE has pointer type, clobber the pointer rather than what it points
8469 build_trivial_dtor_call (tree instance
, bool no_ptr_deref
)
8471 gcc_assert (!is_dummy_object (instance
));
8473 if (!flag_lifetime_dse
)
8476 return fold_convert (void_type_node
, instance
);
8479 if (INDIRECT_TYPE_P (TREE_TYPE (instance
))
8480 && (!no_ptr_deref
|| TYPE_REF_P (TREE_TYPE (instance
))))
8482 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance
))))
8484 instance
= cp_build_fold_indirect_ref (instance
);
8487 /* A trivial destructor should still clobber the object. */
8488 tree clobber
= build_clobber (TREE_TYPE (instance
));
8489 return build2 (MODIFY_EXPR
, void_type_node
,
8493 /* Subroutine of the various build_*_call functions. Overload resolution
8494 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
8495 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
8496 bitmask of various LOOKUP_* flags which apply to the call itself. */
8499 build_over_call (struct z_candidate
*cand
, int flags
, tsubst_flags_t complain
)
8502 const vec
<tree
, va_gc
> *args
= cand
->args
;
8503 tree first_arg
= cand
->first_arg
;
8504 conversion
**convs
= cand
->convs
;
8506 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
8511 unsigned int arg_index
= 0;
8515 bool already_used
= false;
8517 /* In a template, there is no need to perform all of the work that
8518 is normally done. We are only interested in the type of the call
8519 expression, i.e., the return type of the function. Any semantic
8520 errors will be deferred until the template is instantiated. */
8521 if (processing_template_decl
)
8525 const tree
*argarray
;
8528 if (undeduced_auto_decl (fn
))
8529 mark_used (fn
, complain
);
8531 /* Otherwise set TREE_USED for the benefit of -Wunused-function.
8535 return_type
= TREE_TYPE (TREE_TYPE (fn
));
8536 nargs
= vec_safe_length (args
);
8537 if (first_arg
== NULL_TREE
)
8538 argarray
= args
->address ();
8546 alcarray
= XALLOCAVEC (tree
, nargs
);
8547 alcarray
[0] = build_this (first_arg
);
8548 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
8549 alcarray
[ix
+ 1] = arg
;
8550 argarray
= alcarray
;
8553 addr
= build_addr_func (fn
, complain
);
8554 if (addr
== error_mark_node
)
8555 return error_mark_node
;
8556 expr
= build_call_array_loc (input_location
, return_type
,
8557 addr
, nargs
, argarray
);
8558 if (TREE_THIS_VOLATILE (fn
) && cfun
)
8559 current_function_returns_abnormally
= 1;
8560 if (TREE_CODE (fn
) == FUNCTION_DECL
8561 && DECL_IMMEDIATE_FUNCTION_P (fn
)
8562 && cp_unevaluated_operand
== 0
8563 && (current_function_decl
== NULL_TREE
8564 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
))
8565 && (current_binding_level
->kind
!= sk_function_parms
8566 || !current_binding_level
->immediate_fn_ctx_p
))
8568 tree obj_arg
= NULL_TREE
, exprimm
= expr
;
8569 if (DECL_CONSTRUCTOR_P (fn
))
8570 obj_arg
= first_arg
;
8572 && is_dummy_object (obj_arg
)
8573 && !type_dependent_expression_p (obj_arg
))
8575 exprimm
= build_cplus_new (DECL_CONTEXT (fn
), expr
, complain
);
8576 obj_arg
= NULL_TREE
;
8578 /* Look through *(const T *)&obj. */
8579 else if (obj_arg
&& TREE_CODE (obj_arg
) == INDIRECT_REF
)
8581 tree addr
= TREE_OPERAND (obj_arg
, 0);
8583 if (TREE_CODE (addr
) == ADDR_EXPR
)
8585 tree typeo
= TREE_TYPE (obj_arg
);
8586 tree typei
= TREE_TYPE (TREE_OPERAND (addr
, 0));
8587 if (same_type_ignoring_top_level_qualifiers_p (typeo
, typei
))
8588 obj_arg
= TREE_OPERAND (addr
, 0);
8591 fold_non_dependent_expr (exprimm
, complain
,
8592 /*manifestly_const_eval=*/true,
8595 return convert_from_reference (expr
);
8598 /* Give any warnings we noticed during overload resolution. */
8599 if (cand
->warnings
&& (complain
& tf_warning
))
8601 struct candidate_warning
*w
;
8602 for (w
= cand
->warnings
; w
; w
= w
->next
)
8603 joust (cand
, w
->loser
, 1, complain
);
8606 /* Core issue 2327: P0135 doesn't say how to handle the case where the
8607 argument to the copy constructor ends up being a prvalue after
8608 conversion. Let's do the normal processing, but pretend we aren't
8609 actually using the copy constructor. */
8610 bool force_elide
= false;
8611 if (cxx_dialect
>= cxx17
8612 && cand
->num_convs
== 1
8613 && DECL_COMPLETE_CONSTRUCTOR_P (fn
)
8614 && (DECL_COPY_CONSTRUCTOR_P (fn
)
8615 || DECL_MOVE_CONSTRUCTOR_P (fn
))
8616 && !unsafe_return_slot_p (first_arg
)
8617 && conv_binds_ref_to_prvalue (convs
[0]))
8620 goto not_really_used
;
8623 /* OK, we're actually calling this inherited constructor; set its deletedness
8624 appropriately. We can get away with doing this here because calling is
8625 the only way to refer to a constructor. */
8626 if (DECL_INHERITED_CTOR (fn
))
8627 deduce_inheriting_ctor (fn
);
8629 /* Make =delete work with SFINAE. */
8630 if (DECL_DELETED_FN (fn
))
8632 if (complain
& tf_error
)
8634 return error_mark_node
;
8637 if (DECL_FUNCTION_MEMBER_P (fn
))
8640 /* If FN is a template function, two cases must be considered.
8645 template <class T> void f();
8647 template <class T> struct B {
8651 struct C : A, B<int> {
8653 using B<int>::g; // #2
8656 In case #1 where `A::f' is a member template, DECL_ACCESS is
8657 recorded in the primary template but not in its specialization.
8658 We check access of FN using its primary template.
8660 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
8661 because it is a member of class template B, DECL_ACCESS is
8662 recorded in the specialization `B<int>::g'. We cannot use its
8663 primary template because `B<T>::g' and `B<int>::g' may have
8664 different access. */
8665 if (DECL_TEMPLATE_INFO (fn
)
8666 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
8667 access_fn
= DECL_TI_TEMPLATE (fn
);
8670 if (!perform_or_defer_access_check (cand
->access_path
, access_fn
,
8672 return error_mark_node
;
8675 /* If we're checking for implicit delete, don't bother with argument
8677 if (flags
& LOOKUP_SPECULATIVE
)
8679 if (cand
->viable
== 1)
8681 else if (!(complain
& tf_error
))
8682 /* Reject bad conversions now. */
8683 return error_mark_node
;
8684 /* else continue to get conversion error. */
8689 /* N3276 magic doesn't apply to nested calls. */
8690 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
8691 complain
&= ~tf_decltype
;
8692 /* No-Cleanup doesn't apply to nested calls either. */
8693 tsubst_flags_t no_cleanup_complain
= complain
;
8694 complain
&= ~tf_no_cleanup
;
8696 /* Find maximum size of vector to hold converted arguments. */
8697 parmlen
= list_length (parm
);
8698 nargs
= vec_safe_length (args
) + (first_arg
!= NULL_TREE
? 1 : 0);
8699 if (parmlen
> nargs
)
8701 argarray
= XALLOCAVEC (tree
, nargs
);
8703 /* The implicit parameters to a constructor are not considered by overload
8704 resolution, and must be of the proper type. */
8705 if (DECL_CONSTRUCTOR_P (fn
))
8708 if (first_arg
!= NULL_TREE
)
8710 object_arg
= first_arg
;
8711 first_arg
= NULL_TREE
;
8715 object_arg
= (*args
)[arg_index
];
8718 argarray
[j
++] = build_this (object_arg
);
8719 parm
= TREE_CHAIN (parm
);
8720 /* We should never try to call the abstract constructor. */
8721 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn
));
8723 if (DECL_HAS_VTT_PARM_P (fn
))
8725 argarray
[j
++] = (*args
)[arg_index
];
8727 parm
= TREE_CHAIN (parm
);
8730 if (cxx_dialect
< cxx20
8731 && (cand
->flags
& LOOKUP_PREFER_RVALUE
))
8733 /* The implicit move specified in 15.8.3/3 fails "...if the type of
8734 the first parameter of the selected constructor is not an rvalue
8735 reference to the object's type (possibly cv-qualified)...." */
8736 gcc_assert (!(complain
& tf_error
));
8737 tree ptype
= convs
[0]->type
;
8738 /* Allow calling a by-value converting constructor even though it
8739 isn't permitted by the above, because we've allowed it since GCC 5
8740 (PR58051) and it's allowed in C++20. But don't call a copy
8742 if ((TYPE_REF_P (ptype
) && !TYPE_REF_IS_RVALUE (ptype
))
8743 || CONVERSION_RANK (convs
[0]) > cr_exact
)
8744 return error_mark_node
;
8747 /* Bypass access control for 'this' parameter. */
8748 else if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
8750 tree arg
= build_this (first_arg
!= NULL_TREE
8752 : (*args
)[arg_index
]);
8753 tree argtype
= TREE_TYPE (arg
);
8755 if (arg
== error_mark_node
)
8756 return error_mark_node
;
8758 if (convs
[i
]->bad_p
)
8760 if (complain
& tf_error
)
8762 auto_diagnostic_group d
;
8763 if (permerror (input_location
, "passing %qT as %<this%> "
8764 "argument discards qualifiers",
8765 TREE_TYPE (argtype
)))
8766 inform (DECL_SOURCE_LOCATION (fn
), " in call to %qD", fn
);
8769 return error_mark_node
;
8772 /* The class where FN is defined. */
8773 tree ctx
= DECL_CONTEXT (fn
);
8775 /* See if the function member or the whole class type is declared
8776 final and the call can be devirtualized. */
8777 if (DECL_FINAL_P (fn
) || CLASSTYPE_FINAL (ctx
))
8778 flags
|= LOOKUP_NONVIRTUAL
;
8780 /* [class.mfct.non-static]: If a non-static member function of a class
8781 X is called for an object that is not of type X, or of a type
8782 derived from X, the behavior is undefined.
8784 So we can assume that anything passed as 'this' is non-null, and
8785 optimize accordingly. */
8786 /* Check that the base class is accessible. */
8787 if (!accessible_base_p (TREE_TYPE (argtype
),
8788 BINFO_TYPE (cand
->conversion_path
), true))
8790 if (complain
& tf_error
)
8791 error ("%qT is not an accessible base of %qT",
8792 BINFO_TYPE (cand
->conversion_path
),
8793 TREE_TYPE (argtype
));
8795 return error_mark_node
;
8797 /* If fn was found by a using declaration, the conversion path
8798 will be to the derived class, not the base declaring fn. We
8799 must convert to the base. */
8800 tree base_binfo
= cand
->conversion_path
;
8801 if (BINFO_TYPE (base_binfo
) != ctx
)
8803 base_binfo
= lookup_base (base_binfo
, ctx
, ba_unique
, NULL
, complain
);
8804 if (base_binfo
== error_mark_node
)
8805 return error_mark_node
;
8807 tree converted_arg
= build_base_path (PLUS_EXPR
, arg
,
8808 base_binfo
, 1, complain
);
8810 /* If we know the dynamic type of the object, look up the final overrider
8812 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0
8813 && resolves_to_fixed_type_p (arg
))
8815 fn
= lookup_vfn_in_binfo (DECL_VINDEX (fn
), base_binfo
);
8816 flags
|= LOOKUP_NONVIRTUAL
;
8819 argarray
[j
++] = converted_arg
;
8820 parm
= TREE_CHAIN (parm
);
8821 if (first_arg
!= NULL_TREE
)
8822 first_arg
= NULL_TREE
;
8829 gcc_assert (first_arg
== NULL_TREE
);
8830 for (; arg_index
< vec_safe_length (args
) && parm
;
8831 parm
= TREE_CHAIN (parm
), ++arg_index
, ++i
)
8833 tree type
= TREE_VALUE (parm
);
8834 tree arg
= (*args
)[arg_index
];
8835 bool conversion_warning
= true;
8839 /* If the argument is NULL and used to (implicitly) instantiate a
8840 template function (and bind one of the template arguments to
8841 the type of 'long int'), we don't want to warn about passing NULL
8842 to non-pointer argument.
8843 For example, if we have this template function:
8845 template<typename T> void func(T x) {}
8847 we want to warn (when -Wconversion is enabled) in this case:
8853 but not in this case:
8859 if (null_node_p (arg
)
8860 && DECL_TEMPLATE_INFO (fn
)
8861 && cand
->template_decl
8862 && !cand
->explicit_targs
)
8863 conversion_warning
= false;
8865 /* Set user_conv_p on the argument conversions, so rvalue/base handling
8866 knows not to allow any more UDCs. This needs to happen after we
8867 process cand->warnings. */
8868 if (flags
& LOOKUP_NO_CONVERSION
)
8869 conv
->user_conv_p
= true;
8871 tsubst_flags_t arg_complain
= complain
;
8872 if (!conversion_warning
)
8873 arg_complain
&= ~tf_warning
;
8875 val
= convert_like_with_context (conv
, arg
, fn
, i
- is_method
,
8877 val
= convert_for_arg_passing (type
, val
, arg_complain
);
8879 if (val
== error_mark_node
)
8880 return error_mark_node
;
8882 argarray
[j
++] = val
;
8885 /* Default arguments */
8886 for (; parm
&& parm
!= void_list_node
; parm
= TREE_CHAIN (parm
), i
++)
8888 if (TREE_VALUE (parm
) == error_mark_node
)
8889 return error_mark_node
;
8890 val
= convert_default_arg (TREE_VALUE (parm
),
8891 TREE_PURPOSE (parm
),
8894 if (val
== error_mark_node
)
8895 return error_mark_node
;
8896 argarray
[j
++] = val
;
8900 int magic
= magic_varargs_p (fn
);
8901 for (; arg_index
< vec_safe_length (args
); ++arg_index
)
8903 tree a
= (*args
)[arg_index
];
8904 if ((magic
== 3 && arg_index
== 2) || magic
== 2)
8906 /* Do no conversions for certain magic varargs. */
8907 a
= mark_type_use (a
);
8908 if (TREE_CODE (a
) == FUNCTION_DECL
&& reject_gcc_builtin (a
))
8909 return error_mark_node
;
8911 else if (magic
!= 0)
8912 /* For other magic varargs only do decay_conversion. */
8913 a
= decay_conversion (a
, complain
);
8914 else if (DECL_CONSTRUCTOR_P (fn
)
8915 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn
),
8918 /* Avoid infinite recursion trying to call A(...). */
8919 if (complain
& tf_error
)
8920 /* Try to call the actual copy constructor for a good error. */
8921 call_copy_ctor (a
, complain
);
8922 return error_mark_node
;
8925 a
= convert_arg_to_ellipsis (a
, complain
);
8926 if (a
== error_mark_node
)
8927 return error_mark_node
;
8931 gcc_assert (j
<= nargs
);
8934 /* Avoid performing argument transformation if warnings are disabled.
8935 When tf_warning is set and at least one of the warnings is active
8936 the check_function_arguments function might warn about something. */
8938 bool warned_p
= false;
8939 if ((complain
& tf_warning
)
8942 || warn_suggest_attribute_format
8945 tree
*fargs
= (!nargs
? argarray
8946 : (tree
*) alloca (nargs
* sizeof (tree
)));
8947 for (j
= 0; j
< nargs
; j
++)
8949 /* For -Wformat undo the implicit passing by hidden reference
8950 done by convert_arg_to_ellipsis. */
8951 if (TREE_CODE (argarray
[j
]) == ADDR_EXPR
8952 && TYPE_REF_P (TREE_TYPE (argarray
[j
])))
8953 fargs
[j
] = TREE_OPERAND (argarray
[j
], 0);
8955 fargs
[j
] = argarray
[j
];
8958 warned_p
= check_function_arguments (input_location
, fn
, TREE_TYPE (fn
),
8959 nargs
, fargs
, NULL
);
8962 if (DECL_INHERITED_CTOR (fn
))
8964 /* Check for passing ellipsis arguments to an inherited constructor. We
8965 could handle this by open-coding the inherited constructor rather than
8966 defining it, but let's not bother now. */
8967 if (!cp_unevaluated_operand
8969 && cand
->convs
[cand
->num_convs
-1]->ellipsis_p
)
8971 if (complain
& tf_error
)
8973 sorry ("passing arguments to ellipsis of inherited constructor "
8975 inform (DECL_SOURCE_LOCATION (cand
->fn
), "declared here");
8977 return error_mark_node
;
8980 /* A base constructor inheriting from a virtual base doesn't get the
8981 inherited arguments, just this and __vtt. */
8982 if (ctor_omit_inherited_parms (fn
))
8986 /* Avoid actually calling copy constructors and copy assignment operators,
8989 if (! flag_elide_constructors
&& !force_elide
)
8990 /* Do things the hard way. */;
8991 else if (cand
->num_convs
== 1
8992 && (DECL_COPY_CONSTRUCTOR_P (fn
)
8993 || DECL_MOVE_CONSTRUCTOR_P (fn
))
8994 /* It's unsafe to elide the constructor when handling
8995 a noexcept-expression, it may evaluate to the wrong
8996 value (c++/53025). */
8997 && (force_elide
|| cp_noexcept_operand
== 0))
9000 tree arg
= argarray
[num_artificial_parms_for (fn
)];
9001 tree fa
= argarray
[0];
9002 bool trivial
= trivial_fn_p (fn
);
9004 /* Pull out the real argument, disregarding const-correctness. */
9006 /* Strip the reference binding for the constructor parameter. */
9007 if (CONVERT_EXPR_P (targ
)
9008 && TYPE_REF_P (TREE_TYPE (targ
)))
9009 targ
= TREE_OPERAND (targ
, 0);
9010 /* But don't strip any other reference bindings; binding a temporary to a
9011 reference prevents copy elision. */
9012 while ((CONVERT_EXPR_P (targ
)
9013 && !TYPE_REF_P (TREE_TYPE (targ
)))
9014 || TREE_CODE (targ
) == NON_LVALUE_EXPR
)
9015 targ
= TREE_OPERAND (targ
, 0);
9016 if (TREE_CODE (targ
) == ADDR_EXPR
)
9018 targ
= TREE_OPERAND (targ
, 0);
9019 if (!same_type_ignoring_top_level_qualifiers_p
9020 (TREE_TYPE (TREE_TYPE (arg
)), TREE_TYPE (targ
)))
9029 arg
= cp_build_fold_indirect_ref (arg
);
9031 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
9032 potentially-overlapping subobject. */
9033 if (CHECKING_P
&& cxx_dialect
>= cxx17
)
9034 gcc_assert (TREE_CODE (arg
) != TARGET_EXPR
9036 /* It's from binding the ref parm to a packed field. */
9037 || convs
[0]->need_temporary_p
9039 /* See unsafe_copy_elision_p. */
9040 || unsafe_return_slot_p (fa
));
9042 bool unsafe
= unsafe_copy_elision_p (fa
, arg
);
9043 bool eliding_temp
= (TREE_CODE (arg
) == TARGET_EXPR
&& !unsafe
);
9045 /* [class.copy]: the copy constructor is implicitly defined even if the
9046 implementation elided its use. But don't warn about deprecation when
9047 eliding a temporary, as then no copy is actually performed. */
9048 warning_sentinel
s (warn_deprecated_copy
, eliding_temp
);
9050 /* The language says this isn't called. */;
9053 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
9054 return error_mark_node
;
9055 already_used
= true;
9058 cp_warn_deprecated_use (fn
, complain
);
9060 /* If we're creating a temp and we already have one, don't create a
9061 new one. If we're not creating a temp but we get one, use
9062 INIT_EXPR to collapse the temp into our target. Otherwise, if the
9063 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
9064 temp or an INIT_EXPR otherwise. */
9065 if (is_dummy_object (fa
))
9067 if (TREE_CODE (arg
) == TARGET_EXPR
)
9070 return force_target_expr (DECL_CONTEXT (fn
), arg
, complain
);
9072 else if ((trivial
|| TREE_CODE (arg
) == TARGET_EXPR
)
9075 tree to
= cp_build_fold_indirect_ref (fa
);
9076 val
= build2 (INIT_EXPR
, DECL_CONTEXT (fn
), to
, arg
);
9080 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
9081 && DECL_OVERLOADED_OPERATOR_IS (fn
, NOP_EXPR
)
9082 && trivial_fn_p (fn
))
9084 tree to
= cp_build_fold_indirect_ref (argarray
[0]);
9085 tree type
= TREE_TYPE (to
);
9086 tree as_base
= CLASSTYPE_AS_BASE (type
);
9087 tree arg
= argarray
[1];
9088 location_t loc
= cp_expr_loc_or_input_loc (arg
);
9090 if (is_really_empty_class (type
, /*ignore_vptr*/true))
9092 /* Avoid copying empty classes. */
9093 val
= build2 (COMPOUND_EXPR
, type
, arg
, to
);
9094 TREE_NO_WARNING (val
) = 1;
9096 else if (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (as_base
)))
9098 if (is_std_init_list (type
)
9099 && conv_binds_ref_to_prvalue (convs
[1]))
9100 warning_at (loc
, OPT_Winit_list_lifetime
,
9101 "assignment from temporary %<initializer_list%> does "
9102 "not extend the lifetime of the underlying array");
9103 arg
= cp_build_fold_indirect_ref (arg
);
9104 val
= build2 (MODIFY_EXPR
, TREE_TYPE (to
), to
, arg
);
9108 /* We must only copy the non-tail padding parts. */
9110 tree array_type
, alias_set
;
9112 arg2
= TYPE_SIZE_UNIT (as_base
);
9113 to
= cp_stabilize_reference (to
);
9114 arg0
= cp_build_addr_expr (to
, complain
);
9116 array_type
= build_array_type (unsigned_char_type_node
,
9118 (size_binop (MINUS_EXPR
,
9119 arg2
, size_int (1))));
9120 alias_set
= build_int_cst (build_pointer_type (type
), 0);
9121 t
= build2 (MODIFY_EXPR
, void_type_node
,
9122 build2 (MEM_REF
, array_type
, arg0
, alias_set
),
9123 build2 (MEM_REF
, array_type
, arg
, alias_set
));
9124 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (to
), t
, to
);
9125 TREE_NO_WARNING (val
) = 1;
9128 cp_warn_deprecated_use (fn
, complain
);
9132 else if (trivial_fn_p (fn
))
9134 if (DECL_DESTRUCTOR_P (fn
))
9135 return build_trivial_dtor_call (argarray
[0]);
9136 else if (default_ctor_p (fn
))
9138 if (is_dummy_object (argarray
[0]))
9139 return force_target_expr (DECL_CONTEXT (fn
), void_node
,
9140 no_cleanup_complain
);
9142 return cp_build_fold_indirect_ref (argarray
[0]);
9146 gcc_assert (!force_elide
);
9149 && !mark_used (fn
, complain
))
9150 return error_mark_node
;
9152 /* Warn if the built-in writes to an object of a non-trivial type. */
9153 if (warn_class_memaccess
9154 && vec_safe_length (args
) >= 2
9155 && DECL_BUILT_IN_CLASS (fn
) == BUILT_IN_NORMAL
)
9156 maybe_warn_class_memaccess (input_location
, fn
, args
);
9158 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0)
9161 tree binfo
= lookup_base (TREE_TYPE (TREE_TYPE (argarray
[0])),
9163 ba_any
, NULL
, complain
);
9164 gcc_assert (binfo
&& binfo
!= error_mark_node
);
9166 argarray
[0] = build_base_path (PLUS_EXPR
, argarray
[0], binfo
, 1,
9168 if (TREE_SIDE_EFFECTS (argarray
[0]))
9169 argarray
[0] = save_expr (argarray
[0]);
9170 t
= build_pointer_type (TREE_TYPE (fn
));
9171 fn
= build_vfn_ref (argarray
[0], DECL_VINDEX (fn
));
9176 /* If FN is marked deprecated, then we've already issued a deprecated-use
9177 warning from mark_used above, so avoid redundantly issuing another one
9178 from build_addr_func. */
9179 warning_sentinel
w (warn_deprecated_decl
);
9181 fn
= build_addr_func (fn
, complain
);
9182 if (fn
== error_mark_node
)
9183 return error_mark_node
;
9186 tree call
= build_cxx_call (fn
, nargs
, argarray
, complain
|decltype_flag
);
9187 if (call
== error_mark_node
)
9189 if (cand
->flags
& LOOKUP_LIST_INIT_CTOR
)
9191 tree c
= extract_call_expr (call
);
9192 /* build_new_op_1 will clear this when appropriate. */
9193 CALL_EXPR_ORDERED_ARGS (c
) = true;
9197 tree c
= extract_call_expr (call
);
9198 if (TREE_CODE (c
) == CALL_EXPR
)
9199 TREE_NO_WARNING (c
) = 1;
9201 if (TREE_CODE (fn
) == ADDR_EXPR
)
9203 tree fndecl
= STRIP_TEMPLATE (TREE_OPERAND (fn
, 0));
9204 if (TREE_CODE (fndecl
) == FUNCTION_DECL
9205 && DECL_IMMEDIATE_FUNCTION_P (fndecl
)
9206 && cp_unevaluated_operand
== 0
9207 && (current_function_decl
== NULL_TREE
9208 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
))
9209 && (current_binding_level
->kind
!= sk_function_parms
9210 || !current_binding_level
->immediate_fn_ctx_p
))
9212 tree obj_arg
= NULL_TREE
;
9213 if (DECL_CONSTRUCTOR_P (fndecl
))
9214 obj_arg
= cand
->first_arg
? cand
->first_arg
: (*args
)[0];
9215 if (obj_arg
&& is_dummy_object (obj_arg
))
9217 call
= build_cplus_new (DECL_CONTEXT (fndecl
), call
, complain
);
9218 obj_arg
= NULL_TREE
;
9220 /* Look through *(const T *)&obj. */
9221 else if (obj_arg
&& TREE_CODE (obj_arg
) == INDIRECT_REF
)
9223 tree addr
= TREE_OPERAND (obj_arg
, 0);
9225 if (TREE_CODE (addr
) == ADDR_EXPR
)
9227 tree typeo
= TREE_TYPE (obj_arg
);
9228 tree typei
= TREE_TYPE (TREE_OPERAND (addr
, 0));
9229 if (same_type_ignoring_top_level_qualifiers_p (typeo
, typei
))
9230 obj_arg
= TREE_OPERAND (addr
, 0);
9233 call
= cxx_constant_value (call
, obj_arg
);
9234 if (obj_arg
&& !error_operand_p (call
))
9235 call
= build2 (INIT_EXPR
, void_type_node
, obj_arg
, call
);
9244 /* Return the DECL of the first non-static subobject of class TYPE
9245 that satisfies the predicate PRED or null if none can be found. */
9247 template <class Predicate
>
9249 first_non_static_field (tree type
, Predicate pred
)
9251 if (!type
|| !CLASS_TYPE_P (type
))
9254 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
9256 if (TREE_CODE (field
) != FIELD_DECL
)
9258 if (TREE_STATIC (field
))
9266 for (tree base_binfo
, binfo
= TYPE_BINFO (type
);
9267 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
9269 tree base
= TREE_TYPE (base_binfo
);
9272 if (tree field
= first_non_static_field (base
, pred
))
9279 struct NonPublicField
9281 bool operator() (const_tree t
)
9283 return DECL_P (t
) && (TREE_PRIVATE (t
) || TREE_PROTECTED (t
));
9287 /* Return the DECL of the first non-public subobject of class TYPE
9288 or null if none can be found. */
9291 first_non_public_field (tree type
)
9293 return first_non_static_field (type
, NonPublicField ());
9296 struct NonTrivialField
9298 bool operator() (const_tree t
)
9300 return !trivial_type_p (DECL_P (t
) ? TREE_TYPE (t
) : t
);
9304 /* Return the DECL of the first non-trivial subobject of class TYPE
9305 or null if none can be found. */
9308 first_non_trivial_field (tree type
)
9310 return first_non_static_field (type
, NonTrivialField ());
9313 } /* unnamed namespace */
9315 /* Return true if all copy and move assignment operator overloads for
9316 class TYPE are trivial and at least one of them is not deleted and,
9317 when ACCESS is set, accessible. Return false otherwise. Set
9318 HASASSIGN to true when the TYPE has a (not necessarily trivial)
9319 copy or move assignment. */
9322 has_trivial_copy_assign_p (tree type
, bool access
, bool *hasassign
)
9324 tree fns
= get_class_binding (type
, assign_op_identifier
);
9325 bool all_trivial
= true;
9327 /* Iterate over overloads of the assignment operator, checking
9328 accessible copy assignments for triviality. */
9330 for (ovl_iterator
oi (fns
); oi
; ++oi
)
9334 /* Skip operators that aren't copy assignments. */
9338 bool accessible
= (!access
|| !(TREE_PRIVATE (f
) || TREE_PROTECTED (f
))
9339 || accessible_p (TYPE_BINFO (type
), f
, true));
9341 /* Skip template assignment operators and deleted functions. */
9342 if (TREE_CODE (f
) != FUNCTION_DECL
|| DECL_DELETED_FN (f
))
9348 if (!accessible
|| !trivial_fn_p (f
))
9349 all_trivial
= false;
9351 /* Break early when both properties have been determined. */
9352 if (*hasassign
&& !all_trivial
)
9356 /* Return true if they're all trivial and one of the expressions
9357 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
9358 tree ref
= cp_build_reference_type (type
, false);
9360 && (is_trivially_xible (MODIFY_EXPR
, type
, type
)
9361 || is_trivially_xible (MODIFY_EXPR
, type
, ref
)));
9364 /* Return true if all copy and move ctor overloads for class TYPE are
9365 trivial and at least one of them is not deleted and, when ACCESS is
9366 set, accessible. Return false otherwise. Set each element of HASCTOR[]
9367 to true when the TYPE has a (not necessarily trivial) default and copy
9368 (or move) ctor, respectively. */
9371 has_trivial_copy_p (tree type
, bool access
, bool hasctor
[2])
9373 tree fns
= get_class_binding (type
, complete_ctor_identifier
);
9374 bool all_trivial
= true;
9376 for (ovl_iterator
oi (fns
); oi
; ++oi
)
9380 /* Skip template constructors. */
9381 if (TREE_CODE (f
) != FUNCTION_DECL
)
9384 bool cpy_or_move_ctor_p
= copy_fn_p (f
);
9386 /* Skip ctors other than default, copy, and move. */
9387 if (!cpy_or_move_ctor_p
&& !default_ctor_p (f
))
9390 if (DECL_DELETED_FN (f
))
9393 bool accessible
= (!access
|| !(TREE_PRIVATE (f
) || TREE_PROTECTED (f
))
9394 || accessible_p (TYPE_BINFO (type
), f
, true));
9397 hasctor
[cpy_or_move_ctor_p
] = true;
9399 if (cpy_or_move_ctor_p
&& (!accessible
|| !trivial_fn_p (f
)))
9400 all_trivial
= false;
9402 /* Break early when both properties have been determined. */
9403 if (hasctor
[0] && hasctor
[1] && !all_trivial
)
9410 /* Issue a warning on a call to the built-in function FNDECL if it is
9411 a raw memory write whose destination is not an object of (something
9412 like) trivial or standard layout type with a non-deleted assignment
9413 and copy ctor. Detects const correctness violations, corrupting
9414 references, virtual table pointers, and bypassing non-trivial
9418 maybe_warn_class_memaccess (location_t loc
, tree fndecl
,
9419 const vec
<tree
, va_gc
> *args
)
9421 /* Except for bcopy where it's second, the destination pointer is
9422 the first argument for all functions handled here. Compute
9423 the index of the destination and source arguments. */
9424 unsigned dstidx
= DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_BCOPY
;
9425 unsigned srcidx
= !dstidx
;
9427 tree dest
= (*args
)[dstidx
];
9428 if (!TREE_TYPE (dest
)
9429 || (TREE_CODE (TREE_TYPE (dest
)) != ARRAY_TYPE
9430 && !INDIRECT_TYPE_P (TREE_TYPE (dest
))))
9433 tree srctype
= NULL_TREE
;
9435 /* Determine the type of the pointed-to object and whether it's
9436 a complete class type. */
9437 tree desttype
= TREE_TYPE (TREE_TYPE (dest
));
9439 if (!desttype
|| !COMPLETE_TYPE_P (desttype
) || !CLASS_TYPE_P (desttype
))
9442 /* Check to see if the raw memory call is made by a non-static member
9443 function with THIS as the destination argument for the destination
9444 type. If so, and if the class has no non-trivial bases or members,
9445 be more permissive. */
9446 if (current_function_decl
9447 && DECL_NONSTATIC_MEMBER_FUNCTION_P (current_function_decl
)
9448 && is_this_parameter (tree_strip_nop_conversions (dest
)))
9450 tree ctx
= DECL_CONTEXT (current_function_decl
);
9451 bool special
= same_type_ignoring_top_level_qualifiers_p (ctx
, desttype
);
9452 tree binfo
= TYPE_BINFO (ctx
);
9455 && !BINFO_VTABLE (binfo
)
9456 && !first_non_trivial_field (desttype
))
9460 /* True if the class is trivial. */
9461 bool trivial
= trivial_type_p (desttype
);
9463 /* Set to true if DESTYPE has an accessible copy assignment. */
9464 bool hasassign
= false;
9465 /* True if all of the class' overloaded copy assignment operators
9466 are all trivial (and not deleted) and at least one of them is
9468 bool trivassign
= has_trivial_copy_assign_p (desttype
, true, &hasassign
);
9470 /* Set to true if DESTTYPE has an accessible default and copy ctor,
9472 bool hasctors
[2] = { false, false };
9474 /* True if all of the class' overloaded copy constructors are all
9475 trivial (and not deleted) and at least one of them is accessible. */
9476 bool trivcopy
= has_trivial_copy_p (desttype
, true, hasctors
);
9478 /* Set FLD to the first private/protected member of the class. */
9479 tree fld
= trivial
? first_non_public_field (desttype
) : NULL_TREE
;
9481 /* The warning format string. */
9482 const char *warnfmt
= NULL
;
9483 /* A suggested alternative to offer instead of the raw memory call.
9484 Empty string when none can be come up with. */
9485 const char *suggest
= "";
9486 bool warned
= false;
9488 switch (DECL_FUNCTION_CODE (fndecl
))
9490 case BUILT_IN_MEMSET
:
9491 if (!integer_zerop (maybe_constant_value ((*args
)[1])))
9493 /* Diagnose setting non-copy-assignable or non-trivial types,
9494 or types with a private member, to (potentially) non-zero
9495 bytes. Since the value of the bytes being written is unknown,
9496 suggest using assignment instead (if one exists). Also warn
9497 for writes into objects for which zero-initialization doesn't
9498 mean all bits clear (pointer-to-member data, where null is all
9499 bits set). Since the value being written is (most likely)
9500 non-zero, simply suggest assignment (but not copy assignment). */
9501 suggest
= "; use assignment instead";
9503 warnfmt
= G_("%qD writing to an object of type %#qT with "
9504 "no trivial copy-assignment");
9506 warnfmt
= G_("%qD writing to an object of non-trivial type %#qT%s");
9509 const char *access
= TREE_PRIVATE (fld
) ? "private" : "protected";
9510 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9511 "%qD writing to an object of type %#qT with "
9513 fndecl
, desttype
, access
, fld
);
9515 else if (!zero_init_p (desttype
))
9516 warnfmt
= G_("%qD writing to an object of type %#qT containing "
9517 "a pointer to data member%s");
9523 case BUILT_IN_BZERO
:
9524 /* Similarly to the above, diagnose clearing non-trivial or non-
9525 standard layout objects, or objects of types with no assignmenmt.
9526 Since the value being written is known to be zero, suggest either
9527 copy assignment, copy ctor, or default ctor as an alternative,
9528 depending on what's available. */
9530 if (hasassign
&& hasctors
[0])
9531 suggest
= G_("; use assignment or value-initialization instead");
9533 suggest
= G_("; use assignment instead");
9534 else if (hasctors
[0])
9535 suggest
= G_("; use value-initialization instead");
9538 warnfmt
= G_("%qD clearing an object of type %#qT with "
9539 "no trivial copy-assignment%s");
9541 warnfmt
= G_("%qD clearing an object of non-trivial type %#qT%s");
9542 else if (!zero_init_p (desttype
))
9543 warnfmt
= G_("%qD clearing an object of type %#qT containing "
9544 "a pointer-to-member%s");
9547 case BUILT_IN_BCOPY
:
9548 case BUILT_IN_MEMCPY
:
9549 case BUILT_IN_MEMMOVE
:
9550 case BUILT_IN_MEMPCPY
:
9551 /* Determine the type of the source object. */
9552 srctype
= TREE_TYPE ((*args
)[srcidx
]);
9553 if (!srctype
|| !INDIRECT_TYPE_P (srctype
))
9554 srctype
= void_type_node
;
9556 srctype
= TREE_TYPE (srctype
);
9558 /* Since it's impossible to determine wheter the byte copy is
9559 being used in place of assignment to an existing object or
9560 as a substitute for initialization, assume it's the former.
9561 Determine the best alternative to use instead depending on
9562 what's not deleted. */
9563 if (hasassign
&& hasctors
[1])
9564 suggest
= G_("; use copy-assignment or copy-initialization instead");
9566 suggest
= G_("; use copy-assignment instead");
9567 else if (hasctors
[1])
9568 suggest
= G_("; use copy-initialization instead");
9571 warnfmt
= G_("%qD writing to an object of type %#qT with no trivial "
9572 "copy-assignment%s");
9573 else if (!trivially_copyable_p (desttype
))
9574 warnfmt
= G_("%qD writing to an object of non-trivially copyable "
9577 warnfmt
= G_("%qD writing to an object with a deleted copy constructor");
9580 && !VOID_TYPE_P (srctype
)
9581 && !is_byte_access_type (srctype
)
9582 && !same_type_ignoring_top_level_qualifiers_p (desttype
,
9585 /* Warn when copying into a non-trivial object from an object
9586 of a different type other than void or char. */
9587 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9588 "%qD copying an object of non-trivial type "
9589 "%#qT from an array of %#qT",
9590 fndecl
, desttype
, srctype
);
9593 && !VOID_TYPE_P (srctype
)
9594 && !is_byte_access_type (srctype
)
9595 && !same_type_ignoring_top_level_qualifiers_p (desttype
,
9598 const char *access
= TREE_PRIVATE (fld
) ? "private" : "protected";
9599 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9600 "%qD copying an object of type %#qT with "
9601 "%qs member %qD from an array of %#qT; use "
9602 "assignment or copy-initialization instead",
9603 fndecl
, desttype
, access
, fld
, srctype
);
9605 else if (!trivial
&& vec_safe_length (args
) > 2)
9607 tree sz
= maybe_constant_value ((*args
)[2]);
9608 if (!tree_fits_uhwi_p (sz
))
9611 /* Finally, warn on partial copies. */
9612 unsigned HOST_WIDE_INT typesize
9613 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype
));
9614 if (unsigned HOST_WIDE_INT partial
= tree_to_uhwi (sz
) % typesize
)
9615 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9616 (typesize
- partial
> 1
9617 ? G_("%qD writing to an object of "
9618 "a non-trivial type %#qT leaves %wu "
9620 : G_("%qD writing to an object of "
9621 "a non-trivial type %#qT leaves %wu "
9623 fndecl
, desttype
, typesize
- partial
);
9627 case BUILT_IN_REALLOC
:
9629 if (!trivially_copyable_p (desttype
))
9630 warnfmt
= G_("%qD moving an object of non-trivially copyable type "
9631 "%#qT; use %<new%> and %<delete%> instead");
9633 warnfmt
= G_("%qD moving an object of type %#qT with deleted copy "
9634 "constructor; use %<new%> and %<delete%> instead");
9635 else if (!get_dtor (desttype
, tf_none
))
9636 warnfmt
= G_("%qD moving an object of type %#qT with deleted "
9640 tree sz
= maybe_constant_value ((*args
)[1]);
9641 if (TREE_CODE (sz
) == INTEGER_CST
9642 && tree_int_cst_lt (sz
, TYPE_SIZE_UNIT (desttype
)))
9643 /* Finally, warn on reallocation into insufficient space. */
9644 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9645 "%qD moving an object of non-trivial type "
9646 "%#qT and size %E into a region of size %E",
9647 fndecl
, desttype
, TYPE_SIZE_UNIT (desttype
),
9659 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9660 warnfmt
, fndecl
, desttype
, suggest
);
9662 warned
= warning_at (loc
, OPT_Wclass_memaccess
,
9663 warnfmt
, fndecl
, desttype
);
9667 inform (location_of (desttype
), "%#qT declared here", desttype
);
9670 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
9671 If FN is the result of resolving an overloaded target built-in,
9672 ORIG_FNDECL is the original function decl, otherwise it is null.
9673 This function performs no overload resolution, conversion, or other
9674 high-level operations. */
9677 build_cxx_call (tree fn
, int nargs
, tree
*argarray
,
9678 tsubst_flags_t complain
, tree orig_fndecl
)
9682 /* Remember roughly where this call is. */
9683 location_t loc
= cp_expr_loc_or_input_loc (fn
);
9684 fn
= build_call_a (fn
, nargs
, argarray
);
9685 SET_EXPR_LOCATION (fn
, loc
);
9687 fndecl
= get_callee_fndecl (fn
);
9689 orig_fndecl
= fndecl
;
9691 /* Check that arguments to builtin functions match the expectations. */
9693 && !processing_template_decl
9694 && fndecl_built_in_p (fndecl
))
9698 /* We need to take care that values to BUILT_IN_NORMAL
9700 for (i
= 0; i
< nargs
; i
++)
9701 argarray
[i
] = maybe_constant_value (argarray
[i
]);
9703 if (!check_builtin_function_arguments (EXPR_LOCATION (fn
), vNULL
, fndecl
,
9704 orig_fndecl
, nargs
, argarray
))
9705 return error_mark_node
;
9708 if (VOID_TYPE_P (TREE_TYPE (fn
)))
9711 /* 5.2.2/11: If a function call is a prvalue of object type: if the
9712 function call is either the operand of a decltype-specifier or the
9713 right operand of a comma operator that is the operand of a
9714 decltype-specifier, a temporary object is not introduced for the
9715 prvalue. The type of the prvalue may be incomplete. */
9716 if (!(complain
& tf_decltype
))
9718 fn
= require_complete_type_sfinae (fn
, complain
);
9719 if (fn
== error_mark_node
)
9720 return error_mark_node
;
9722 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn
)))
9724 fn
= build_cplus_new (TREE_TYPE (fn
), fn
, complain
);
9725 maybe_warn_parm_abi (TREE_TYPE (fn
), loc
);
9728 return convert_from_reference (fn
);
9731 /* Returns the value to use for the in-charge parameter when making a
9732 call to a function with the indicated NAME.
9734 FIXME:Can't we find a neater way to do this mapping? */
9737 in_charge_arg_for_name (tree name
)
9739 if (IDENTIFIER_CTOR_P (name
))
9741 if (name
== complete_ctor_identifier
)
9742 return integer_one_node
;
9743 gcc_checking_assert (name
== base_ctor_identifier
);
9747 if (name
== complete_dtor_identifier
)
9748 return integer_two_node
;
9749 else if (name
== deleting_dtor_identifier
)
9750 return integer_three_node
;
9751 gcc_checking_assert (name
== base_dtor_identifier
);
9754 return integer_zero_node
;
9757 /* We've built up a constructor call RET. Complain if it delegates to the
9758 constructor we're currently compiling. */
9761 check_self_delegation (tree ret
)
9763 if (TREE_CODE (ret
) == TARGET_EXPR
)
9764 ret
= TARGET_EXPR_INITIAL (ret
);
9765 tree fn
= cp_get_callee_fndecl_nofold (ret
);
9766 if (fn
&& DECL_ABSTRACT_ORIGIN (fn
) == current_function_decl
)
9767 error ("constructor delegates to itself");
9770 /* Build a call to a constructor, destructor, or an assignment
9771 operator for INSTANCE, an expression with class type. NAME
9772 indicates the special member function to call; *ARGS are the
9773 arguments. ARGS may be NULL. This may change ARGS. BINFO
9774 indicates the base of INSTANCE that is to be passed as the `this'
9775 parameter to the member function called.
9777 FLAGS are the LOOKUP_* flags to use when processing the call.
9779 If NAME indicates a complete object constructor, INSTANCE may be
9780 NULL_TREE. In this case, the caller will call build_cplus_new to
9781 store the newly constructed object into a VAR_DECL. */
9784 build_special_member_call (tree instance
, tree name
, vec
<tree
, va_gc
> **args
,
9785 tree binfo
, int flags
, tsubst_flags_t complain
)
9788 /* The type of the subobject to be constructed or destroyed. */
9790 vec
<tree
, va_gc
> *allocated
= NULL
;
9793 gcc_assert (IDENTIFIER_CDTOR_P (name
) || name
== assign_op_identifier
);
9795 if (error_operand_p (instance
))
9796 return error_mark_node
;
9798 if (IDENTIFIER_DTOR_P (name
))
9800 gcc_assert (args
== NULL
|| vec_safe_is_empty (*args
));
9801 if (!type_build_dtor_call (TREE_TYPE (instance
)))
9802 /* Shortcut to avoid lazy destructor declaration. */
9803 return build_trivial_dtor_call (instance
);
9808 /* Resolve the name. */
9809 if (!complete_type_or_maybe_complain (binfo
, NULL_TREE
, complain
))
9810 return error_mark_node
;
9812 binfo
= TYPE_BINFO (binfo
);
9815 gcc_assert (binfo
!= NULL_TREE
);
9817 class_type
= BINFO_TYPE (binfo
);
9819 /* Handle the special case where INSTANCE is NULL_TREE. */
9820 if (name
== complete_ctor_identifier
&& !instance
)
9821 instance
= build_dummy_object (class_type
);
9824 /* Convert to the base class, if necessary. */
9825 if (!same_type_ignoring_top_level_qualifiers_p
9826 (TREE_TYPE (instance
), BINFO_TYPE (binfo
)))
9828 if (IDENTIFIER_CDTOR_P (name
))
9829 /* For constructors and destructors, either the base is
9830 non-virtual, or it is virtual but we are doing the
9831 conversion from a constructor or destructor for the
9832 complete object. In either case, we can convert
9834 instance
= convert_to_base_statically (instance
, binfo
);
9837 /* However, for assignment operators, we must convert
9838 dynamically if the base is virtual. */
9839 gcc_checking_assert (name
== assign_op_identifier
);
9840 instance
= build_base_path (PLUS_EXPR
, instance
,
9841 binfo
, /*nonnull=*/1, complain
);
9846 gcc_assert (instance
!= NULL_TREE
);
9848 /* In C++17, "If the initializer expression is a prvalue and the
9849 cv-unqualified version of the source type is the same class as the class
9850 of the destination, the initializer expression is used to initialize the
9851 destination object." Handle that here to avoid doing overload
9853 if (cxx_dialect
>= cxx17
9854 && args
&& vec_safe_length (*args
) == 1
9855 && !unsafe_return_slot_p (instance
))
9857 tree arg
= (**args
)[0];
9859 if (BRACE_ENCLOSED_INITIALIZER_P (arg
)
9860 && !TYPE_HAS_LIST_CTOR (class_type
)
9861 && CONSTRUCTOR_NELTS (arg
) == 1)
9862 arg
= CONSTRUCTOR_ELT (arg
, 0)->value
;
9864 if ((TREE_CODE (arg
) == TARGET_EXPR
9865 || TREE_CODE (arg
) == CONSTRUCTOR
)
9866 && (same_type_ignoring_top_level_qualifiers_p
9867 (class_type
, TREE_TYPE (arg
))))
9869 if (is_dummy_object (instance
))
9871 else if (TREE_CODE (arg
) == TARGET_EXPR
)
9872 TARGET_EXPR_DIRECT_INIT_P (arg
) = true;
9874 if ((complain
& tf_error
)
9875 && (flags
& LOOKUP_DELEGATING_CONS
))
9876 check_self_delegation (arg
);
9877 /* Avoid change of behavior on Wunused-var-2.C. */
9878 instance
= mark_lvalue_use (instance
);
9879 return build2 (INIT_EXPR
, class_type
, instance
, arg
);
9883 fns
= lookup_fnfields (binfo
, name
, 1, complain
);
9885 /* When making a call to a constructor or destructor for a subobject
9886 that uses virtual base classes, pass down a pointer to a VTT for
9888 if ((name
== base_ctor_identifier
9889 || name
== base_dtor_identifier
)
9890 && CLASSTYPE_VBASECLASSES (class_type
))
9895 /* If the current function is a complete object constructor
9896 or destructor, then we fetch the VTT directly.
9897 Otherwise, we look it up using the VTT we were given. */
9898 vtt
= DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type
));
9899 vtt
= decay_conversion (vtt
, complain
);
9900 if (vtt
== error_mark_node
)
9901 return error_mark_node
;
9902 vtt
= build_if_in_charge (vtt
, current_vtt_parm
);
9903 if (BINFO_SUBVTT_INDEX (binfo
))
9904 sub_vtt
= fold_build_pointer_plus (vtt
, BINFO_SUBVTT_INDEX (binfo
));
9910 allocated
= make_tree_vector ();
9914 vec_safe_insert (*args
, 0, sub_vtt
);
9917 ret
= build_new_method_call (instance
, fns
, args
,
9918 TYPE_BINFO (BINFO_TYPE (binfo
)),
9922 if (allocated
!= NULL
)
9923 release_tree_vector (allocated
);
9925 if ((complain
& tf_error
)
9926 && (flags
& LOOKUP_DELEGATING_CONS
)
9927 && name
== complete_ctor_identifier
)
9928 check_self_delegation (ret
);
9933 /* Return the NAME, as a C string. The NAME indicates a function that
9934 is a member of TYPE. *FREE_P is set to true if the caller must
9935 free the memory returned.
9937 Rather than go through all of this, we should simply set the names
9938 of constructors and destructors appropriately, and dispense with
9939 ctor_identifier, dtor_identifier, etc. */
9942 name_as_c_string (tree name
, tree type
, bool *free_p
)
9944 const char *pretty_name
;
9946 /* Assume that we will not allocate memory. */
9948 /* Constructors and destructors are special. */
9949 if (IDENTIFIER_CDTOR_P (name
))
9952 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type
)));
9953 /* For a destructor, add the '~'. */
9954 if (IDENTIFIER_DTOR_P (name
))
9956 pretty_name
= concat ("~", pretty_name
, NULL
);
9957 /* Remember that we need to free the memory allocated. */
9961 else if (IDENTIFIER_CONV_OP_P (name
))
9963 pretty_name
= concat ("operator ",
9964 type_as_string_translate (TREE_TYPE (name
),
9965 TFF_PLAIN_IDENTIFIER
),
9967 /* Remember that we need to free the memory allocated. */
9971 pretty_name
= identifier_to_locale (IDENTIFIER_POINTER (name
));
9973 return CONST_CAST (char *, pretty_name
);
9976 /* If CANDIDATES contains exactly one candidate, return it, otherwise
9979 static z_candidate
*
9980 single_z_candidate (z_candidate
*candidates
)
9982 if (candidates
== NULL
)
9985 if (candidates
->next
)
9991 /* If CANDIDATE is invalid due to a bad argument type, return the
9992 pertinent conversion_info.
9994 Otherwise, return NULL. */
9996 static const conversion_info
*
9997 maybe_get_bad_conversion_for_unmatched_call (const z_candidate
*candidate
)
9999 /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
10000 rejection_reason
*r
= candidate
->reason
;
10010 case rr_arg_conversion
:
10011 return &r
->u
.conversion
;
10013 case rr_bad_arg_conversion
:
10014 return &r
->u
.bad_conversion
;
10018 /* Issue an error and note complaining about a bad argument type at a
10019 callsite with a single candidate FNDECL.
10021 ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
10022 case input_location is used).
10023 FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
10024 the formal parameter. */
10027 complain_about_bad_argument (location_t arg_loc
,
10028 tree from_type
, tree to_type
,
10029 tree fndecl
, int parmnum
)
10031 auto_diagnostic_group d
;
10032 range_label_for_type_mismatch
rhs_label (from_type
, to_type
);
10033 range_label
*label
= &rhs_label
;
10034 if (arg_loc
== UNKNOWN_LOCATION
)
10036 arg_loc
= input_location
;
10039 gcc_rich_location
richloc (arg_loc
, label
);
10040 error_at (&richloc
,
10041 "cannot convert %qH to %qI",
10042 from_type
, to_type
);
10043 maybe_inform_about_fndecl_for_bogus_argument_init (fndecl
,
10047 /* Subroutine of build_new_method_call_1, for where there are no viable
10048 candidates for the call. */
10051 complain_about_no_candidates_for_method_call (tree instance
,
10052 z_candidate
*candidates
,
10053 tree explicit_targs
,
10055 tree optype
, tree name
,
10056 bool skip_first_for_error
,
10057 vec
<tree
, va_gc
> *user_args
)
10059 auto_diagnostic_group d
;
10060 if (!COMPLETE_OR_OPEN_TYPE_P (basetype
))
10061 cxx_incomplete_type_error (instance
, basetype
);
10063 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
10064 basetype
, optype
, build_tree_list_vec (user_args
),
10065 TREE_TYPE (instance
));
10068 /* Special-case for when there's a single candidate that's failing
10069 due to a bad argument type. */
10070 if (z_candidate
*candidate
= single_z_candidate (candidates
))
10071 if (const conversion_info
*conv
10072 = maybe_get_bad_conversion_for_unmatched_call (candidate
))
10074 tree from_type
= conv
->from
;
10075 if (!TYPE_P (conv
->from
))
10076 from_type
= lvalue_type (conv
->from
);
10077 complain_about_bad_argument (conv
->loc
,
10078 from_type
, conv
->to_type
,
10079 candidate
->fn
, conv
->n_arg
);
10083 tree arglist
= build_tree_list_vec (user_args
);
10084 tree errname
= name
;
10085 bool twiddle
= false;
10086 if (IDENTIFIER_CDTOR_P (errname
))
10088 twiddle
= IDENTIFIER_DTOR_P (errname
);
10089 errname
= constructor_name (basetype
);
10091 if (explicit_targs
)
10092 errname
= lookup_template_function (errname
, explicit_targs
);
10093 if (skip_first_for_error
)
10094 arglist
= TREE_CHAIN (arglist
);
10095 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
10096 basetype
, &"~"[!twiddle
], errname
, arglist
,
10097 TREE_TYPE (instance
));
10099 print_z_candidates (location_of (name
), candidates
);
10102 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
10103 be set, upon return, to the function called. ARGS may be NULL.
10104 This may change ARGS. */
10107 build_new_method_call_1 (tree instance
, tree fns
, vec
<tree
, va_gc
> **args
,
10108 tree conversion_path
, int flags
,
10109 tree
*fn_p
, tsubst_flags_t complain
)
10111 struct z_candidate
*candidates
= 0, *cand
;
10112 tree explicit_targs
= NULL_TREE
;
10113 tree basetype
= NULL_TREE
;
10116 tree first_mem_arg
= NULL_TREE
;
10118 bool skip_first_for_error
;
10119 vec
<tree
, va_gc
> *user_args
;
10122 int template_only
= 0;
10124 tree orig_instance
;
10126 vec
<tree
, va_gc
> *orig_args
= NULL
;
10129 gcc_assert (instance
!= NULL_TREE
);
10131 /* We don't know what function we're going to call, yet. */
10135 if (error_operand_p (instance
)
10136 || !fns
|| error_operand_p (fns
))
10137 return error_mark_node
;
10139 if (!BASELINK_P (fns
))
10141 if (complain
& tf_error
)
10142 error ("call to non-function %qD", fns
);
10143 return error_mark_node
;
10146 orig_instance
= instance
;
10149 /* Dismantle the baselink to collect all the information we need. */
10150 if (!conversion_path
)
10151 conversion_path
= BASELINK_BINFO (fns
);
10152 access_binfo
= BASELINK_ACCESS_BINFO (fns
);
10153 optype
= BASELINK_OPTYPE (fns
);
10154 fns
= BASELINK_FUNCTIONS (fns
);
10155 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
10157 explicit_targs
= TREE_OPERAND (fns
, 1);
10158 fns
= TREE_OPERAND (fns
, 0);
10161 gcc_assert (OVL_P (fns
));
10162 fn
= OVL_FIRST (fns
);
10163 name
= DECL_NAME (fn
);
10165 basetype
= TYPE_MAIN_VARIANT (TREE_TYPE (instance
));
10166 gcc_assert (CLASS_TYPE_P (basetype
));
10168 user_args
= args
== NULL
? NULL
: *args
;
10169 /* Under DR 147 A::A() is an invalid constructor call,
10170 not a functional cast. */
10171 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
10173 if (! (complain
& tf_error
))
10174 return error_mark_node
;
10176 basetype
= DECL_CONTEXT (fn
);
10177 name
= constructor_name (basetype
);
10178 auto_diagnostic_group d
;
10179 if (permerror (input_location
,
10180 "cannot call constructor %<%T::%D%> directly",
10182 inform (input_location
, "for a function-style cast, remove the "
10183 "redundant %<::%D%>", name
);
10184 call
= build_functional_cast (input_location
, basetype
,
10185 build_tree_list_vec (user_args
),
10190 if (processing_template_decl
)
10192 orig_args
= args
== NULL
? NULL
: make_tree_vector_copy (*args
);
10193 instance
= build_non_dependent_expr (instance
);
10195 make_args_non_dependent (*args
);
10198 /* Process the argument list. */
10199 if (args
!= NULL
&& *args
!= NULL
)
10201 *args
= resolve_args (*args
, complain
);
10203 return error_mark_node
;
10207 /* Consider the object argument to be used even if we end up selecting a
10208 static member function. */
10209 instance
= mark_type_use (instance
);
10211 /* Figure out whether to skip the first argument for the error
10212 message we will display to users if an error occurs. We don't
10213 want to display any compiler-generated arguments. The "this"
10214 pointer hasn't been added yet. However, we must remove the VTT
10215 pointer if this is a call to a base-class constructor or
10217 skip_first_for_error
= false;
10218 if (IDENTIFIER_CDTOR_P (name
))
10220 /* Callers should explicitly indicate whether they want to ctor
10221 the complete object or just the part without virtual bases. */
10222 gcc_assert (name
!= ctor_identifier
);
10224 /* Remove the VTT pointer, if present. */
10225 if ((name
== base_ctor_identifier
|| name
== base_dtor_identifier
)
10226 && CLASSTYPE_VBASECLASSES (basetype
))
10227 skip_first_for_error
= true;
10229 /* It's OK to call destructors and constructors on cv-qualified
10230 objects. Therefore, convert the INSTANCE to the unqualified
10231 type, if necessary. */
10232 if (!same_type_p (basetype
, TREE_TYPE (instance
)))
10234 instance
= build_this (instance
);
10235 instance
= build_nop (build_pointer_type (basetype
), instance
);
10236 instance
= build_fold_indirect_ref (instance
);
10240 gcc_assert (!DECL_DESTRUCTOR_P (fn
) && !DECL_CONSTRUCTOR_P (fn
));
10242 /* For the overload resolution we need to find the actual `this`
10243 that would be captured if the call turns out to be to a
10244 non-static member function. Do not actually capture it at this
10246 if (DECL_CONSTRUCTOR_P (fn
))
10247 /* Constructors don't use the enclosing 'this'. */
10248 first_mem_arg
= instance
;
10250 first_mem_arg
= maybe_resolve_dummy (instance
, false);
10252 /* Get the high-water mark for the CONVERSION_OBSTACK. */
10253 p
= conversion_obstack_alloc (0);
10255 /* The number of arguments artificial parms in ARGS; we subtract one because
10256 there's no 'this' in ARGS. */
10257 unsigned skip
= num_artificial_parms_for (fn
) - 1;
10259 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
10260 initializer, not T({ }). */
10261 if (DECL_CONSTRUCTOR_P (fn
)
10262 && vec_safe_length (user_args
) > skip
10263 && DIRECT_LIST_INIT_P ((*user_args
)[skip
]))
10265 tree init_list
= (*user_args
)[skip
];
10266 tree init
= NULL_TREE
;
10268 gcc_assert (user_args
->length () == skip
+ 1
10269 && !(flags
& LOOKUP_ONLYCONVERTING
));
10271 /* If the initializer list has no elements and T is a class type with
10272 a default constructor, the object is value-initialized. Handle
10273 this here so we don't need to handle it wherever we use
10274 build_special_member_call. */
10275 if (CONSTRUCTOR_NELTS (init_list
) == 0
10276 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
)
10277 /* For a user-provided default constructor, use the normal
10278 mechanisms so that protected access works. */
10279 && type_has_non_user_provided_default_constructor (basetype
)
10280 && !processing_template_decl
)
10281 init
= build_value_init (basetype
, complain
);
10283 /* If BASETYPE is an aggregate, we need to do aggregate
10285 else if (CP_AGGREGATE_TYPE_P (basetype
))
10287 init
= reshape_init (basetype
, init_list
, complain
);
10288 init
= digest_init (basetype
, init
, complain
);
10293 if (is_dummy_object (instance
))
10294 return get_target_expr_sfinae (init
, complain
);
10295 init
= build2 (INIT_EXPR
, TREE_TYPE (instance
), instance
, init
);
10296 TREE_SIDE_EFFECTS (init
) = true;
10300 /* Otherwise go ahead with overload resolution. */
10301 add_list_candidates (fns
, first_mem_arg
, user_args
,
10302 basetype
, explicit_targs
, template_only
,
10303 conversion_path
, access_binfo
, flags
,
10304 &candidates
, complain
);
10307 add_candidates (fns
, first_mem_arg
, user_args
, optype
,
10308 explicit_targs
, template_only
, conversion_path
,
10309 access_binfo
, flags
, &candidates
, complain
);
10311 any_viable_p
= false;
10312 candidates
= splice_viable (candidates
, false, &any_viable_p
);
10316 /* [dcl.init], 17.6.2.2:
10318 Otherwise, if no constructor is viable, the destination type is
10319 a (possibly cv-qualified) aggregate class A, and the initializer
10320 is a parenthesized expression-list, the object is initialized as
10323 We achieve this by building up a CONSTRUCTOR, as for list-init,
10324 and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
10326 if (DECL_CONSTRUCTOR_P (fn
)
10327 && !(flags
& LOOKUP_ONLYCONVERTING
)
10328 && cxx_dialect
>= cxx20
10329 && CP_AGGREGATE_TYPE_P (basetype
)
10330 && !vec_safe_is_empty (user_args
))
10332 /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
10333 tree ctor
= build_constructor_from_vec (init_list_type_node
,
10335 CONSTRUCTOR_IS_DIRECT_INIT (ctor
) = true;
10336 CONSTRUCTOR_IS_PAREN_INIT (ctor
) = true;
10337 if (is_dummy_object (instance
))
10341 ctor
= digest_init (basetype
, ctor
, complain
);
10342 if (ctor
== error_mark_node
)
10343 return error_mark_node
;
10344 ctor
= build2 (INIT_EXPR
, TREE_TYPE (instance
), instance
, ctor
);
10345 TREE_SIDE_EFFECTS (ctor
) = true;
10349 if (complain
& tf_error
)
10350 complain_about_no_candidates_for_method_call (instance
, candidates
,
10351 explicit_targs
, basetype
,
10353 skip_first_for_error
,
10355 call
= error_mark_node
;
10359 cand
= tourney (candidates
, complain
);
10366 if (complain
& tf_error
)
10368 pretty_name
= name_as_c_string (name
, basetype
, &free_p
);
10369 arglist
= build_tree_list_vec (user_args
);
10370 if (skip_first_for_error
)
10371 arglist
= TREE_CHAIN (arglist
);
10372 auto_diagnostic_group d
;
10373 if (!any_strictly_viable (candidates
))
10374 error ("no matching function for call to %<%s(%A)%>",
10375 pretty_name
, arglist
);
10377 error ("call of overloaded %<%s(%A)%> is ambiguous",
10378 pretty_name
, arglist
);
10379 print_z_candidates (location_of (name
), candidates
);
10381 free (pretty_name
);
10383 call
= error_mark_node
;
10390 if (!(flags
& LOOKUP_NONVIRTUAL
)
10391 && DECL_PURE_VIRTUAL_P (fn
)
10392 && instance
== current_class_ref
10393 && (complain
& tf_warning
))
10395 /* This is not an error, it is runtime undefined
10397 if (!current_function_decl
)
10398 warning (0, "pure virtual %q#D called from "
10399 "non-static data member initializer", fn
);
10400 else if (DECL_CONSTRUCTOR_P (current_function_decl
)
10401 || DECL_DESTRUCTOR_P (current_function_decl
))
10402 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl
)
10403 ? G_("pure virtual %q#D called from constructor")
10404 : G_("pure virtual %q#D called from destructor")),
10408 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
10409 && !DECL_CONSTRUCTOR_P (fn
)
10410 && is_dummy_object (instance
))
10412 instance
= maybe_resolve_dummy (instance
, true);
10413 if (instance
== error_mark_node
)
10414 call
= error_mark_node
;
10415 else if (!is_dummy_object (instance
))
10417 /* We captured 'this' in the current lambda now that
10418 we know we really need it. */
10419 cand
->first_arg
= instance
;
10421 else if (any_dependent_bases_p ())
10422 /* We can't tell until instantiation time whether we can use
10423 *this as the implicit object argument. */;
10426 if (complain
& tf_error
)
10427 error ("cannot call member function %qD without object",
10429 call
= error_mark_node
;
10433 if (call
!= error_mark_node
)
10435 /* Now we know what function is being called. */
10438 /* Build the actual CALL_EXPR. */
10439 call
= build_over_call (cand
, flags
, complain
);
10440 /* In an expression of the form `a->f()' where `f' turns
10441 out to be a static member function, `a' is
10442 none-the-less evaluated. */
10443 if (TREE_CODE (TREE_TYPE (fn
)) != METHOD_TYPE
10444 && !is_dummy_object (instance
)
10445 && TREE_SIDE_EFFECTS (instance
))
10447 /* But avoid the implicit lvalue-rvalue conversion when 'a'
10450 if (TREE_THIS_VOLATILE (a
))
10451 a
= build_this (a
);
10452 call
= build2 (COMPOUND_EXPR
, TREE_TYPE (call
), a
, call
);
10454 else if (call
!= error_mark_node
10455 && DECL_DESTRUCTOR_P (cand
->fn
)
10456 && !VOID_TYPE_P (TREE_TYPE (call
)))
10457 /* An explicit call of the form "x->~X()" has type
10458 "void". However, on platforms where destructors
10459 return "this" (i.e., those where
10460 targetm.cxx.cdtor_returns_this is true), such calls
10461 will appear to have a return value of pointer type
10462 to the low-level call machinery. We do not want to
10463 change the low-level machinery, since we want to be
10464 able to optimize "delete f()" on such platforms as
10465 "operator delete(~X(f()))" (rather than generating
10466 "t = f(), ~X(t), operator delete (t)"). */
10467 call
= build_nop (void_type_node
, call
);
10472 if (processing_template_decl
&& call
!= error_mark_node
)
10474 bool cast_to_void
= false;
10476 if (TREE_CODE (call
) == COMPOUND_EXPR
)
10477 call
= TREE_OPERAND (call
, 1);
10478 else if (TREE_CODE (call
) == NOP_EXPR
)
10480 cast_to_void
= true;
10481 call
= TREE_OPERAND (call
, 0);
10483 if (INDIRECT_REF_P (call
))
10484 call
= TREE_OPERAND (call
, 0);
10485 call
= (build_min_non_dep_call_vec
10487 build_min (COMPONENT_REF
, TREE_TYPE (CALL_EXPR_FN (call
)),
10488 orig_instance
, orig_fns
, NULL_TREE
),
10490 SET_EXPR_LOCATION (call
, input_location
);
10491 call
= convert_from_reference (call
);
10493 call
= build_nop (void_type_node
, call
);
10496 /* Free all the conversions we allocated. */
10497 obstack_free (&conversion_obstack
, p
);
10499 if (orig_args
!= NULL
)
10500 release_tree_vector (orig_args
);
10505 /* Wrapper for above. */
10508 build_new_method_call (tree instance
, tree fns
, vec
<tree
, va_gc
> **args
,
10509 tree conversion_path
, int flags
,
10510 tree
*fn_p
, tsubst_flags_t complain
)
10513 bool subtime
= timevar_cond_start (TV_OVERLOAD
);
10514 ret
= build_new_method_call_1 (instance
, fns
, args
, conversion_path
, flags
,
10516 timevar_cond_stop (TV_OVERLOAD
, subtime
);
10520 /* Returns true iff standard conversion sequence ICS1 is a proper
10521 subsequence of ICS2. */
10524 is_subseq (conversion
*ics1
, conversion
*ics2
)
10526 /* We can assume that a conversion of the same code
10527 between the same types indicates a subsequence since we only get
10528 here if the types we are converting from are the same. */
10530 while (ics1
->kind
== ck_rvalue
10531 || ics1
->kind
== ck_lvalue
)
10532 ics1
= next_conversion (ics1
);
10536 while (ics2
->kind
== ck_rvalue
10537 || ics2
->kind
== ck_lvalue
)
10538 ics2
= next_conversion (ics2
);
10540 if (ics2
->kind
== ck_user
10541 || ics2
->kind
== ck_ambig
10542 || ics2
->kind
== ck_aggr
10543 || ics2
->kind
== ck_list
10544 || ics2
->kind
== ck_identity
)
10545 /* At this point, ICS1 cannot be a proper subsequence of
10546 ICS2. We can get a USER_CONV when we are comparing the
10547 second standard conversion sequence of two user conversion
10551 ics2
= next_conversion (ics2
);
10553 while (ics2
->kind
== ck_rvalue
10554 || ics2
->kind
== ck_lvalue
)
10555 ics2
= next_conversion (ics2
);
10557 if (ics2
->kind
== ics1
->kind
10558 && same_type_p (ics2
->type
, ics1
->type
)
10559 && (ics1
->kind
== ck_identity
10560 || same_type_p (next_conversion (ics2
)->type
,
10561 next_conversion (ics1
)->type
)))
10566 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
10567 be any _TYPE nodes. */
10570 is_properly_derived_from (tree derived
, tree base
)
10572 if (!CLASS_TYPE_P (derived
) || !CLASS_TYPE_P (base
))
10575 /* We only allow proper derivation here. The DERIVED_FROM_P macro
10576 considers every class derived from itself. */
10577 return (!same_type_ignoring_top_level_qualifiers_p (derived
, base
)
10578 && DERIVED_FROM_P (base
, derived
));
10581 /* We build the ICS for an implicit object parameter as a pointer
10582 conversion sequence. However, such a sequence should be compared
10583 as if it were a reference conversion sequence. If ICS is the
10584 implicit conversion sequence for an implicit object parameter,
10585 modify it accordingly. */
10588 maybe_handle_implicit_object (conversion
**ics
)
10590 if ((*ics
)->this_p
)
10592 /* [over.match.funcs]
10594 For non-static member functions, the type of the
10595 implicit object parameter is "reference to cv X"
10596 where X is the class of which the function is a
10597 member and cv is the cv-qualification on the member
10598 function declaration. */
10599 conversion
*t
= *ics
;
10600 tree reference_type
;
10602 /* The `this' parameter is a pointer to a class type. Make the
10603 implicit conversion talk about a reference to that same class
10605 reference_type
= TREE_TYPE (t
->type
);
10606 reference_type
= build_reference_type (reference_type
);
10608 if (t
->kind
== ck_qual
)
10609 t
= next_conversion (t
);
10610 if (t
->kind
== ck_ptr
)
10611 t
= next_conversion (t
);
10612 t
= build_identity_conv (TREE_TYPE (t
->type
), NULL_TREE
);
10613 t
= direct_reference_binding (reference_type
, t
);
10615 t
->rvaluedness_matches_p
= 0;
10620 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
10621 and return the initial reference binding conversion. Otherwise,
10622 leave *ICS unchanged and return NULL. */
10624 static conversion
*
10625 maybe_handle_ref_bind (conversion
**ics
)
10627 if ((*ics
)->kind
== ck_ref_bind
)
10629 conversion
*old_ics
= *ics
;
10630 *ics
= next_conversion (old_ics
);
10631 (*ics
)->user_conv_p
= old_ics
->user_conv_p
;
10638 /* Get the expression at the beginning of the conversion chain C. */
10641 conv_get_original_expr (conversion
*c
)
10643 for (; c
; c
= next_conversion (c
))
10644 if (c
->kind
== ck_identity
|| c
->kind
== ck_ambig
|| c
->kind
== ck_aggr
)
10649 /* Return a tree representing the number of elements initialized by the
10650 list-initialization C. The caller must check that C converts to an
10654 nelts_initialized_by_list_init (conversion
*c
)
10656 /* If the array we're converting to has a dimension, we'll use that. */
10657 if (TYPE_DOMAIN (c
->type
))
10658 return array_type_nelts_top (c
->type
);
10661 /* Otherwise, we look at how many elements the constructor we're
10662 initializing from has. */
10663 tree ctor
= conv_get_original_expr (c
);
10664 return size_int (CONSTRUCTOR_NELTS (ctor
));
10668 /* True iff C is a conversion that binds a reference or a pointer to
10669 an array of unknown bound. */
10672 conv_binds_to_array_of_unknown_bound (conversion
*c
)
10674 /* ck_ref_bind won't have the reference stripped. */
10675 tree type
= non_reference (c
->type
);
10676 /* ck_qual won't have the pointer stripped. */
10677 type
= strip_pointer_operator (type
);
10678 return (TREE_CODE (type
) == ARRAY_TYPE
10679 && TYPE_DOMAIN (type
) == NULL_TREE
);
10682 /* Compare two implicit conversion sequences according to the rules set out in
10683 [over.ics.rank]. Return values:
10685 1: ics1 is better than ics2
10686 -1: ics2 is better than ics1
10687 0: ics1 and ics2 are indistinguishable */
10690 compare_ics (conversion
*ics1
, conversion
*ics2
)
10696 tree deref_from_type1
= NULL_TREE
;
10697 tree deref_from_type2
= NULL_TREE
;
10698 tree deref_to_type1
= NULL_TREE
;
10699 tree deref_to_type2
= NULL_TREE
;
10700 conversion_rank rank1
, rank2
;
10702 /* REF_BINDING is nonzero if the result of the conversion sequence
10703 is a reference type. In that case REF_CONV is the reference
10704 binding conversion. */
10705 conversion
*ref_conv1
;
10706 conversion
*ref_conv2
;
10708 /* Compare badness before stripping the reference conversion. */
10709 if (ics1
->bad_p
> ics2
->bad_p
)
10711 else if (ics1
->bad_p
< ics2
->bad_p
)
10714 /* Handle implicit object parameters. */
10715 maybe_handle_implicit_object (&ics1
);
10716 maybe_handle_implicit_object (&ics2
);
10718 /* Handle reference parameters. */
10719 ref_conv1
= maybe_handle_ref_bind (&ics1
);
10720 ref_conv2
= maybe_handle_ref_bind (&ics2
);
10722 /* List-initialization sequence L1 is a better conversion sequence than
10723 list-initialization sequence L2 if L1 converts to
10724 std::initializer_list<X> for some X and L2 does not. */
10725 if (ics1
->kind
== ck_list
&& ics2
->kind
!= ck_list
)
10727 if (ics2
->kind
== ck_list
&& ics1
->kind
!= ck_list
)
10732 When comparing the basic forms of implicit conversion sequences (as
10733 defined in _over.best.ics_)
10735 --a standard conversion sequence (_over.ics.scs_) is a better
10736 conversion sequence than a user-defined conversion sequence
10737 or an ellipsis conversion sequence, and
10739 --a user-defined conversion sequence (_over.ics.user_) is a
10740 better conversion sequence than an ellipsis conversion sequence
10741 (_over.ics.ellipsis_). */
10742 /* Use BAD_CONVERSION_RANK because we already checked for a badness
10743 mismatch. If both ICS are bad, we try to make a decision based on
10744 what would have happened if they'd been good. This is not an
10745 extension, we'll still give an error when we build up the call; this
10746 just helps us give a more helpful error message. */
10747 rank1
= BAD_CONVERSION_RANK (ics1
);
10748 rank2
= BAD_CONVERSION_RANK (ics2
);
10752 else if (rank1
< rank2
)
10755 if (ics1
->ellipsis_p
)
10756 /* Both conversions are ellipsis conversions. */
10759 /* User-defined conversion sequence U1 is a better conversion sequence
10760 than another user-defined conversion sequence U2 if they contain the
10761 same user-defined conversion operator or constructor and if the sec-
10762 ond standard conversion sequence of U1 is better than the second
10763 standard conversion sequence of U2. */
10765 /* Handle list-conversion with the same code even though it isn't always
10766 ranked as a user-defined conversion and it doesn't have a second
10767 standard conversion sequence; it will still have the desired effect.
10768 Specifically, we need to do the reference binding comparison at the
10769 end of this function. */
10771 if (ics1
->user_conv_p
|| ics1
->kind
== ck_list
10772 || ics1
->kind
== ck_aggr
|| ics2
->kind
== ck_aggr
)
10774 conversion
*t1
= strip_standard_conversion (ics1
);
10775 conversion
*t2
= strip_standard_conversion (ics2
);
10777 if (!t1
|| !t2
|| t1
->kind
!= t2
->kind
)
10779 else if (t1
->kind
== ck_user
)
10781 tree f1
= t1
->cand
? t1
->cand
->fn
: t1
->type
;
10782 tree f2
= t2
->cand
? t2
->cand
->fn
: t2
->type
;
10786 /* List-initialization sequence L1 is a better conversion sequence than
10787 list-initialization sequence L2 if
10789 -- L1 and L2 convert to arrays of the same element type, and either
10790 the number of elements n1 initialized by L1 is less than the number
10791 of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
10792 of unknown bound and L1 does not. (Added in CWG 1307 and extended by
10794 else if (t1
->kind
== ck_aggr
10795 && TREE_CODE (t1
->type
) == ARRAY_TYPE
10796 && TREE_CODE (t2
->type
) == ARRAY_TYPE
)
10798 /* The type of the array elements must be the same. */
10799 if (!same_type_p (TREE_TYPE (t1
->type
), TREE_TYPE (t2
->type
)))
10802 tree n1
= nelts_initialized_by_list_init (t1
);
10803 tree n2
= nelts_initialized_by_list_init (t2
);
10804 if (tree_int_cst_lt (n1
, n2
))
10806 else if (tree_int_cst_lt (n2
, n1
))
10808 /* The n1 == n2 case. */
10809 bool c1
= conv_binds_to_array_of_unknown_bound (t1
);
10810 bool c2
= conv_binds_to_array_of_unknown_bound (t2
);
10813 else if (!c1
&& c2
)
10820 /* For ambiguous or aggregate conversions, use the target type as
10821 a proxy for the conversion function. */
10822 if (!same_type_ignoring_top_level_qualifiers_p (t1
->type
, t2
->type
))
10826 /* We can just fall through here, after setting up
10827 FROM_TYPE1 and FROM_TYPE2. */
10828 from_type1
= t1
->type
;
10829 from_type2
= t2
->type
;
10836 /* We're dealing with two standard conversion sequences.
10840 Standard conversion sequence S1 is a better conversion
10841 sequence than standard conversion sequence S2 if
10843 --S1 is a proper subsequence of S2 (comparing the conversion
10844 sequences in the canonical form defined by _over.ics.scs_,
10845 excluding any Lvalue Transformation; the identity
10846 conversion sequence is considered to be a subsequence of
10847 any non-identity conversion sequence */
10850 while (t1
->kind
!= ck_identity
)
10851 t1
= next_conversion (t1
);
10852 from_type1
= t1
->type
;
10855 while (t2
->kind
!= ck_identity
)
10856 t2
= next_conversion (t2
);
10857 from_type2
= t2
->type
;
10860 /* One sequence can only be a subsequence of the other if they start with
10861 the same type. They can start with different types when comparing the
10862 second standard conversion sequence in two user-defined conversion
10864 if (same_type_p (from_type1
, from_type2
))
10866 if (is_subseq (ics1
, ics2
))
10868 if (is_subseq (ics2
, ics1
))
10876 --the rank of S1 is better than the rank of S2 (by the rules
10879 Standard conversion sequences are ordered by their ranks: an Exact
10880 Match is a better conversion than a Promotion, which is a better
10881 conversion than a Conversion.
10883 Two conversion sequences with the same rank are indistinguishable
10884 unless one of the following rules applies:
10886 --A conversion that does not a convert a pointer, pointer to member,
10887 or std::nullptr_t to bool is better than one that does.
10889 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
10890 so that we do not have to check it explicitly. */
10891 if (ics1
->rank
< ics2
->rank
)
10893 else if (ics2
->rank
< ics1
->rank
)
10896 to_type1
= ics1
->type
;
10897 to_type2
= ics2
->type
;
10899 /* A conversion from scalar arithmetic type to complex is worse than a
10900 conversion between scalar arithmetic types. */
10901 if (same_type_p (from_type1
, from_type2
)
10902 && ARITHMETIC_TYPE_P (from_type1
)
10903 && ARITHMETIC_TYPE_P (to_type1
)
10904 && ARITHMETIC_TYPE_P (to_type2
)
10905 && ((TREE_CODE (to_type1
) == COMPLEX_TYPE
)
10906 != (TREE_CODE (to_type2
) == COMPLEX_TYPE
)))
10908 if (TREE_CODE (to_type1
) == COMPLEX_TYPE
)
10914 if (TYPE_PTR_P (from_type1
)
10915 && TYPE_PTR_P (from_type2
)
10916 && TYPE_PTR_P (to_type1
)
10917 && TYPE_PTR_P (to_type2
))
10919 deref_from_type1
= TREE_TYPE (from_type1
);
10920 deref_from_type2
= TREE_TYPE (from_type2
);
10921 deref_to_type1
= TREE_TYPE (to_type1
);
10922 deref_to_type2
= TREE_TYPE (to_type2
);
10924 /* The rules for pointers to members A::* are just like the rules
10925 for pointers A*, except opposite: if B is derived from A then
10926 A::* converts to B::*, not vice versa. For that reason, we
10927 switch the from_ and to_ variables here. */
10928 else if ((TYPE_PTRDATAMEM_P (from_type1
) && TYPE_PTRDATAMEM_P (from_type2
)
10929 && TYPE_PTRDATAMEM_P (to_type1
) && TYPE_PTRDATAMEM_P (to_type2
))
10930 || (TYPE_PTRMEMFUNC_P (from_type1
)
10931 && TYPE_PTRMEMFUNC_P (from_type2
)
10932 && TYPE_PTRMEMFUNC_P (to_type1
)
10933 && TYPE_PTRMEMFUNC_P (to_type2
)))
10935 deref_to_type1
= TYPE_PTRMEM_CLASS_TYPE (from_type1
);
10936 deref_to_type2
= TYPE_PTRMEM_CLASS_TYPE (from_type2
);
10937 deref_from_type1
= TYPE_PTRMEM_CLASS_TYPE (to_type1
);
10938 deref_from_type2
= TYPE_PTRMEM_CLASS_TYPE (to_type2
);
10941 if (deref_from_type1
!= NULL_TREE
10942 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1
))
10943 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2
)))
10945 /* This was one of the pointer or pointer-like conversions.
10949 --If class B is derived directly or indirectly from class A,
10950 conversion of B* to A* is better than conversion of B* to
10951 void*, and conversion of A* to void* is better than
10952 conversion of B* to void*. */
10953 if (VOID_TYPE_P (deref_to_type1
)
10954 && VOID_TYPE_P (deref_to_type2
))
10956 if (is_properly_derived_from (deref_from_type1
,
10959 else if (is_properly_derived_from (deref_from_type2
,
10963 else if (VOID_TYPE_P (deref_to_type1
)
10964 || VOID_TYPE_P (deref_to_type2
))
10966 if (same_type_p (deref_from_type1
, deref_from_type2
))
10968 if (VOID_TYPE_P (deref_to_type2
))
10970 if (is_properly_derived_from (deref_from_type1
,
10974 /* We know that DEREF_TO_TYPE1 is `void' here. */
10975 else if (is_properly_derived_from (deref_from_type1
,
10980 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1
))
10981 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2
)))
10985 --If class B is derived directly or indirectly from class A
10986 and class C is derived directly or indirectly from B,
10988 --conversion of C* to B* is better than conversion of C* to
10991 --conversion of B* to A* is better than conversion of C* to
10993 if (same_type_p (deref_from_type1
, deref_from_type2
))
10995 if (is_properly_derived_from (deref_to_type1
,
10998 else if (is_properly_derived_from (deref_to_type2
,
11002 else if (same_type_p (deref_to_type1
, deref_to_type2
))
11004 if (is_properly_derived_from (deref_from_type2
,
11007 else if (is_properly_derived_from (deref_from_type1
,
11013 else if (CLASS_TYPE_P (non_reference (from_type1
))
11014 && same_type_p (from_type1
, from_type2
))
11016 tree from
= non_reference (from_type1
);
11020 --binding of an expression of type C to a reference of type
11021 B& is better than binding an expression of type C to a
11022 reference of type A&
11024 --conversion of C to B is better than conversion of C to A, */
11025 if (is_properly_derived_from (from
, to_type1
)
11026 && is_properly_derived_from (from
, to_type2
))
11028 if (is_properly_derived_from (to_type1
, to_type2
))
11030 else if (is_properly_derived_from (to_type2
, to_type1
))
11034 else if (CLASS_TYPE_P (non_reference (to_type1
))
11035 && same_type_p (to_type1
, to_type2
))
11037 tree to
= non_reference (to_type1
);
11041 --binding of an expression of type B to a reference of type
11042 A& is better than binding an expression of type C to a
11043 reference of type A&,
11045 --conversion of B to A is better than conversion of C to A */
11046 if (is_properly_derived_from (from_type1
, to
)
11047 && is_properly_derived_from (from_type2
, to
))
11049 if (is_properly_derived_from (from_type2
, from_type1
))
11051 else if (is_properly_derived_from (from_type1
, from_type2
))
11058 --S1 and S2 differ only in their qualification conversion and yield
11059 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
11060 qualification signature of type T1 is a proper subset of the cv-
11061 qualification signature of type T2 */
11062 if (ics1
->kind
== ck_qual
11063 && ics2
->kind
== ck_qual
11064 && same_type_p (from_type1
, from_type2
))
11066 int result
= comp_cv_qual_signature (to_type1
, to_type2
);
11073 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
11074 to an implicit object parameter of a non-static member function
11075 declared without a ref-qualifier, and either S1 binds an lvalue
11076 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
11077 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
11078 draft standard, 13.3.3.2)
11080 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
11081 types to which the references refer are the same type except for
11082 top-level cv-qualifiers, and the type to which the reference
11083 initialized by S2 refers is more cv-qualified than the type to
11084 which the reference initialized by S1 refers.
11086 DR 1328 [over.match.best]: the context is an initialization by
11087 conversion function for direct reference binding (13.3.1.6) of a
11088 reference to function type, the return type of F1 is the same kind of
11089 reference (i.e. lvalue or rvalue) as the reference being initialized,
11090 and the return type of F2 is not. */
11092 if (ref_conv1
&& ref_conv2
)
11094 if (!ref_conv1
->this_p
&& !ref_conv2
->this_p
11095 && (ref_conv1
->rvaluedness_matches_p
11096 != ref_conv2
->rvaluedness_matches_p
)
11097 && (same_type_p (ref_conv1
->type
, ref_conv2
->type
)
11098 || (TYPE_REF_IS_RVALUE (ref_conv1
->type
)
11099 != TYPE_REF_IS_RVALUE (ref_conv2
->type
))))
11101 if (ref_conv1
->bad_p
11102 && !same_type_p (TREE_TYPE (ref_conv1
->type
),
11103 TREE_TYPE (ref_conv2
->type
)))
11104 /* Don't prefer a bad conversion that drops cv-quals to a bad
11105 conversion with the wrong rvalueness. */
11107 return (ref_conv1
->rvaluedness_matches_p
11108 - ref_conv2
->rvaluedness_matches_p
);
11111 if (same_type_ignoring_top_level_qualifiers_p (to_type1
, to_type2
))
11115 void f (int(&)[]), // (1)
11116 f (int(&)[1]), // (2)
11119 (2) is better than (1), but (3) should be equal to (1) and to
11120 (2). For that reason we don't use ck_qual for (1) which would
11121 give it the cr_exact rank while (3) remains ck_identity.
11122 Therefore we compare (1) and (2) here. For (1) we'll have
11124 ck_ref_bind <- ck_identity
11127 so to handle this we must look at ref_conv. */
11128 bool c1
= conv_binds_to_array_of_unknown_bound (ref_conv1
);
11129 bool c2
= conv_binds_to_array_of_unknown_bound (ref_conv2
);
11132 else if (!c1
&& c2
)
11135 int q1
= cp_type_quals (TREE_TYPE (ref_conv1
->type
));
11136 int q2
= cp_type_quals (TREE_TYPE (ref_conv2
->type
));
11137 if (ref_conv1
->bad_p
)
11139 /* Prefer the one that drops fewer cv-quals. */
11140 tree ftype
= next_conversion (ref_conv1
)->type
;
11141 int fquals
= cp_type_quals (ftype
);
11145 return comp_cv_qualification (q2
, q1
);
11152 -- A conversion that promotes an enumeration whose underlying type
11153 is fixed to its underlying type is better than one that promotes to
11154 the promoted underlying type, if the two are different. */
11155 if (ics1
->rank
== cr_promotion
11156 && ics2
->rank
== cr_promotion
11157 && UNSCOPED_ENUM_P (from_type1
)
11158 && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1
)
11159 && same_type_p (from_type1
, from_type2
))
11161 tree utype
= ENUM_UNDERLYING_TYPE (from_type1
);
11162 tree prom
= type_promotes_to (from_type1
);
11163 if (!same_type_p (utype
, prom
))
11165 if (same_type_p (to_type1
, utype
)
11166 && same_type_p (to_type2
, prom
))
11168 else if (same_type_p (to_type2
, utype
)
11169 && same_type_p (to_type1
, prom
))
11174 /* Neither conversion sequence is better than the other. */
11178 /* The source type for this standard conversion sequence. */
11181 source_type (conversion
*t
)
11183 return strip_standard_conversion (t
)->type
;
11186 /* Note a warning about preferring WINNER to LOSER. We do this by storing
11187 a pointer to LOSER and re-running joust to produce the warning if WINNER
11188 is actually used. */
11191 add_warning (struct z_candidate
*winner
, struct z_candidate
*loser
)
11193 candidate_warning
*cw
= (candidate_warning
*)
11194 conversion_obstack_alloc (sizeof (candidate_warning
));
11196 cw
->next
= winner
->warnings
;
11197 winner
->warnings
= cw
;
11200 /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
11201 prvalue returned from a conversion function, replace CAND with the candidate
11202 for the conversion and return true. Otherwise, return false. */
11205 joust_maybe_elide_copy (z_candidate
*&cand
)
11207 tree fn
= cand
->fn
;
11208 if (!DECL_COPY_CONSTRUCTOR_P (fn
) && !DECL_MOVE_CONSTRUCTOR_P (fn
))
11210 conversion
*conv
= cand
->convs
[0];
11211 gcc_checking_assert (conv
->kind
== ck_ref_bind
);
11212 conv
= next_conversion (conv
);
11213 if (conv
->kind
== ck_user
&& !TYPE_REF_P (conv
->type
))
11215 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
11216 (conv
->type
, DECL_CONTEXT (fn
)));
11217 z_candidate
*uc
= conv
->cand
;
11218 if (DECL_CONV_FN_P (uc
->fn
))
11227 /* True if the defining declarations of the two candidates have equivalent
11231 cand_parms_match (z_candidate
*c1
, z_candidate
*c2
)
11237 if (identifier_p (fn1
) || identifier_p (fn2
))
11239 /* We don't look at c1->template_decl because that's only set for primary
11240 templates, not e.g. non-template member functions of class templates. */
11241 tree t1
= most_general_template (fn1
);
11242 tree t2
= most_general_template (fn2
);
11249 fn1
= DECL_TEMPLATE_RESULT (t1
);
11250 fn2
= DECL_TEMPLATE_RESULT (t2
);
11252 return compparms (TYPE_ARG_TYPES (TREE_TYPE (fn1
)),
11253 TYPE_ARG_TYPES (TREE_TYPE (fn2
)));
11256 /* Compare two candidates for overloading as described in
11257 [over.match.best]. Return values:
11259 1: cand1 is better than cand2
11260 -1: cand2 is better than cand1
11261 0: cand1 and cand2 are indistinguishable */
11264 joust (struct z_candidate
*cand1
, struct z_candidate
*cand2
, bool warn
,
11265 tsubst_flags_t complain
)
11268 int off1
= 0, off2
= 0;
11272 /* Candidates that involve bad conversions are always worse than those
11274 if (cand1
->viable
> cand2
->viable
)
11276 if (cand1
->viable
< cand2
->viable
)
11279 /* If we have two pseudo-candidates for conversions to the same type,
11280 or two candidates for the same function, arbitrarily pick one. */
11281 if (cand1
->fn
== cand2
->fn
11282 && cand1
->reversed () == cand2
->reversed ()
11283 && (IS_TYPE_OR_DECL_P (cand1
->fn
)))
11286 /* Prefer a non-deleted function over an implicitly deleted move
11287 constructor or assignment operator. This differs slightly from the
11288 wording for issue 1402 (which says the move op is ignored by overload
11289 resolution), but this way produces better error messages. */
11290 if (TREE_CODE (cand1
->fn
) == FUNCTION_DECL
11291 && TREE_CODE (cand2
->fn
) == FUNCTION_DECL
11292 && DECL_DELETED_FN (cand1
->fn
) != DECL_DELETED_FN (cand2
->fn
))
11294 if (DECL_DELETED_FN (cand1
->fn
) && DECL_DEFAULTED_FN (cand1
->fn
)
11295 && move_fn_p (cand1
->fn
))
11297 if (DECL_DELETED_FN (cand2
->fn
) && DECL_DEFAULTED_FN (cand2
->fn
)
11298 && move_fn_p (cand2
->fn
))
11302 /* a viable function F1
11303 is defined to be a better function than another viable function F2 if
11304 for all arguments i, ICSi(F1) is not a worse conversion sequence than
11305 ICSi(F2), and then */
11307 /* for some argument j, ICSj(F1) is a better conversion sequence than
11310 /* For comparing static and non-static member functions, we ignore
11311 the implicit object parameter of the non-static function. The
11312 standard says to pretend that the static function has an object
11313 parm, but that won't work with operator overloading. */
11314 len
= cand1
->num_convs
;
11315 if (len
!= cand2
->num_convs
)
11317 int static_1
= DECL_STATIC_FUNCTION_P (cand1
->fn
);
11318 int static_2
= DECL_STATIC_FUNCTION_P (cand2
->fn
);
11320 if (DECL_CONSTRUCTOR_P (cand1
->fn
)
11321 && is_list_ctor (cand1
->fn
) != is_list_ctor (cand2
->fn
))
11322 /* We're comparing a near-match list constructor and a near-match
11323 non-list constructor. Just treat them as unordered. */
11326 gcc_assert (static_1
!= static_2
);
11337 /* Handle C++17 copy elision in [over.match.ctor] (direct-init) context. The
11338 standard currently says that only constructors are candidates, but if one
11339 copies a prvalue returned by a conversion function we want to treat the
11340 conversion as the candidate instead.
11342 Clang does something similar, as discussed at
11343 http://lists.isocpp.org/core/2017/10/3166.php
11344 http://lists.isocpp.org/core/2019/03/5721.php */
11345 int elided_tiebreaker
= 0;
11346 if (len
== 1 && cxx_dialect
>= cxx17
11347 && DECL_P (cand1
->fn
)
11348 && DECL_COMPLETE_CONSTRUCTOR_P (cand1
->fn
)
11349 && !(cand1
->flags
& LOOKUP_ONLYCONVERTING
))
11351 bool elided1
= joust_maybe_elide_copy (cand1
);
11352 bool elided2
= joust_maybe_elide_copy (cand2
);
11353 /* As a tiebreaker below we will prefer a constructor to a conversion
11354 operator exposed this way. */
11355 elided_tiebreaker
= elided2
- elided1
;
11358 for (i
= 0; i
< len
; ++i
)
11360 conversion
*t1
= cand1
->convs
[i
+ off1
];
11361 conversion
*t2
= cand2
->convs
[i
+ off2
];
11362 int comp
= compare_ics (t1
, t2
);
11366 if ((complain
& tf_warning
)
11368 && (CONVERSION_RANK (t1
) + CONVERSION_RANK (t2
)
11369 == cr_std
+ cr_promotion
)
11370 && t1
->kind
== ck_std
11371 && t2
->kind
== ck_std
11372 && TREE_CODE (t1
->type
) == INTEGER_TYPE
11373 && TREE_CODE (t2
->type
) == INTEGER_TYPE
11374 && (TYPE_PRECISION (t1
->type
)
11375 == TYPE_PRECISION (t2
->type
))
11376 && (TYPE_UNSIGNED (next_conversion (t1
)->type
)
11377 || (TREE_CODE (next_conversion (t1
)->type
)
11378 == ENUMERAL_TYPE
)))
11380 tree type
= next_conversion (t1
)->type
;
11382 struct z_candidate
*w
, *l
;
11384 type1
= t1
->type
, type2
= t2
->type
,
11385 w
= cand1
, l
= cand2
;
11387 type1
= t2
->type
, type2
= t1
->type
,
11388 w
= cand2
, l
= cand1
;
11392 warning (OPT_Wsign_promo
, "passing %qT chooses %qT over %qT",
11393 type
, type1
, type2
);
11394 warning (OPT_Wsign_promo
, " in call to %qD", w
->fn
);
11397 add_warning (w
, l
);
11400 if (winner
&& comp
!= winner
)
11402 /* Ambiguity between normal and reversed comparison operators
11403 with the same parameter types; prefer the normal one. */
11404 if ((cand1
->reversed () != cand2
->reversed ())
11405 && cand_parms_match (cand1
, cand2
))
11406 return cand1
->reversed () ? -1 : 1;
11415 /* warn about confusing overload resolution for user-defined conversions,
11416 either between a constructor and a conversion op, or between two
11418 if ((complain
& tf_warning
)
11419 /* In C++17, the constructor might have been elided, which means that
11420 an originally null ->second_conv could become non-null. */
11421 && winner
&& warn_conversion
&& cand1
->second_conv
&& cand2
->second_conv
11422 && (!DECL_CONSTRUCTOR_P (cand1
->fn
) || !DECL_CONSTRUCTOR_P (cand2
->fn
))
11423 && winner
!= compare_ics (cand1
->second_conv
, cand2
->second_conv
))
11425 struct z_candidate
*w
, *l
;
11426 bool give_warning
= false;
11429 w
= cand1
, l
= cand2
;
11431 w
= cand2
, l
= cand1
;
11433 /* We don't want to complain about `X::operator T1 ()'
11434 beating `X::operator T2 () const', when T2 is a no less
11435 cv-qualified version of T1. */
11436 if (DECL_CONTEXT (w
->fn
) == DECL_CONTEXT (l
->fn
)
11437 && !DECL_CONSTRUCTOR_P (w
->fn
) && !DECL_CONSTRUCTOR_P (l
->fn
))
11439 tree t
= TREE_TYPE (TREE_TYPE (l
->fn
));
11440 tree f
= TREE_TYPE (TREE_TYPE (w
->fn
));
11442 if (TREE_CODE (t
) == TREE_CODE (f
) && INDIRECT_TYPE_P (t
))
11447 if (!comp_ptr_ttypes (t
, f
))
11448 give_warning
= true;
11451 give_warning
= true;
11457 tree source
= source_type (w
->convs
[0]);
11458 if (INDIRECT_TYPE_P (source
))
11459 source
= TREE_TYPE (source
);
11460 auto_diagnostic_group d
;
11461 if (warning (OPT_Wconversion
, "choosing %qD over %qD", w
->fn
, l
->fn
)
11462 && warning (OPT_Wconversion
, " for conversion from %qH to %qI",
11463 source
, w
->second_conv
->type
))
11465 inform (input_location
, " because conversion sequence "
11466 "for the argument is better");
11470 add_warning (w
, l
);
11476 /* Put this tiebreaker first, so that we don't try to look at second_conv of
11477 a constructor candidate that doesn't have one. */
11478 if (elided_tiebreaker
)
11479 return elided_tiebreaker
;
11481 /* DR 495 moved this tiebreaker above the template ones. */
11482 /* or, if not that,
11483 the context is an initialization by user-defined conversion (see
11484 _dcl.init_ and _over.match.user_) and the standard conversion
11485 sequence from the return type of F1 to the destination type (i.e.,
11486 the type of the entity being initialized) is a better conversion
11487 sequence than the standard conversion sequence from the return type
11488 of F2 to the destination type. */
11490 if (cand1
->second_conv
)
11492 winner
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
11497 /* or, if not that,
11498 F1 is a non-template function and F2 is a template function
11501 if (!cand1
->template_decl
&& cand2
->template_decl
)
11503 else if (cand1
->template_decl
&& !cand2
->template_decl
)
11506 /* or, if not that,
11507 F1 and F2 are template functions and the function template for F1 is
11508 more specialized than the template for F2 according to the partial
11511 if (cand1
->template_decl
&& cand2
->template_decl
)
11513 winner
= more_specialized_fn
11514 (TI_TEMPLATE (cand1
->template_decl
),
11515 TI_TEMPLATE (cand2
->template_decl
),
11516 /* [temp.func.order]: The presence of unused ellipsis and default
11517 arguments has no effect on the partial ordering of function
11518 templates. add_function_candidate() will not have
11519 counted the "this" argument for constructors. */
11520 cand1
->num_convs
+ DECL_CONSTRUCTOR_P (cand1
->fn
));
11525 /* Concepts: F1 and F2 are non-template functions with the same
11526 parameter-type-lists, and F1 is more constrained than F2 according to the
11527 partial ordering of constraints described in 13.5.4. */
11529 if (flag_concepts
&& DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
11530 && !cand1
->template_decl
&& !cand2
->template_decl
11531 && cand_parms_match (cand1
, cand2
))
11533 winner
= more_constrained (cand1
->fn
, cand2
->fn
);
11538 /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
11539 rewritten candidates, and F2 is a synthesized candidate with reversed
11540 order of parameters and F1 is not. */
11541 if (cand1
->rewritten ())
11543 if (!cand2
->rewritten ())
11545 if (!cand1
->reversed () && cand2
->reversed ())
11547 if (cand1
->reversed () && !cand2
->reversed ())
11550 else if (cand2
->rewritten ())
11553 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
11554 if (deduction_guide_p (cand1
->fn
))
11556 gcc_assert (deduction_guide_p (cand2
->fn
));
11557 /* We distinguish between candidates from an explicit deduction guide and
11558 candidates built from a constructor based on DECL_ARTIFICIAL. */
11559 int art1
= DECL_ARTIFICIAL (cand1
->fn
);
11560 int art2
= DECL_ARTIFICIAL (cand2
->fn
);
11562 return art2
- art1
;
11566 /* Prefer the special copy guide over a declared copy/move
11568 if (copy_guide_p (cand1
->fn
))
11570 if (copy_guide_p (cand2
->fn
))
11573 /* Prefer a candidate generated from a non-template constructor. */
11574 int tg1
= template_guide_p (cand1
->fn
);
11575 int tg2
= template_guide_p (cand2
->fn
);
11581 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
11582 for all arguments the corresponding parameters of F1 and F2 have the same
11583 type (CWG 2273/2277). */
11584 if (DECL_P (cand1
->fn
) && DECL_CLASS_SCOPE_P (cand1
->fn
)
11585 && !DECL_CONV_FN_P (cand1
->fn
)
11586 && DECL_P (cand2
->fn
) && DECL_CLASS_SCOPE_P (cand2
->fn
)
11587 && !DECL_CONV_FN_P (cand2
->fn
))
11589 tree base1
= DECL_CONTEXT (strip_inheriting_ctors (cand1
->fn
));
11590 tree base2
= DECL_CONTEXT (strip_inheriting_ctors (cand2
->fn
));
11592 bool used1
= false;
11593 bool used2
= false;
11594 if (base1
== base2
)
11595 /* No difference. */;
11596 else if (DERIVED_FROM_P (base1
, base2
))
11598 else if (DERIVED_FROM_P (base2
, base1
))
11601 if (int diff
= used2
- used1
)
11603 for (i
= 0; i
< len
; ++i
)
11605 conversion
*t1
= cand1
->convs
[i
+ off1
];
11606 conversion
*t2
= cand2
->convs
[i
+ off2
];
11607 if (!same_type_p (t1
->type
, t2
->type
))
11615 /* Check whether we can discard a builtin candidate, either because we
11616 have two identical ones or matching builtin and non-builtin candidates.
11618 (Pedantically in the latter case the builtin which matched the user
11619 function should not be added to the overload set, but we spot it here.
11622 ... the builtin candidates include ...
11623 - do not have the same parameter type list as any non-template
11624 non-member candidate. */
11626 if (identifier_p (cand1
->fn
) || identifier_p (cand2
->fn
))
11628 for (i
= 0; i
< len
; ++i
)
11629 if (!same_type_p (cand1
->convs
[i
]->type
,
11630 cand2
->convs
[i
]->type
))
11632 if (i
== cand1
->num_convs
)
11634 if (cand1
->fn
== cand2
->fn
)
11635 /* Two built-in candidates; arbitrarily pick one. */
11637 else if (identifier_p (cand1
->fn
))
11638 /* cand1 is built-in; prefer cand2. */
11641 /* cand2 is built-in; prefer cand1. */
11646 /* For candidates of a multi-versioned function, make the version with
11647 the highest priority win. This version will be checked for dispatching
11648 first. If this version can be inlined into the caller, the front-end
11649 will simply make a direct call to this function. */
11651 if (TREE_CODE (cand1
->fn
) == FUNCTION_DECL
11652 && DECL_FUNCTION_VERSIONED (cand1
->fn
)
11653 && TREE_CODE (cand2
->fn
) == FUNCTION_DECL
11654 && DECL_FUNCTION_VERSIONED (cand2
->fn
))
11656 tree f1
= TREE_TYPE (cand1
->fn
);
11657 tree f2
= TREE_TYPE (cand2
->fn
);
11658 tree p1
= TYPE_ARG_TYPES (f1
);
11659 tree p2
= TYPE_ARG_TYPES (f2
);
11661 /* Check if cand1->fn and cand2->fn are versions of the same function. It
11662 is possible that cand1->fn and cand2->fn are function versions but of
11663 different functions. Check types to see if they are versions of the same
11665 if (compparms (p1
, p2
)
11666 && same_type_p (TREE_TYPE (f1
), TREE_TYPE (f2
)))
11668 /* Always make the version with the higher priority, more
11669 specialized, win. */
11670 gcc_assert (targetm
.compare_version_priority
);
11671 if (targetm
.compare_version_priority (cand1
->fn
, cand2
->fn
) >= 0)
11678 /* If the two function declarations represent the same function (this can
11679 happen with declarations in multiple scopes and arg-dependent lookup),
11680 arbitrarily choose one. But first make sure the default args we're
11682 if (DECL_P (cand1
->fn
) && DECL_P (cand2
->fn
)
11683 && equal_functions (cand1
->fn
, cand2
->fn
))
11685 tree parms1
= TYPE_ARG_TYPES (TREE_TYPE (cand1
->fn
));
11686 tree parms2
= TYPE_ARG_TYPES (TREE_TYPE (cand2
->fn
));
11688 gcc_assert (!DECL_CONSTRUCTOR_P (cand1
->fn
));
11690 for (i
= 0; i
< len
; ++i
)
11692 /* Don't crash if the fn is variadic. */
11695 parms1
= TREE_CHAIN (parms1
);
11696 parms2
= TREE_CHAIN (parms2
);
11700 parms1
= TREE_CHAIN (parms1
);
11702 parms2
= TREE_CHAIN (parms2
);
11704 for (; parms1
; ++i
)
11706 if (!cp_tree_equal (TREE_PURPOSE (parms1
),
11707 TREE_PURPOSE (parms2
)))
11711 if (complain
& tf_error
)
11713 auto_diagnostic_group d
;
11714 if (permerror (input_location
,
11715 "default argument mismatch in "
11716 "overload resolution"))
11718 inform (DECL_SOURCE_LOCATION (cand1
->fn
),
11719 " candidate 1: %q#F", cand1
->fn
);
11720 inform (DECL_SOURCE_LOCATION (cand2
->fn
),
11721 " candidate 2: %q#F", cand2
->fn
);
11728 add_warning (cand1
, cand2
);
11731 parms1
= TREE_CHAIN (parms1
);
11732 parms2
= TREE_CHAIN (parms2
);
11740 /* Extension: If the worst conversion for one candidate is better than the
11741 worst conversion for the other, take the first. */
11742 if (!pedantic
&& (complain
& tf_warning_or_error
))
11744 conversion_rank rank1
= cr_identity
, rank2
= cr_identity
;
11745 struct z_candidate
*w
= 0, *l
= 0;
11747 for (i
= 0; i
< len
; ++i
)
11749 if (CONVERSION_RANK (cand1
->convs
[i
+off1
]) > rank1
)
11750 rank1
= CONVERSION_RANK (cand1
->convs
[i
+off1
]);
11751 if (CONVERSION_RANK (cand2
->convs
[i
+ off2
]) > rank2
)
11752 rank2
= CONVERSION_RANK (cand2
->convs
[i
+ off2
]);
11755 winner
= 1, w
= cand1
, l
= cand2
;
11757 winner
= -1, w
= cand2
, l
= cand1
;
11760 /* Don't choose a deleted function over ambiguity. */
11761 if (DECL_P (w
->fn
) && DECL_DELETED_FN (w
->fn
))
11765 auto_diagnostic_group d
;
11766 if (pedwarn (input_location
, 0,
11767 "ISO C++ says that these are ambiguous, even "
11768 "though the worst conversion for the first is "
11769 "better than the worst conversion for the second:"))
11771 print_z_candidate (input_location
, N_("candidate 1:"), w
);
11772 print_z_candidate (input_location
, N_("candidate 2:"), l
);
11776 add_warning (w
, l
);
11781 gcc_assert (!winner
);
11785 /* Given a list of candidates for overloading, find the best one, if any.
11786 This algorithm has a worst case of O(2n) (winner is last), and a best
11787 case of O(n/2) (totally ambiguous); much better than a sorting
11790 static struct z_candidate
*
11791 tourney (struct z_candidate
*candidates
, tsubst_flags_t complain
)
11793 struct z_candidate
*champ
= candidates
, *challenger
;
11795 int champ_compared_to_predecessor
= 0;
11797 /* Walk through the list once, comparing each current champ to the next
11798 candidate, knocking out a candidate or two with each comparison. */
11800 for (challenger
= champ
->next
; challenger
; )
11802 fate
= joust (champ
, challenger
, 0, complain
);
11804 challenger
= challenger
->next
;
11809 champ
= challenger
->next
;
11812 champ_compared_to_predecessor
= 0;
11816 champ
= challenger
;
11817 champ_compared_to_predecessor
= 1;
11820 challenger
= champ
->next
;
11824 /* Make sure the champ is better than all the candidates it hasn't yet
11825 been compared to. */
11827 for (challenger
= candidates
;
11828 challenger
!= champ
11829 && !(champ_compared_to_predecessor
&& challenger
->next
== champ
);
11830 challenger
= challenger
->next
)
11832 fate
= joust (champ
, challenger
, 0, complain
);
11840 /* Returns nonzero if things of type FROM can be converted to TO. */
11843 can_convert (tree to
, tree from
, tsubst_flags_t complain
)
11845 tree arg
= NULL_TREE
;
11846 /* implicit_conversion only considers user-defined conversions
11847 if it has an expression for the call argument list. */
11848 if (CLASS_TYPE_P (from
) || CLASS_TYPE_P (to
))
11849 arg
= build1 (CAST_EXPR
, from
, NULL_TREE
);
11850 return can_convert_arg (to
, from
, arg
, LOOKUP_IMPLICIT
, complain
);
11853 /* Returns nonzero if things of type FROM can be converted to TO with a
11854 standard conversion. */
11857 can_convert_standard (tree to
, tree from
, tsubst_flags_t complain
)
11859 return can_convert_arg (to
, from
, NULL_TREE
, LOOKUP_IMPLICIT
, complain
);
11862 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
11865 can_convert_arg (tree to
, tree from
, tree arg
, int flags
,
11866 tsubst_flags_t complain
)
11872 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11873 p
= conversion_obstack_alloc (0);
11874 /* We want to discard any access checks done for this test,
11875 as we might not be in the appropriate access context and
11876 we'll do the check again when we actually perform the
11878 push_deferring_access_checks (dk_deferred
);
11880 t
= implicit_conversion (to
, from
, arg
, /*c_cast_p=*/false,
11882 ok_p
= (t
&& !t
->bad_p
);
11884 /* Discard the access checks now. */
11885 pop_deferring_access_checks ();
11886 /* Free all the conversions we allocated. */
11887 obstack_free (&conversion_obstack
, p
);
11892 /* Like can_convert_arg, but allows dubious conversions as well. */
11895 can_convert_arg_bad (tree to
, tree from
, tree arg
, int flags
,
11896 tsubst_flags_t complain
)
11901 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11902 p
= conversion_obstack_alloc (0);
11903 /* Try to perform the conversion. */
11904 t
= implicit_conversion (to
, from
, arg
, /*c_cast_p=*/false,
11906 /* Free all the conversions we allocated. */
11907 obstack_free (&conversion_obstack
, p
);
11912 /* Convert EXPR to TYPE. Return the converted expression.
11914 Note that we allow bad conversions here because by the time we get to
11915 this point we are committed to doing the conversion. If we end up
11916 doing a bad conversion, convert_like will complain. */
11919 perform_implicit_conversion_flags (tree type
, tree expr
,
11920 tsubst_flags_t complain
, int flags
)
11924 location_t loc
= cp_expr_loc_or_input_loc (expr
);
11926 if (TYPE_REF_P (type
))
11927 expr
= mark_lvalue_use (expr
);
11929 expr
= mark_rvalue_use (expr
);
11931 if (error_operand_p (expr
))
11932 return error_mark_node
;
11934 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11935 p
= conversion_obstack_alloc (0);
11937 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
11938 /*c_cast_p=*/false,
11943 if (complain
& tf_error
)
11944 implicit_conversion_error (loc
, type
, expr
);
11945 expr
= error_mark_node
;
11947 else if (processing_template_decl
&& conv
->kind
!= ck_identity
)
11949 /* In a template, we are only concerned about determining the
11950 type of non-dependent expressions, so we do not have to
11951 perform the actual conversion. But for initializers, we
11952 need to be able to perform it at instantiation
11953 (or instantiate_non_dependent_expr) time. */
11954 expr
= build1 (IMPLICIT_CONV_EXPR
, type
, expr
);
11955 if (!(flags
& LOOKUP_ONLYCONVERTING
))
11956 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr
) = true;
11957 if (flags
& LOOKUP_NO_NARROWING
)
11958 IMPLICIT_CONV_EXPR_BRACED_INIT (expr
) = true;
11961 expr
= convert_like (conv
, expr
, complain
);
11963 /* Free all the conversions we allocated. */
11964 obstack_free (&conversion_obstack
, p
);
11970 perform_implicit_conversion (tree type
, tree expr
, tsubst_flags_t complain
)
11972 return perform_implicit_conversion_flags (type
, expr
, complain
,
11976 /* Convert EXPR to TYPE (as a direct-initialization) if that is
11977 permitted. If the conversion is valid, the converted expression is
11978 returned. Otherwise, NULL_TREE is returned, except in the case
11979 that TYPE is a class type; in that case, an error is issued. If
11980 C_CAST_P is true, then this direct-initialization is taking
11981 place as part of a static_cast being attempted as part of a C-style
11985 perform_direct_initialization_if_possible (tree type
,
11988 tsubst_flags_t complain
)
11993 if (type
== error_mark_node
|| error_operand_p (expr
))
11994 return error_mark_node
;
11997 If the destination type is a (possibly cv-qualified) class type:
11999 -- If the initialization is direct-initialization ...,
12000 constructors are considered.
12002 -- If overload resolution is successful, the selected constructor
12003 is called to initialize the object, with the initializer expression
12004 or expression-list as its argument(s).
12006 -- Otherwise, if no constructor is viable, the destination type is
12007 a (possibly cv-qualified) aggregate class A, and the initializer is
12008 a parenthesized expression-list, the object is initialized as
12010 if (CLASS_TYPE_P (type
))
12012 releasing_vec
args (make_tree_vector_single (expr
));
12013 expr
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
12014 &args
, type
, LOOKUP_NORMAL
, complain
);
12015 return build_cplus_new (type
, expr
, complain
);
12018 /* Get the high-water mark for the CONVERSION_OBSTACK. */
12019 p
= conversion_obstack_alloc (0);
12021 conv
= implicit_conversion (type
, TREE_TYPE (expr
), expr
,
12023 LOOKUP_NORMAL
, complain
);
12024 if (!conv
|| conv
->bad_p
)
12026 else if (processing_template_decl
&& conv
->kind
!= ck_identity
)
12028 /* In a template, we are only concerned about determining the
12029 type of non-dependent expressions, so we do not have to
12030 perform the actual conversion. But for initializers, we
12031 need to be able to perform it at instantiation
12032 (or instantiate_non_dependent_expr) time. */
12033 expr
= build1 (IMPLICIT_CONV_EXPR
, type
, expr
);
12034 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr
) = true;
12037 expr
= convert_like (conv
, expr
, NULL_TREE
, 0,
12038 /*issue_conversion_warnings=*/false,
12039 c_cast_p
, complain
);
12041 /* Free all the conversions we allocated. */
12042 obstack_free (&conversion_obstack
, p
);
12047 /* When initializing a reference that lasts longer than a full-expression,
12048 this special rule applies:
12052 The temporary to which the reference is bound or the temporary
12053 that is the complete object to which the reference is bound
12054 persists for the lifetime of the reference.
12056 The temporaries created during the evaluation of the expression
12057 initializing the reference, except the temporary to which the
12058 reference is bound, are destroyed at the end of the
12059 full-expression in which they are created.
12061 In that case, we store the converted expression into a new
12062 VAR_DECL in a new scope.
12064 However, we want to be careful not to create temporaries when
12065 they are not required. For example, given:
12068 struct D : public B {};
12072 there is no need to copy the return value from "f"; we can just
12073 extend its lifetime. Similarly, given:
12076 struct T { operator S(); };
12080 we can extend the lifetime of the return value of the conversion
12083 The next several functions are involved in this lifetime extension. */
12085 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
12086 reference is being bound to a temporary. Create and return a new
12087 VAR_DECL with the indicated TYPE; this variable will store the value to
12088 which the reference is bound. */
12091 make_temporary_var_for_ref_to_temp (tree decl
, tree type
)
12093 tree var
= create_temporary_var (type
);
12095 /* Register the variable. */
12097 && (TREE_STATIC (decl
) || CP_DECL_THREAD_LOCAL_P (decl
)))
12099 /* Namespace-scope or local static; give it a mangled name. */
12101 /* If an initializer is visible to multiple translation units, those
12102 translation units must agree on the addresses of the
12103 temporaries. Therefore the temporaries must be given a consistent name
12104 and vague linkage. The mangled name of a temporary is the name of the
12105 non-temporary object in whose initializer they appear, prefixed with
12106 GR and suffixed with a sequence number mangled using the usual rules
12107 for a seq-id. Temporaries are numbered with a pre-order, depth-first,
12108 left-to-right walk of the complete initializer. */
12109 copy_linkage (var
, decl
);
12111 tree name
= mangle_ref_init_variable (decl
);
12112 DECL_NAME (var
) = name
;
12113 SET_DECL_ASSEMBLER_NAME (var
, name
);
12116 /* Create a new cleanup level if necessary. */
12117 maybe_push_cleanup_level (type
);
12119 return pushdecl (var
);
12122 /* EXPR is the initializer for a variable DECL of reference or
12123 std::initializer_list type. Create, push and return a new VAR_DECL
12124 for the initializer so that it will live as long as DECL. Any
12125 cleanup for the new variable is returned through CLEANUP, and the
12126 code to initialize the new variable is returned through INITP. */
12129 set_up_extended_ref_temp (tree decl
, tree expr
, vec
<tree
, va_gc
> **cleanups
,
12130 tree
*initp
, tree
*cond_guard
)
12136 /* Create the temporary variable. */
12137 type
= TREE_TYPE (expr
);
12138 var
= make_temporary_var_for_ref_to_temp (decl
, type
);
12139 layout_decl (var
, 0);
12140 /* If the rvalue is the result of a function call it will be
12141 a TARGET_EXPR. If it is some other construct (such as a
12142 member access expression where the underlying object is
12143 itself the result of a function call), turn it into a
12144 TARGET_EXPR here. It is important that EXPR be a
12145 TARGET_EXPR below since otherwise the INIT_EXPR will
12146 attempt to make a bitwise copy of EXPR to initialize
12148 if (TREE_CODE (expr
) != TARGET_EXPR
)
12149 expr
= get_target_expr (expr
);
12151 if (TREE_CODE (decl
) == FIELD_DECL
12152 && extra_warnings
&& !TREE_NO_WARNING (decl
))
12154 warning (OPT_Wextra
, "a temporary bound to %qD only persists "
12155 "until the constructor exits", decl
);
12156 TREE_NO_WARNING (decl
) = true;
12159 /* Recursively extend temps in this initializer. */
12160 TARGET_EXPR_INITIAL (expr
)
12161 = extend_ref_init_temps (decl
, TARGET_EXPR_INITIAL (expr
), cleanups
,
12164 /* Any reference temp has a non-trivial initializer. */
12165 DECL_NONTRIVIALLY_INITIALIZED_P (var
) = true;
12167 /* If the initializer is constant, put it in DECL_INITIAL so we get
12168 static initialization and use in constant expressions. */
12169 init
= maybe_constant_init (expr
);
12170 /* As in store_init_value. */
12171 init
= cp_fully_fold (init
);
12172 if (TREE_CONSTANT (init
))
12174 if (literal_type_p (type
) && CP_TYPE_CONST_NON_VOLATILE_P (type
))
12176 /* 5.19 says that a constant expression can include an
12177 lvalue-rvalue conversion applied to "a glvalue of literal type
12178 that refers to a non-volatile temporary object initialized
12179 with a constant expression". Rather than try to communicate
12180 that this VAR_DECL is a temporary, just mark it constexpr. */
12181 DECL_DECLARED_CONSTEXPR_P (var
) = true;
12182 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var
) = true;
12183 TREE_CONSTANT (var
) = true;
12184 TREE_READONLY (var
) = true;
12186 DECL_INITIAL (var
) = init
;
12190 /* Create the INIT_EXPR that will initialize the temporary
12192 init
= split_nonconstant_init (var
, expr
);
12193 if (at_function_scope_p ())
12195 add_decl_expr (var
);
12197 if (TREE_STATIC (var
))
12198 init
= add_stmt_to_compound (init
, register_dtor_fn (var
));
12201 tree cleanup
= cxx_maybe_build_cleanup (var
, tf_warning_or_error
);
12204 if (cond_guard
&& cleanup
!= error_mark_node
)
12206 if (*cond_guard
== NULL_TREE
)
12208 *cond_guard
= build_local_temp (boolean_type_node
);
12209 add_decl_expr (*cond_guard
);
12210 tree set
= cp_build_modify_expr (UNKNOWN_LOCATION
,
12211 *cond_guard
, NOP_EXPR
,
12212 boolean_false_node
,
12213 tf_warning_or_error
);
12214 finish_expr_stmt (set
);
12216 cleanup
= build3 (COND_EXPR
, void_type_node
,
12217 *cond_guard
, cleanup
, NULL_TREE
);
12219 vec_safe_push (*cleanups
, cleanup
);
12223 /* We must be careful to destroy the temporary only
12224 after its initialization has taken place. If the
12225 initialization throws an exception, then the
12226 destructor should not be run. We cannot simply
12227 transform INIT into something like:
12229 (INIT, ({ CLEANUP_STMT; }))
12231 because emit_local_var always treats the
12232 initializer as a full-expression. Thus, the
12233 destructor would run too early; it would run at the
12234 end of initializing the reference variable, rather
12235 than at the end of the block enclosing the
12236 reference variable.
12238 The solution is to pass back a cleanup expression
12239 which the caller is responsible for attaching to
12240 the statement tree. */
12244 rest_of_decl_compilation (var
, /*toplev=*/1, at_eof
);
12245 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
12247 if (CP_DECL_THREAD_LOCAL_P (var
))
12248 tls_aggregates
= tree_cons (NULL_TREE
, var
,
12251 static_aggregates
= tree_cons (NULL_TREE
, var
,
12252 static_aggregates
);
12255 /* Check whether the dtor is callable. */
12256 cxx_maybe_build_cleanup (var
, tf_warning_or_error
);
12258 /* Avoid -Wunused-variable warning (c++/38958). */
12259 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
12261 TREE_USED (decl
) = DECL_READ_P (decl
) = true;
12267 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
12268 initializing a variable of that TYPE. */
12271 initialize_reference (tree type
, tree expr
,
12272 int flags
, tsubst_flags_t complain
)
12276 location_t loc
= cp_expr_loc_or_input_loc (expr
);
12278 if (type
== error_mark_node
|| error_operand_p (expr
))
12279 return error_mark_node
;
12281 /* Get the high-water mark for the CONVERSION_OBSTACK. */
12282 p
= conversion_obstack_alloc (0);
12284 conv
= reference_binding (type
, TREE_TYPE (expr
), expr
, /*c_cast_p=*/false,
12286 /* If this conversion failed, we're in C++20, and we have something like
12287 A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
12288 if ((!conv
|| conv
->bad_p
)
12289 && (flags
& LOOKUP_AGGREGATE_PAREN_INIT
))
12291 tree e
= build_constructor_single (init_list_type_node
, NULL_TREE
, expr
);
12292 CONSTRUCTOR_IS_DIRECT_INIT (e
) = true;
12293 CONSTRUCTOR_IS_PAREN_INIT (e
) = true;
12294 conversion
*c
= reference_binding (type
, TREE_TYPE (e
), e
,
12295 /*c_cast_p=*/false, flags
, complain
);
12296 /* If this worked, use it. */
12297 if (c
&& !c
->bad_p
)
12298 expr
= e
, conv
= c
;
12300 if (!conv
|| conv
->bad_p
)
12302 if (complain
& tf_error
)
12305 convert_like (conv
, expr
, complain
);
12306 else if (!CP_TYPE_CONST_P (TREE_TYPE (type
))
12307 && !TYPE_REF_IS_RVALUE (type
)
12308 && !lvalue_p (expr
))
12309 error_at (loc
, "invalid initialization of non-const reference of "
12310 "type %qH from an rvalue of type %qI",
12311 type
, TREE_TYPE (expr
));
12313 error_at (loc
, "invalid initialization of reference of type "
12314 "%qH from expression of type %qI", type
,
12317 return error_mark_node
;
12320 if (conv
->kind
== ck_ref_bind
)
12321 /* Perform the conversion. */
12322 expr
= convert_like (conv
, expr
, complain
);
12323 else if (conv
->kind
== ck_ambig
)
12324 /* We gave an error in build_user_type_conversion_1. */
12325 expr
= error_mark_node
;
12327 gcc_unreachable ();
12329 /* Free all the conversions we allocated. */
12330 obstack_free (&conversion_obstack
, p
);
12335 /* If *P is an xvalue expression, prevent temporary lifetime extension if it
12336 gets used to initialize a reference. */
12339 prevent_lifetime_extension (tree t
)
12342 while (TREE_CODE (*p
) == COMPOUND_EXPR
)
12343 p
= &TREE_OPERAND (*p
, 1);
12344 while (handled_component_p (*p
))
12345 p
= &TREE_OPERAND (*p
, 0);
12346 /* Change a TARGET_EXPR from prvalue to xvalue. */
12347 if (TREE_CODE (*p
) == TARGET_EXPR
)
12348 *p
= build2 (COMPOUND_EXPR
, TREE_TYPE (*p
), *p
,
12349 move (TARGET_EXPR_SLOT (*p
)));
12353 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
12354 which is bound either to a reference or a std::initializer_list. */
12357 extend_ref_init_temps_1 (tree decl
, tree init
, vec
<tree
, va_gc
> **cleanups
,
12363 if (TREE_CODE (sub
) == COMPOUND_EXPR
)
12365 TREE_OPERAND (sub
, 1)
12366 = extend_ref_init_temps_1 (decl
, TREE_OPERAND (sub
, 1), cleanups
,
12370 if (TREE_CODE (sub
) == COND_EXPR
)
12372 tree cur_cond_guard
= NULL_TREE
;
12373 if (TREE_OPERAND (sub
, 1))
12374 TREE_OPERAND (sub
, 1)
12375 = extend_ref_init_temps_1 (decl
, TREE_OPERAND (sub
, 1), cleanups
,
12377 if (cur_cond_guard
)
12379 tree set
= cp_build_modify_expr (UNKNOWN_LOCATION
, cur_cond_guard
,
12380 NOP_EXPR
, boolean_true_node
,
12381 tf_warning_or_error
);
12382 TREE_OPERAND (sub
, 1)
12383 = cp_build_compound_expr (set
, TREE_OPERAND (sub
, 1),
12384 tf_warning_or_error
);
12386 cur_cond_guard
= NULL_TREE
;
12387 if (TREE_OPERAND (sub
, 2))
12388 TREE_OPERAND (sub
, 2)
12389 = extend_ref_init_temps_1 (decl
, TREE_OPERAND (sub
, 2), cleanups
,
12391 if (cur_cond_guard
)
12393 tree set
= cp_build_modify_expr (UNKNOWN_LOCATION
, cur_cond_guard
,
12394 NOP_EXPR
, boolean_true_node
,
12395 tf_warning_or_error
);
12396 TREE_OPERAND (sub
, 2)
12397 = cp_build_compound_expr (set
, TREE_OPERAND (sub
, 2),
12398 tf_warning_or_error
);
12402 if (TREE_CODE (sub
) != ADDR_EXPR
)
12404 /* Deal with binding to a subobject. */
12405 for (p
= &TREE_OPERAND (sub
, 0);
12406 TREE_CODE (*p
) == COMPONENT_REF
|| TREE_CODE (*p
) == ARRAY_REF
; )
12407 p
= &TREE_OPERAND (*p
, 0);
12408 if (TREE_CODE (*p
) == TARGET_EXPR
)
12410 tree subinit
= NULL_TREE
;
12411 *p
= set_up_extended_ref_temp (decl
, *p
, cleanups
, &subinit
, cond_guard
);
12412 recompute_tree_invariant_for_addr_expr (sub
);
12414 init
= fold_convert (TREE_TYPE (init
), sub
);
12416 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), subinit
, init
);
12421 /* INIT is part of the initializer for DECL. If there are any
12422 reference or initializer lists being initialized, extend their
12423 lifetime to match that of DECL. */
12426 extend_ref_init_temps (tree decl
, tree init
, vec
<tree
, va_gc
> **cleanups
,
12429 tree type
= TREE_TYPE (init
);
12430 if (processing_template_decl
)
12432 if (TYPE_REF_P (type
))
12433 init
= extend_ref_init_temps_1 (decl
, init
, cleanups
, cond_guard
);
12437 if (TREE_CODE (ctor
) == TARGET_EXPR
)
12438 ctor
= TARGET_EXPR_INITIAL (ctor
);
12439 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
12441 /* [dcl.init] When initializing an aggregate from a parenthesized list
12442 of values... a temporary object bound to a reference does not have
12443 its lifetime extended. */
12444 if (CONSTRUCTOR_IS_PAREN_INIT (ctor
))
12447 if (is_std_init_list (type
))
12449 /* The temporary array underlying a std::initializer_list
12450 is handled like a reference temporary. */
12451 tree array
= CONSTRUCTOR_ELT (ctor
, 0)->value
;
12452 array
= extend_ref_init_temps_1 (decl
, array
, cleanups
,
12454 CONSTRUCTOR_ELT (ctor
, 0)->value
= array
;
12459 constructor_elt
*p
;
12460 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
12461 FOR_EACH_VEC_SAFE_ELT (elts
, i
, p
)
12462 p
->value
= extend_ref_init_temps (decl
, p
->value
, cleanups
,
12465 recompute_constructor_flags (ctor
);
12466 if (decl_maybe_constant_var_p (decl
) && TREE_CONSTANT (ctor
))
12467 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = true;
12474 /* Returns true iff an initializer for TYPE could contain temporaries that
12475 need to be extended because they are bound to references or
12476 std::initializer_list. */
12479 type_has_extended_temps (tree type
)
12481 type
= strip_array_types (type
);
12482 if (TYPE_REF_P (type
))
12484 if (CLASS_TYPE_P (type
))
12486 if (is_std_init_list (type
))
12488 for (tree f
= next_initializable_field (TYPE_FIELDS (type
));
12489 f
; f
= next_initializable_field (DECL_CHAIN (f
)))
12490 if (type_has_extended_temps (TREE_TYPE (f
)))
12496 /* Returns true iff TYPE is some variant of std::initializer_list. */
12499 is_std_init_list (tree type
)
12501 if (!TYPE_P (type
))
12503 if (cxx_dialect
== cxx98
)
12505 /* Look through typedefs. */
12506 type
= TYPE_MAIN_VARIANT (type
);
12507 return (CLASS_TYPE_P (type
)
12508 && CP_TYPE_CONTEXT (type
) == std_node
12509 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
)));
12512 /* Returns true iff DECL is a list constructor: i.e. a constructor which
12513 will accept an argument list of a single std::initializer_list<T>. */
12516 is_list_ctor (tree decl
)
12518 tree args
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
12521 if (!args
|| args
== void_list_node
)
12524 arg
= non_reference (TREE_VALUE (args
));
12525 if (!is_std_init_list (arg
))
12528 args
= TREE_CHAIN (args
);
12530 if (args
&& args
!= void_list_node
&& !TREE_PURPOSE (args
))
12531 /* There are more non-defaulted parms. */
12537 #include "gt-cp-call.h"